-
Notifications
You must be signed in to change notification settings - Fork 3
/
DumpModule.cpp
149 lines (113 loc) · 3.63 KB
/
DumpModule.cpp
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
#include <stdio.h>
#include <Windows.h>
#include <tlhelp32.h>
HANDLE hSnapshot = NULL;
HANDLE hProcess = NULL;
PROCESSENTRY32 pe32;
MODULEENTRY32 me32;
int iProcessCount = NULL;
int iModuleCount = NULL;
char szProcessName[256];
char szModuleName[256];
char szDumpPath[256];
BOOL bNoProcessName = NULL;
BOOL bNoModuleName = NULL;
DWORD dwBuffer;
HANDLE hToken;
TOKEN_PRIVILEGES tpToken;
LUID luid;
int main( int argc, char * argv[] )
{
printf( "*******************************************************************************\n" );
printf( "*\n* Module Dumper by blub.txt \n" );
printf( "* This Tool dumps a module of the specified process memory to a file on Hardrive*\n*\n" );
printf( "* Usage: DumpModule.exe ProcessName ModuleName DumpPath \n" );
printf( "* Example: DumpModule.exe steam.exe Steam.dll C:\\test.dump \n*\n" );
printf( "* If you don`t refer a process name you get a list of the current process, * the same with modules\n*\n" );
printf( "*******************************************************************************\n\n" );
if( argv[1] == NULL )
bNoProcessName = 1;
else
{
strcpy( szProcessName, argv[1] );
if( argv[2] == NULL )
bNoModuleName = 1;
else
{
strcpy( szModuleName, argv[2] );
strcpy( szDumpPath, argv[3] );
}
}
hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
if( hSnapshot == INVALID_HANDLE_VALUE )
{
CloseHandle( hSnapshot );
return 0;
}
pe32.dwSize = sizeof( PROCESSENTRY32 );
if( !Process32First( hSnapshot, &pe32 ) )
{
CloseHandle( hSnapshot );
return 0;
}
while( Process32Next( hSnapshot, &pe32 ) )
{
if( bNoProcessName == 1)
printf( "%s \n", pe32.szExeFile );
else if( !lstrcmp( pe32.szExeFile, szProcessName ) )
{
printf( "Process found! %s \n", szProcessName );
hProcess = OpenProcess( PROCESS_ALL_ACCESS, false, pe32.th32ProcessID );
if( OpenProcessToken( hProcess, TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY, &hToken) == 0 )
{
CloseHandle( hProcess);
return 0;
}
if( ( LookupPrivilegeValue( 0, SE_SECURITY_NAME, &luid ) == 0) || ( LookupPrivilegeValue( 0, SE_DEBUG_NAME, &luid ) == 0 ) )
{
CloseHandle( hProcess );
return 0;
}
tpToken.PrivilegeCount = 1;
tpToken.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
tpToken.Privileges[0].Luid = luid;
AdjustTokenPrivileges( hToken, false, &tpToken, sizeof( tpToken ), NULL, 0 );
hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, pe32.th32ProcessID );
me32.dwSize = sizeof( MODULEENTRY32 );
if ( !Module32First( hSnapshot, &me32 ) )
{
CloseHandle( hSnapshot );
return 0;
}
while( Module32Next( hSnapshot, &me32) )
{
if( bNoModuleName == 1 )
printf( "%s \n", me32.szModule );
else if( !lstrcmp( me32.szModule, szModuleName ) )
{
LPVOID lpBuffer;
LPDWORD dwBytesWritten;
HANDLE hDrop;
printf( "Module found! %s \n", szModuleName );
printf( "- Base Address: 0x%x \n", me32.modBaseAddr );
printf( "- Base Size: %d \n", me32.modBaseSize );
lpBuffer = VirtualAlloc( NULL, me32.modBaseSize, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE );
ReadProcessMemory( hProcess, me32.modBaseAddr, lpBuffer, me32.modBaseSize, NULL );
hDrop = CreateFile( szDumpPath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
if( hDrop != INVALID_HANDLE_VALUE )
{
WriteFile( hDrop, lpBuffer, me32.modBaseSize, dwBytesWritten, NULL );
}
VirtualFree( lpBuffer, NULL, MEM_RELEASE );
CloseHandle( hDrop );
CloseHandle( hSnapshot );
CloseHandle( hProcess );
return 1;
}
}
}
}
CloseHandle( hSnapshot );
Sleep( 5 );
return 1;
}