|
.assembly extern mscorlib |
|
{ |
|
.ver 4:0:0:0 |
|
.publickeytoken = (B7 7A 5C 56 19 34 E0 89) |
|
} |
|
|
|
.assembly DllMainExample |
|
{ |
|
.ver 1:0:0:0 |
|
} |
|
|
|
.module DllMainExample.dll |
|
|
|
// STARTUPINFO structure (simplified - just need size) |
|
.class private sequential ansi sealed beforefieldinit STARTUPINFO |
|
extends [mscorlib]System.ValueType |
|
{ |
|
.pack 1 |
|
.size 68 |
|
} |
|
|
|
// PROCESS_INFORMATION structure |
|
.class private sequential ansi sealed beforefieldinit PROCESS_INFORMATION |
|
extends [mscorlib]System.ValueType |
|
{ |
|
.field public native int hProcess |
|
.field public native int hThread |
|
.field public int32 dwProcessId |
|
.field public int32 dwThreadId |
|
} |
|
|
|
// Import CreateProcess from kernel32.dll |
|
.class private auto ansi sealed Kernel32 |
|
{ |
|
.method public hidebysig static pinvokeimpl("kernel32.dll" winapi) |
|
bool CreateProcessA(native int lpApplicationName, |
|
string lpCommandLine, |
|
native int lpProcessAttributes, |
|
native int lpThreadAttributes, |
|
bool bInheritHandles, |
|
uint32 dwCreationFlags, |
|
native int lpEnvironment, |
|
native int lpCurrentDirectory, |
|
native int lpStartupInfo, |
|
native int lpProcessInformation) cil managed preservesig |
|
{ |
|
} |
|
|
|
.method public hidebysig static pinvokeimpl("kernel32.dll" winapi) |
|
bool CloseHandle(native int hObject) cil managed preservesig |
|
{ |
|
} |
|
} |
|
|
|
// DllMain function |
|
.method public hidebysig static int32 DllMain(native int hModule, |
|
uint32 ul_reason_for_call, |
|
native int lpReserved) cil managed |
|
{ |
|
.export [1] as DllMain |
|
.maxstack 16 |
|
.locals init (valuetype STARTUPINFO V_0, |
|
valuetype PROCESS_INFORMATION V_1) |
|
|
|
// Check if DLL_PROCESS_ATTACH (ul_reason_for_call == 1) |
|
ldarg.1 |
|
ldc.i4.1 |
|
bne.un IL_END |
|
|
|
// Initialize STARTUPINFO |
|
ldloca.s V_0 |
|
initobj STARTUPINFO |
|
|
|
// Initialize PROCESS_INFORMATION |
|
ldloca.s V_1 |
|
initobj PROCESS_INFORMATION |
|
|
|
// Call CreateProcess to start cmd.exe |
|
ldc.i4.0 // lpApplicationName = NULL |
|
ldstr "cmd.exe" // lpCommandLine = "cmd.exe" |
|
ldc.i4.0 // lpProcessAttributes = NULL |
|
ldc.i4.0 // lpThreadAttributes = NULL |
|
ldc.i4.0 // bInheritHandles = FALSE |
|
ldc.i4.0 // dwCreationFlags = 0 |
|
ldc.i4.0 // lpEnvironment = NULL |
|
ldc.i4.0 // lpCurrentDirectory = NULL |
|
ldloca.s V_0 // lpStartupInfo |
|
conv.i |
|
ldloca.s V_1 // lpProcessInformation |
|
conv.i |
|
call bool Kernel32::CreateProcessA(native int, string, native int, native int, bool, uint32, native int, native int, native int, native int) |
|
|
|
// If CreateProcess succeeded, close handles |
|
brfalse.s IL_END |
|
|
|
ldloc.1 // Load PROCESS_INFORMATION |
|
ldfld native int PROCESS_INFORMATION::hProcess |
|
call bool Kernel32::CloseHandle(native int) |
|
pop |
|
|
|
ldloc.1 |
|
ldfld native int PROCESS_INFORMATION::hThread |
|
call bool Kernel32::CloseHandle(native int) |
|
pop |
|
|
|
IL_END: |
|
ldc.i4.1 // Return TRUE |
|
ret |
|
} |