pwn++  0.1.4
A (toy) Windows & Linux pwn library to play with modern C++.
Utils.hpp
1 #pragma once
2 
3 #include <algorithm>
4 #include <concepts>
5 #include <filesystem>
6 #include <optional>
7 #include <span>
8 #include <thread>
9 #include <type_traits>
10 #include <unordered_map>
11 #include <variant>
12 
13 #include "Architecture.hpp"
14 #include "Common.hpp"
15 
16 
17 namespace pwn::Utils
18 {
19 
20 using MemoryView = std::span<u8*>;
21 
22 namespace StringLib
23 {
24 
25 namespace Charset
26 {
27 constexpr std::string_view Lower = "abcdefghijklmnopqrstuvwxyz";
28 constexpr std::string_view Upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
29 constexpr std::string_view UpperLower = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
30 constexpr std::string_view Digits = "0123456789";
31 constexpr std::string_view Alphanumeric = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
32 constexpr std::string_view AllPrintable =
33  "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!\"#$ % &'()*+,-./:;<=>?@[\\]^_`{|}~ ";
34 constexpr std::string_view Basic64Characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/+";
35 
36 constexpr std::wstring_view WideLower = L"abcdefghijklmnopqrstuvwxyz";
37 constexpr std::wstring_view WideUpper = L"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
38 constexpr std::wstring_view WideUpperLower = L"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
39 constexpr std::wstring_view WideDigits = L"0123456789";
40 constexpr std::wstring_view WideAlphanumeric = L"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
41 constexpr std::wstring_view WideAllPrintable =
42  L"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!\"#$ % &'()*+,-./:;<=>?@[\\]^_`{|}~ ";
43 } // namespace Charset
44 
54 template<typename ToType, typename FromType, typename T = char>
55 static ToType
56 To(FromType const& src)
57 {
58  ToType dst;
59  std::transform(
60  src.cbegin(),
61  src.cend(),
62  std::back_inserter(dst),
63  [](auto const c)
64  {
65  return static_cast<T>(c);
66  });
67  return dst;
68 }
69 
70 static std::string
71 To(std::wstring const& src);
72 
73 template<typename T, typename L>
74 static std::vector<T>
75 Split(T const& src, const L delim)
76 {
77  T cur;
78  std::vector<T> dst;
79  std::for_each(
80  src.cbegin(),
81  src.cend(),
82  [&dst, &cur, &delim](const L& x)
83  {
84  if ( x == delim )
85  {
86  dst.push_back(cur);
87  cur.clear();
88  }
89  else
90  {
91  cur += x;
92  }
93  });
94  if ( !cur.empty() )
95  {
96  dst.push_back(cur);
97  }
98  return dst;
99 }
100 
104 template<typename T, typename L>
105 static T
106 Join(const std::vector<T>& Src, const L delim)
107 {
108  T Dst;
109  std::for_each(
110  Src.cbegin(),
111  Src.cend() - 1,
112  [&Dst, &delim](const T& x)
113  {
114  Dst += x + T {delim};
115  });
116  Dst += Src.back();
117  return Dst;
118 }
119 
123 template<typename T, typename L>
124 static T
125 Strip(T const& Src, const L c)
126 {
127  T Dst {Src};
128  std::erase_if(
129  Dst,
130  [&c](auto const& x)
131  {
132  return x == c;
133  });
134  return Dst;
135 }
136 }; // namespace StringLib
137 
138 
139 namespace Random
140 {
144 void
145 Seed(std::optional<u64> seed = std::nullopt);
146 
147 
153 auto
154 Next() -> u64;
155 
156 
164 auto
165 Next(u64 const max, u64 const min) noexcept -> u64;
166 
167 
173 auto
174 Byte() -> u8;
175 
176 
182 auto
183 Word() -> u16;
184 
185 
191 auto
192 Dword() -> u32;
193 
194 
200 auto
201 Qword() -> u64;
202 
203 
211 auto
212 String(u32 length, std::string_view const& charset = Utils::StringLib::Charset::AllPrintable) -> std::string;
213 
214 
218 auto
219 WideString(u32 length, std::wstring_view const& charset = Utils::StringLib::Charset::WideAllPrintable) -> std::wstring;
220 
221 
225 auto
226 AlnumWideString(u32 length) -> std::wstring;
227 
228 
232 auto
233 Buffer(u32 length) -> std::vector<u8>;
234 } // namespace Random
235 
236 
241 class Base64
242 {
243 public:
251  static auto
252  Encode(const u8* buffer, const usize buffer_length) -> Result<std::string>;
253 
254 
261  static auto
262  Encode(std::vector<u8> const& bytes) -> Result<std::string>;
263 
264 
271  static auto
272  Decode(std::string_view const& encoded_string) -> Result<std::vector<u8>>;
273 };
274 
275 
276 class Pack
277 {
278 public:
286  static std::vector<u8>
287  p64(u64 v, Endianess e = Endianess::unknown);
288 
289 
297  static std::vector<u8>
298  p32(u32 v, Endianess e = Endianess::unknown);
299 
300 
308  static std::vector<u8>
309  p16(u16 v, Endianess e = Endianess::unknown);
310 
311 
319  static std::vector<u8>
320  p8(u8 v, Endianess e = Endianess::unknown);
321 
322 
332  template<Flattenable T, Flattenable... Args>
333  static std::vector<u8>
334  Flatten(T arg, Args... args)
335  {
336  std::vector<u8> out;
337 
338  if constexpr ( std::is_same_v<T, std::string> )
339  {
340  std::vector<u8> s = StringLib::To<std::vector<u8>>(std::string(arg));
341  out.insert(out.end(), s.begin(), s.end());
342  }
343 
344  if constexpr ( std::is_same_v<T, std::wstring> )
345  {
346  std::vector<u8> s = StringLib::To<std::vector<u8>>(std::wstring(arg));
347  out.insert(out.end(), s.begin(), s.end());
348  }
349 
350  if constexpr ( std::is_same_v<T, std::vector<u8>> )
351  {
352  out.insert(out.end(), arg.begin(), arg.end());
353  }
354 
355  if constexpr ( sizeof...(args) > 0 )
356  {
357  auto rec = Flatten(args...);
358  out.insert(out.end(), rec.begin(), rec.end());
359  }
360 
361  return out;
362  }
363 };
364 
365 
373 PWNAPI uptr
374 align(uptr a, u32 sz);
375 
376 
384 PWNAPI auto
385 cyclic(u32 Size, u32 Period = 0) -> Result<std::vector<u8>>;
386 
387 
394 PWNAPI void
395 Hexdump(const u8* Buffer, const usize BufferSize);
396 
397 
401 PWNAPI void
402 Hexdump(std::vector<u8> const& bytes);
403 
404 
408 PWNAPI void
409 Hexdump(Utils::MemoryView const& view);
410 
411 
418 template<class Rep, class Period>
419 void
420 Sleep(const std::chrono::duration<Rep, Period>& sleep_duration)
421 {
422  std::this_thread::sleep_for(sleep_duration);
423 }
424 
425 
429 void PWNAPI
430 Pause();
431 
432 
436 void PWNAPI
437 DebugBreak();
438 
439 
449 Result<std::unordered_map<u16, bool>>
450 GetExecutableCharacteristics(std::filesystem::path const& FilePath);
451 
452 
463 Result<bool>
464 GetExecutableSignature(std::filesystem::path const& FilePath);
465 
466 } // namespace pwn::Utils
Definition: Utils.hpp:242
static auto Decode(std::string_view const &encoded_string) -> Result< std::vector< u8 >>
Decode a base64 string.
static auto Encode(std::vector< u8 > const &bytes) -> Result< std::string >
Encode a vector of bytes to base64.
static auto Encode(const u8 *buffer, const usize buffer_length) -> Result< std::string >
Encode a buffer of a given size to base64.
Definition: Utils.hpp:277
static std::vector< u8 > Flatten(T arg, Args... args)
P.
Definition: Utils.hpp:334
static std::vector< u8 > p8(u8 v, Endianess e=Endianess::unknown)
Pack a 1-byte to a byte vector.
static std::vector< u8 > p16(u16 v, Endianess e=Endianess::unknown)
Pack a 2-byte to a byte vector.
static std::vector< u8 > p32(u32 v, Endianess e=Endianess::unknown)
Pack a 3-byte to a byte vector.
static std::vector< u8 > p64(u64 v, Endianess e=Endianess::unknown)
Pack a 8-byte to a byte vector.