cobf
PE imports obfuscator
shellcode.hpp
Go to the documentation of this file.
1 
9 #ifndef SHELLCODE_HPP
10 #define SHELLCODE_HPP
11 
12 // The includes.
13 #include <Windows.h>
14 #include <winternl.h>
15 #include <intrin.h>
16 
17 // No inline macros.
18 #ifdef _MSC_VER
19 #define no_inline __declspec(noinline)
20 #else
21 #define no_inline __attribute__((noinline))
22 #endif
23 
24 // Resolving a function internally.
25 #define sh_resolve(base, funs, fun) (decltype(fun)*)((PBYTE)base + \
26  funs[shellcode_fun_##fun])
27 
28 // Utility class represents the shellcode builder.
29 class shellcode
30 {
31  // To access the internals.
32  friend class cobf;
33 private:
34 
35  // Union of the symbol hash and ordinal.
36  typedef union {
37  DWORD sym_hash;
38  WORD sym_ord;
39  } u_sym_info;
40 
41  // Struct of an obfuscated API.
42  typedef struct {
43  DWORD dll_name;
44  BOOL by_name;
45  u_sym_info sym_info;
46  DWORD sym_thnk;
47  } obfuscated_sym, * p_obfuscated_sym;
48 
49  // Api set schema version 3.
50  typedef struct _ApiSetHeader63 {
51  DWORD Version;
52  DWORD Size;
53  DWORD Sealed;
54  DWORD NumberOfApisets;
55  DWORD NamesOffset;
56  DWORD TableOffset;
57  DWORD Multiplier;
58  } ApiSetHeader63, * PApiSetHeader63;
59 
60  // Api set schema name entry.
61  typedef struct _ApisetNameEntry {
62  DWORD Sealed;
63  DWORD Offset;
64  DWORD Ignored;
65  DWORD Size;
66  DWORD HostOffset;
67  DWORD NumberOfHosts;
68  } ApisetNameEntry, * PApisetNameEntry;
69 
70  // Api set schema value entry.
71  typedef struct _ApisetValueEntry {
72  DWORD Ignored;
73  DWORD NameOffset;
74  DWORD NameLength;
75  DWORD ValueOffset;
76  DWORD ValueLength;
77  } ApisetValueEntry, * PApisetValueEntry;
78 
79  // Api set schema version 6.
80  typedef struct _ApiSetHeader6 {
81  DWORD Version;
82  DWORD Count;
83  } ApiSetHeader6, * PApiSetHeader6;
84 
85  // Api set schema another version name entry.
86  typedef struct _ApisetNameEntry2 {
87  DWORD NameOffset;
88  DWORD NameLength;
89  DWORD DataOffset;
90  } ApisetNameEntry2, * PApisetNameEntry2;
91 
92  // Api set schema another version value entry.
93  typedef struct _ValuesArray2 {
94  DWORD Count;
95  DWORD NameLength;
96  DWORD DataOffset;
97  } ValuesArray2, * PValuesArray2;
98 
99  // Api set schema another version values entry.
100  typedef struct _ValuesEntry2 {
101  DWORD NameOffset;
102  DWORD NameLength;
103  DWORD ValueOffset;
104  DWORD ValueLength;
105  } ValuesEntry2, * PValuesEntry2;
106 
107  // For LdrLoadDll.
108  typedef NTSTATUS (WINAPI* pLdrLoadDll)(
109  PCWSTR PathToFile,
110  DWORD Flags,
111  PUNICODE_STRING ModuleFileName,
112  PVOID* ModuleHandle);
117  static no_inline VOID exit();
118 
124  static no_inline size_t wstr_len(PWSTR str);
125 
131  static no_inline DWORD hash_string(PCHAR str);
132 
138  static no_inline VOID str_cpy(PCHAR out, PSTR in);
139 
145  static no_inline DWORD str_toi(PSTR str);
146 
153  static no_inline PCHAR str_chr(PSTR str, CHAR chr);
154 
161  static no_inline VOID ansi_to_wide(PCHAR str, PWCHAR out, size_t size);
162 
169  static no_inline VOID wstr_cpy(PWSTR str1, PCWSTR str2, size_t length);
170 
178  static no_inline INT wstr_i_cmp(PWSTR str1, PCWSTR str2, size_t length);
179 
185  static no_inline HANDLE get_dll_handle(HANDLE pe_base, PDWORD sh_funs, PWSTR dll_name);
186 
192  static no_inline HANDLE load_dll(HANDLE pe_base, PDWORD sh_funs, PCHAR dll_name);
193 
201  static no_inline BOOL resolve_api_set(HANDLE pe_base, PDWORD sh_funs, PVOID schema_map,
202  PCWSTR virtual_dll, PWCHAR real_dll);
203 
211  static no_inline PVOID get_symbol_ptr(HANDLE pe_base, PDWORD sh_funs, HANDLE dll_handle,
212  u_sym_info sym_info, BOOL by_name);
213 
220  static no_inline VOID WINAPI load_syms(HANDLE pe_base, DWORD sh_funs_rva, DWORD syms_rva);
221 
225  static no_inline VOID funs_end();
226 
227  // Enum of the functions used at the shellcode.
228  typedef enum {
229  shellcode_fun_exit = 0,
230  shellcode_fun_wstr_len,
231  shellcode_fun_hash_string,
232  shellcode_fun_str_cpy,
233  shellcode_fun_str_toi,
234  shellcode_fun_str_chr,
235  shellcode_fun_ansi_to_wide,
236  shellcode_fun_wstr_cpy,
237  shellcode_fun_wstr_i_cmp,
238  shellcode_fun_get_dll_handle,
239  shellcode_fun_load_dll,
240  shellcode_fun_resolve_api_set,
241  shellcode_fun_get_symbol_ptr,
242  shellcode_fun_load_syms,
243  shellcode_number_of_functions
244  } t_shellcode_funs;
245 
246 public:
247 
248  static PVOID shellcode_start;
249  static DWORD shellcode_entry;
250  static DWORD shellcode_size;
253  static DWORD shellcodes_funs[shellcode_number_of_functions];
254 };
255 
256 #endif // !SHELLCODE_HPP.
cobf
Definition: cobf.hpp:26
shellcode::shellcode_size
static DWORD shellcode_size
Definition: shellcode.hpp:250
no_inline
#define no_inline
Definition: shellcode.hpp:21
shellcode::shellcode_start
static PVOID shellcode_start
Definition: shellcode.hpp:248
shellcode::shellcode_entry
static DWORD shellcode_entry
Definition: shellcode.hpp:249
shellcode
Definition: shellcode.hpp:30
shellcode::shellcodes_funs
static DWORD shellcodes_funs[shellcode_number_of_functions]
Definition: shellcode.hpp:253