-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathimage_hijack.cpp
66 lines (55 loc) · 1.97 KB
/
image_hijack.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
BOOL EnumImageHijack( AUTORUN_CALLBACK pfnCallback, LPVOID lpParam )
{
CHKey hKey;
DWORD dwResult = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Image File Execution Options",
0, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS, (PHKEY)&hKey );
if(ERROR_SUCCESS != dwResult ) {
SetLastError( dwResult );
return FALSE;
}
//查询子键数量
DWORD cSubKey = 0;
dwResult = RegQueryInfoKey( hKey.GetHandle(), NULL, NULL, 0, &cSubKey, NULL,NULL, NULL, NULL, NULL, NULL, NULL );
if( ERROR_SUCCESS != dwResult ) {
SetLastError( dwResult );
return FALSE;
}
LPCTSTR lpszSystemDir = getenv( "SystemRoot" );
if( lpszSystemDir == NULL )
return FALSE;
for( DWORD i=0; i < cSubKey; i++ ) {
AUTORUN_ITEM Item = {0};
dwResult = RegEnumKey( hKey.GetHandle(), i, Item.Name, sizeof( Item.Name ) );
if( ERROR_SUCCESS != dwResult ) {
SetLastError( dwResult );
continue;
}
//打开子键
CHKey hSubKey ;
dwResult = RegOpenKeyEx( hKey.GetHandle(), Item.Name, 0, KEY_QUERY_VALUE, (PHKEY)&hSubKey );
if( ERROR_SUCCESS != dwResult ){
SetLastError( dwResult );
continue;
}
char Buffer[512] = {0};
DWORD cbSize = sizeof( Buffer );
dwResult = RegQueryValueEx( hSubKey.GetHandle(), "Debugger", NULL, NULL, (PUCHAR)Buffer, &cbSize );
if( dwResult != ERROR_SUCCESS ) {
SetLastError( dwResult );
continue;
}
strncpy( Item.ImagePath, lpszSystemDir, sizeof( Item.ImagePath ) - 1 );
PathAddBackslash( Item.ImagePath );
strncat( Item.ImagePath, "System32\\", sizeof( Item.ImagePath ) - strlen( Item.ImagePath ) - 1 );
strncat( Item.ImagePath, Buffer, sizeof( Item.ImagePath ) - strlen( Item.ImagePath ) - 1 );
//去掉删除
PathRemoveArgs( Item.ImagePath );
PathAddExtension( Item.ImagePath, ".exe" );
if( !pfnCallback( AUTORUN_IMAGE_HIJACK, &Item, lpParam ) ) {
SetLastError( ERROR_CANCELLED );
return FALSE;
}
}
return TRUE;
}