-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWin32Process.cs
More file actions
767 lines (623 loc) · 29 KB
/
Win32Process.cs
File metadata and controls
767 lines (623 loc) · 29 KB
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace DllInjectorCS
{
public class Win32Process : IDisposable
{
private const UInt32 INFINITE = 0xFFFFFFFF;
public static readonly string[] PrivilegeStrings = new[]
{
"SeCreateTokenPrivilege",
"SeAssignPrimaryTokenPrivilege",
"SeLockMemoryPrivilege",
"SeIncreaseQuotaPrivilege",
"SeUnsolicitedInputPrivilege",
"SeMachineAccountPrivilege",
"SeTcbPrivilege",
"SeSecurityPrivilege",
"SeTakeOwnershipPrivilege",
"SeLoadDriverPrivilege",
"SeSystemProfilePrivilege",
"SeSystemtimePrivilege",
"SeProfileSingleProcessPrivilege",
"SeIncreaseBasePriorityPrivilege",
"SeCreatePagefilePrivilege",
"SeCreatePermanentPrivilege",
"SeBackupPrivilege",
"SeRestorePrivilege",
"SeShutdownPrivilege",
"SeDebugPrivilege",
"SeAuditPrivilege",
"SeSystemEnvironmentPrivilege",
"SeChangeNotifyPrivilege",
"SeRemoteShutdownPrivilege",
"SeUndockPrivilege",
"SeSyncAgentPrivilege",
"SeEnableDelegationPrivilege",
"SeManageVolumePrivilege",
"SeImpersonatePrivilege",
"SeCreateGlobalPrivilege",
"SeTrustedCredManAccessPrivilege",
"SeRelabelPrivilege",
"SeIncreaseWorkingSetPrivilege",
"SeTimeZonePrivilege",
"SeCreateSymbolicLinkPrivilege",
};
public void Refresh()
{
m_process.Refresh();
}
public ProcessModuleCollection Modules
{
get
{
return m_process.Modules;
}
}
private readonly IntPtr m_hProcess;
private readonly Process m_process = null;
private IntPtr[] m_pausedThreads = null;
public Win32Process(int pid)
{
m_hProcess = Kernel32.OpenProcess(ProcessAccessFlags.All, false, pid);
if (m_hProcess == IntPtr.Zero)
throw new Win32Exception(Marshal.GetLastWin32Error());
if (m_process == null)
m_process = Process.GetProcessById(pid);
}
public Win32Process(Process process)
: this(process.Id)
{
m_process = process;
}
public void Dispose()
{
if (m_hProcess != IntPtr.Zero)
Kernel32.CloseHandle(m_hProcess);
}
public Module GetModule(string moduleName)
{
foreach (ProcessModule processModule in Modules)
{
if (moduleName == processModule.ModuleName)
return new Module(processModule);
}
return null;
}
public unsafe void Read(IntPtr address, byte* buffer, int offset, int size)
{
int bytesRead;
if (!Kernel32.ReadProcessMemory(m_hProcess, address, buffer + offset, size, out bytesRead))
throw new Win32Exception(Marshal.GetLastWin32Error());
}
public unsafe void Read(IntPtr address, byte[] buffer, int offset, int size)
{
fixed (byte* b = buffer)
Read(address, b, offset, size);
}
public byte[] ReadBytes(IntPtr address, int size)
{
int bytesRead;
var buffer = new byte[size];
if (!Kernel32.ReadProcessMemory(m_hProcess, address, buffer, size, out bytesRead))
throw new Win32Exception(Marshal.GetLastWin32Error());
return buffer;
}
public sbyte ReadSByte(IntPtr address)
{
return (sbyte)ReadBytes(address, 1)[0];
}
public short ReadInt16(IntPtr address)
{
return BitConverter.ToInt16(ReadBytes(address, sizeof(short)), 0);
}
public int ReadInt32(IntPtr address)
{
return BitConverter.ToInt32(ReadBytes(address, sizeof(int)), 0);
}
public long ReadInt64(IntPtr address)
{
return BitConverter.ToInt64(ReadBytes(address, sizeof(long)), 0);
}
public byte ReadByte(IntPtr address)
{
return ReadBytes(address, 1)[0];
}
public ushort ReadUInt16(IntPtr address)
{
return BitConverter.ToUInt16(ReadBytes(address, sizeof(ushort)), 0);
}
public uint ReadUInt32(IntPtr address)
{
return BitConverter.ToUInt32(ReadBytes(address, sizeof(uint)), 0);
}
public ulong ReadUInt64(IntPtr address)
{
return BitConverter.ToUInt64(ReadBytes(address, sizeof(ulong)), 0);
}
public float ReadSingle(IntPtr address)
{
return BitConverter.ToSingle(ReadBytes(address, sizeof(float)), 0);
}
public double ReadDouble(IntPtr address)
{
return BitConverter.ToDouble(ReadBytes(address, sizeof(double)), 0);
}
public unsafe T ReadStructure<T>(IntPtr address) where T : struct
{
var data = new byte[Marshal.SizeOf(typeof(T))];
fixed (byte* d = data)
{
Read(address, data, 0, data.Length);
T value = (T)Marshal.PtrToStructure(new IntPtr(d), typeof(T));
return value;
}
}
public string ReadString(IntPtr address, int maxLen)
{
return Encoding.ASCII.GetString(ReadBytes(address, maxLen));
}
public unsafe void Write(IntPtr address, byte* data, int offset, int size)
{
int bytesWritten;
var buffer = new byte[size];
if (!Kernel32.WriteProcessMemory(m_hProcess, address, data + offset, size, out bytesWritten))
throw new Win32Exception(Marshal.GetLastWin32Error());
}
public void Write(IntPtr address, byte[] value)
{
int bytesWritten;
if (!Kernel32.WriteProcessMemory(m_hProcess, address, value, value.Length, out bytesWritten))
throw new Win32Exception(Marshal.GetLastWin32Error());
}
public unsafe void Write(IntPtr address, byte[] data, int offset, int size)
{
fixed (byte* d = data)
Write(address, d, offset, size);
}
public void Write(IntPtr address, sbyte value)
{
Write(address, (byte)value);
}
public void Write(IntPtr address, short value)
{
Write(address, BitConverter.GetBytes(value));
}
public void Write(IntPtr address, int value)
{
Write(address, BitConverter.GetBytes(value));
}
public void Write(IntPtr address, long value)
{
Write(address, BitConverter.GetBytes(value));
}
public void Write(IntPtr address, byte value)
{
Write(address, new byte[] { value });
}
public void Write(IntPtr address, ushort value)
{
Write(address, BitConverter.GetBytes(value));
}
public void Write(IntPtr address, uint value)
{
Write(address, BitConverter.GetBytes(value));
}
public void Write(IntPtr address, ulong value)
{
Write(address, BitConverter.GetBytes(value));
}
public void Write(IntPtr address, float value)
{
Write(address, BitConverter.GetBytes(value));
}
public void Write(IntPtr address, double value)
{
Write(address, BitConverter.GetBytes(value));
}
public unsafe void Write<T>(IntPtr address, T value)
{
var data = new byte[Marshal.SizeOf(typeof(T))];
fixed (byte* d = data)
{
Marshal.StructureToPtr(value, new IntPtr(d), false);
Write(address, data, 0, data.Length);
}
}
public IntPtr Allocate(int size)
{
IntPtr address = Kernel32.VirtualAllocEx(m_hProcess, IntPtr.Zero, size, AllocationType.Reserve | AllocationType.Commit, MemoryProtection.ExecuteReadWrite);
if (address == IntPtr.Zero)
throw new Win32Exception(Marshal.GetLastWin32Error());
return address;
}
public void Free(IntPtr address)
{
if (Kernel32.VirtualFreeEx(m_hProcess, address, 0, FreeType.Release))
throw new Win32Exception(Marshal.GetLastWin32Error());
}
public MemoryProtection Protect(IntPtr address, int size, MemoryProtection newMemoryProtection)
{
MemoryProtection oldMemoryProtection;
if (!Kernel32.VirtualProtectEx(m_hProcess, address, size, newMemoryProtection, out oldMemoryProtection))
throw new Win32Exception(Marshal.GetLastWin32Error());
return oldMemoryProtection;
}
public IntPtr Scan(byte[] data, byte? wildcard = null)
{
bool writeable;
return Scan(data, out writeable, wildcard);
}
public IntPtr Scan(byte[] data, out bool writeable, byte? wildcard = null)
{
IntPtr address = IntPtr.Zero;
int result;
do
{
MemoryBasicInformation memoryBasicInformation;
result = Kernel32.VirtualQueryEx(m_hProcess, address, out memoryBasicInformation, (uint)Marshal.SizeOf(typeof(MemoryBasicInformation)));
if (result > 0)
{
if ((memoryBasicInformation.State & 0x1000) == 0x1000 && (memoryBasicInformation.Protect & 0x100) == 0x0)
{
if (memoryBasicInformation.AllocationProtect.HasFlag(MemoryProtection.ExecuteRead) ||
memoryBasicInformation.AllocationProtect.HasFlag(MemoryProtection.ExecuteReadWrite) ||
memoryBasicInformation.AllocationProtect.HasFlag(MemoryProtection.ReadOnly) ||
memoryBasicInformation.AllocationProtect.HasFlag(MemoryProtection.ReadWrite) ||
memoryBasicInformation.AllocationProtect.HasFlag(MemoryProtection.ExecuteWriteCopy))
{
byte[] block = ReadBytes(memoryBasicInformation.BaseAddress, memoryBasicInformation.RegionSize);
int index;
if (wildcard.HasValue)
index = ByteArrayExtension.IndexOf(block, data, wildcard.Value);
else
index = ByteArrayExtension.IndexOf(block, data);
if (index > 0)
{
if (memoryBasicInformation.AllocationProtect == MemoryProtection.ExecuteRead || memoryBasicInformation.AllocationProtect == MemoryProtection.ReadOnly)
writeable = false;
else
writeable = true;
return (IntPtr)memoryBasicInformation.BaseAddress + index;
}
}
}
address = memoryBasicInformation.BaseAddress + memoryBasicInformation.RegionSize;
}
} while (result > 0);
writeable = false;
return (IntPtr)0;
}
public int Scan(byte[] data, Stream resultOutputStream, byte wildcard)
{
int count = 0;
using (BinaryWriter binaryWriter = new BinaryWriter(resultOutputStream))
{
IntPtr address = IntPtr.Zero;
int result;
do
{
MemoryBasicInformation memoryBasicInformation;
result = Kernel32.VirtualQueryEx(m_hProcess, address, out memoryBasicInformation, (uint)Marshal.SizeOf(typeof(MemoryBasicInformation)));
if (result > 0)
{
if (memoryBasicInformation.Protect == 0 || memoryBasicInformation.AllocationProtect == MemoryProtection.NoAccess)
continue;
if (memoryBasicInformation.AllocationProtect == MemoryProtection.ExecuteRead || memoryBasicInformation.AllocationProtect == MemoryProtection.ExecuteReadWrite || memoryBasicInformation.AllocationProtect == MemoryProtection.ReadOnly || memoryBasicInformation.AllocationProtect == MemoryProtection.ReadWrite)
{
byte[] block = ReadBytes(memoryBasicInformation.BaseAddress, memoryBasicInformation.RegionSize);
int index = ByteArrayExtension.IndexOf(block, data, wildcard);
if (index > 0)
{
if (memoryBasicInformation.AllocationProtect == MemoryProtection.ExecuteRead || memoryBasicInformation.AllocationProtect == MemoryProtection.ReadOnly)
binaryWriter.Write(1);
else
binaryWriter.Write(0);
binaryWriter.Write((int)memoryBasicInformation.BaseAddress + index);
binaryWriter.Write(block);
count++;
}
}
address = memoryBasicInformation.BaseAddress + memoryBasicInformation.RegionSize;
}
} while (result > 0);
}
return count;
}
public int Scan(byte[] data, Stream resultOutputStream)
{
int count = 0;
using (BinaryWriter binaryWriter = new BinaryWriter(resultOutputStream))
{
IntPtr address = IntPtr.Zero;
int result;
do
{
MemoryBasicInformation memoryBasicInformation;
result = Kernel32.VirtualQueryEx(m_hProcess, address, out memoryBasicInformation, (uint)Marshal.SizeOf(typeof(MemoryBasicInformation)));
if (result > 0)
{
if (memoryBasicInformation.Protect == 0 || memoryBasicInformation.AllocationProtect == MemoryProtection.NoAccess)
continue;
if (memoryBasicInformation.AllocationProtect == MemoryProtection.ExecuteRead || memoryBasicInformation.AllocationProtect == MemoryProtection.ExecuteReadWrite || memoryBasicInformation.AllocationProtect == MemoryProtection.ReadOnly || memoryBasicInformation.AllocationProtect == MemoryProtection.ReadWrite)
{
byte[] block = ReadBytes(memoryBasicInformation.BaseAddress, memoryBasicInformation.RegionSize);
int index = block.IndexOf(data);
if (index > 0)
{
if (memoryBasicInformation.AllocationProtect == MemoryProtection.ExecuteRead || memoryBasicInformation.AllocationProtect == MemoryProtection.ReadOnly)
binaryWriter.Write(1);
else
binaryWriter.Write(0);
binaryWriter.Write((int)memoryBasicInformation.BaseAddress + index);
binaryWriter.Write(block);
count++;
}
}
address = memoryBasicInformation.BaseAddress + memoryBasicInformation.RegionSize;
}
} while (result > 0);
}
return count;
}
public int Rescan(byte[] data, Stream resultInputStream, int count, Stream resultOutputStream)
{
int newCount = 0;
using (BinaryWriter binaryWriter = new BinaryWriter(resultOutputStream))
using (BinaryReader binaryReader = new BinaryReader(resultInputStream))
{
for (int i = 0; i < count; i++)
{
bool readOnly = binaryReader.ReadByte() == 1;
IntPtr address = new IntPtr(binaryReader.ReadInt32());
byte[] previousData = binaryReader.ReadBytes(data.Length);
byte[] newData = ReadBytes(address, data.Length);
if (Msvcrt.memcmp(newData, previousData, newData.Length) == 0)
{
binaryWriter.Write(readOnly);
binaryWriter.Write((int)address);
binaryWriter.Write(newData);
newCount++;
}
}
}
return newCount;
}
public void Pause()
{
if (m_pausedThreads != null)
return;
m_process.Refresh();
m_pausedThreads = new IntPtr[m_process.Threads.Count];
lock (m_pausedThreads)
{
for (int i = 0; i < m_pausedThreads.Length; i++)
{
IntPtr hThread = Kernel32.OpenThread(ThreadAccess.SuspendResume, false, m_process.Threads[i].Id);
if (hThread == IntPtr.Zero)
throw new Win32Exception(Marshal.GetLastWin32Error());
m_pausedThreads[i] = hThread;
}
foreach (IntPtr hThread in m_pausedThreads)
{
if (Kernel32.SuspendThread(hThread) == -1)
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}
}
public void Resume()
{
if (m_pausedThreads == null)
return;
lock (m_pausedThreads)
{
foreach (IntPtr hThread in m_pausedThreads)
{
if (Kernel32.ResumeThread(hThread) == -1)
throw new Win32Exception(Marshal.GetLastWin32Error());
if (!Kernel32.CloseHandle(hThread))
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}
m_pausedThreads = null;
}
public void SetPrivilege(PrivilegeName privilegeName, PrivilegeAttribute privilegeAttribute)
{
TokPriv1Luid tokenPrivilege;
IntPtr hCurrentProcess = Kernel32.GetCurrentProcess();
IntPtr hProcessToken = IntPtr.Zero;
if (!AdvApi32.OpenProcessToken(hCurrentProcess, AdvApi32.TOKEN_ADJUST_PRIVILEGES | AdvApi32.TOKEN_QUERY, ref hProcessToken))
throw new Win32Exception(Marshal.GetLastWin32Error());
tokenPrivilege.Count = 1;
tokenPrivilege.Luid = 0;
tokenPrivilege.Attr = (int)privilegeAttribute;
if (!AdvApi32.LookupPrivilegeValue(null, PrivilegeStrings[(int)privilegeName], ref tokenPrivilege.Luid))
throw new Win32Exception(Marshal.GetLastWin32Error());
if (!AdvApi32.AdjustTokenPrivileges(hProcessToken, false, ref tokenPrivilege, 0, IntPtr.Zero, IntPtr.Zero))
throw new Win32Exception(Marshal.GetLastWin32Error());
}
public IntPtr CreateThread(IntPtr address, uint parameter = 0)
{
IntPtr hThread = Kernel32.CreateRemoteThread(m_hProcess, IntPtr.Zero, 0, address, new IntPtr(parameter), 0, IntPtr.Zero);
if (hThread == IntPtr.Zero)
throw new Win32Exception(Marshal.GetLastWin32Error());
return hThread;
}
public void FlushInstructionCache(IntPtr address, int size)
{
if (!Kernel32.FlushInstructionCache(m_hProcess, address, size))
throw new Win32Exception(Marshal.GetLastWin32Error());
}
public void Inject(string dllName, string functionName, uint parameter = 0)
{
byte[] workspace = new byte[1024];
IntPtr address = Allocate(workspace.Length);
IntPtr codeAddress;
Module kernel32 = GetModule("kernel32.dll");
IntPtr loadLibraryAddress = kernel32.FindExportFunction("LoadLibraryA");
IntPtr getProcAddressAddress = kernel32.FindExportFunction("GetProcAddress");
IntPtr exitProcessAddress = kernel32.FindExportFunction("ExitProcess");
IntPtr exitThreadAddress = kernel32.FindExportFunction("ExitThread");
IntPtr freeLibraryAndExitThreadAddress = kernel32.FindExportFunction("FreeLibraryAndExitThread");
using (MemoryStream memoryStream = new MemoryStream(workspace))
using (BinaryWriter binaryWriter = new BinaryWriter(memoryStream))
{
IntPtr dllNameAddress = address + (int)memoryStream.Position;
binaryWriter.WriteCString(dllName);
IntPtr functionNameAddress = address + (int)memoryStream.Position;
binaryWriter.WriteCString(functionName);
IntPtr user32Address = address + (int)memoryStream.Position;
binaryWriter.WriteCString("user32.dll");
IntPtr msgBoxNameAddress = address + (int)memoryStream.Position;
binaryWriter.WriteCString("MessageBoxA");
IntPtr errorAddress = address + (int)memoryStream.Position;
binaryWriter.WriteCString("Error");
IntPtr couldNotLoadDllAddress = address + (int)memoryStream.Position;
binaryWriter.WriteCString("Could not load the dll: " + dllName);
IntPtr couldNotLoadFunctionAddress = address + (int)memoryStream.Position;
binaryWriter.WriteCString("Could not load the function: " + functionName);
IntPtr messageBoxAddress = address + (int)memoryStream.Position;
binaryWriter.Write((uint)0);
IntPtr dllAddress = address + (int)memoryStream.Position;
binaryWriter.Write((uint)0);
codeAddress = address + (int)memoryStream.Position;
//LOAD USER32.DLL
//push dllNameAddress
binaryWriter.Write((byte)0x68);
binaryWriter.Write((uint)user32Address);
//mov eax, LoadLibraryA
binaryWriter.Write((byte)0xB8);
binaryWriter.Write((uint)loadLibraryAddress);
//call eax
binaryWriter.Write((byte)0xFF);
binaryWriter.Write((byte)0xD0);
//LOAD MessageBoxA
//push msgBoxName
binaryWriter.Write((byte)0x68);
binaryWriter.Write((uint)msgBoxNameAddress);
//push eax
binaryWriter.Write((byte)0x50);
//mov eax, GetProcAddress
binaryWriter.Write((byte)0xB8);
binaryWriter.Write((uint)getProcAddressAddress);
//call eax
binaryWriter.Write((byte)0xFF);
binaryWriter.Write((byte)0xD0);
//mov [messageBoxAddress], eax
binaryWriter.Write((byte)0xA3);
binaryWriter.Write((uint)messageBoxAddress);
//LOAD DLL
//push dllNameAddress
binaryWriter.Write((byte)0x68);
binaryWriter.Write((uint)dllNameAddress);
//mov eax, LoadLibraryA
binaryWriter.Write((byte)0xB8);
binaryWriter.Write((uint)loadLibraryAddress);
//call eax
binaryWriter.Write((byte)0xFF);
binaryWriter.Write((byte)0xD0);
//cmp eax, 0
binaryWriter.Write((byte)0x83);
binaryWriter.Write((byte)0xF8);
binaryWriter.Write((byte)0x00);
//jnz eip + 0x1E to skip over error
binaryWriter.Write((byte)0x75);
binaryWriter.Write((byte)0x1E);
//Error Code 1
//MessageBox
//push 0x10 (MB_ICONHAND)
binaryWriter.Write((byte)0x6A);
binaryWriter.Write((byte)0x10);
//push errorAddress
binaryWriter.Write((byte)0x68);
binaryWriter.Write((uint)errorAddress);
//push couldNotLoadDllAddress
binaryWriter.Write((byte)0x68);
binaryWriter.Write((uint)couldNotLoadDllAddress);
//push 0
binaryWriter.Write((byte)0x6A);
binaryWriter.Write((byte)0x00);
//mov eax, [msgBoxAddr]
binaryWriter.Write((byte)0xA1);
binaryWriter.Write((uint)msgBoxNameAddress);
//call eax
binaryWriter.Write((byte)0xFF);
binaryWriter.Write((byte)0xD0);
//Exit Process
//push 0
binaryWriter.Write((byte)0x6A);
binaryWriter.Write((byte)0x00);
//mov eax, exitProcessAddress
binaryWriter.Write((byte)0xB8);
binaryWriter.Write((uint)exitProcessAddress);
//call eax
binaryWriter.Write((byte)0xFF);
binaryWriter.Write((byte)0xD0);
//After error 1
//mov [dllAddress], eax
binaryWriter.Write((byte)0xA3);
binaryWriter.Write((uint)dllAddress);
//push functionNameAddress
binaryWriter.Write((byte)0x68);
binaryWriter.Write((uint)functionNameAddress);
//push eax
binaryWriter.Write((byte)0x50);
//mov eax, getProcAddressAddress
binaryWriter.Write((byte)0xB8);
binaryWriter.Write((uint)getProcAddressAddress);
//call eax
binaryWriter.Write((byte)0xFF);
binaryWriter.Write((byte)0xD0);
//cmp eax, 0
binaryWriter.Write((byte)0x83);
binaryWriter.Write((byte)0xF8);
binaryWriter.Write((byte)0x00);
//jnz eip + 0x1C to skip over error
binaryWriter.Write((byte)0x75);
binaryWriter.Write((byte)0x1C);
//Error Code 2
//MessageBox
//push 0x10 (MB_ICONHAND)
binaryWriter.Write((byte)0x6A);
binaryWriter.Write((byte)0x10);
//push errorAddress
binaryWriter.Write((byte)0x68);
binaryWriter.Write((uint)errorAddress);
//push couldNotLoadFunctionAddress
binaryWriter.Write((byte)0x68);
binaryWriter.Write((uint)couldNotLoadFunctionAddress);
//push 0
binaryWriter.Write((byte)0x6A);
binaryWriter.Write((byte)0x00);
//mov eax, [msgBoxAddr]
binaryWriter.Write((byte)0xA1);
binaryWriter.Write((uint)msgBoxNameAddress);
//call eax (either messagebox or the function gets invoked)
binaryWriter.Write((byte)0xFF);
binaryWriter.Write((byte)0xD0);
//Exit Process
//push 0
binaryWriter.Write((byte)0x6A);
binaryWriter.Write((byte)0x00);
//mov eax, exitProcessAddress
binaryWriter.Write((byte)0xB8);
binaryWriter.Write((uint)exitProcessAddress);
//call eax
binaryWriter.Write((byte)0xFF);
binaryWriter.Write((byte)0xD0);
}
MemoryProtection oldProtection = Protect(address, workspace.Length, MemoryProtection.ExecuteReadWrite);
Write(address, workspace);
Protect(address, workspace.Length, oldProtection);
FlushInstructionCache(address, workspace.Length);
IntPtr hThread = CreateThread(codeAddress, parameter);
Kernel32.WaitForSingleObject(hThread, INFINITE);
Free(address);
}
}
}