forked from hugsy/stuff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshellcode_wrapper_windows.c
107 lines (86 loc) · 2 KB
/
shellcode_wrapper_windows.c
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
/**
* Q'n'd shellcode wrapper for Windows x86-32/64
*
* @_hugsy_
*/
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include <windows.h>
DWORD WINAPI SpawnShellcode(LPVOID lpSc)
{
VOID (*sc)();
sc = lpSc;
sc();
return 0;
}
SIZE_T OpenAndGetSize(LPSTR filename, HANDLE* hFile)
{
DWORD dwSize;
*hFile = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (!*hFile) {
printf("[-] CreateFile\n");
CloseHandle(hFile);
return -1;
}
dwSize = GetFileSize(*hFile, NULL);
if (dwSize == INVALID_FILE_SIZE) {
printf("[-] GetFileSize\n");
CloseHandle(hFile);
}
return dwSize;
}
LPVOID* AllocAndMap(HANDLE *hFile, DWORD dwBytesToRead)
{
LPVOID code = NULL;
DWORD dwBytesRead;
code = VirtualAlloc(NULL, dwBytesToRead+1, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
if (!code) {
printf("[-] VirtualAlloc\n");
return NULL;
}
ZeroMemory(code, dwBytesToRead+1);
if( !ReadFile(*hFile, code, dwBytesToRead, &dwBytesRead, NULL) ||
dwBytesRead != dwBytesToRead) {
printf("[-] ReadFile\n");
VirtualFree(code, dwBytesToRead+1, MEM_RELEASE);
return NULL;
}
return code;
}
VOID MapShellcodeInMemory(LPSTR filename)
{
SIZE_T len;
DWORD pID;
LPVOID code;
HANDLE hFile;
len = OpenAndGetSize(filename, &hFile);
if (len < 0) {
return;
}
printf("[+] '%s' is %d bytes\n", filename, len);
code = AllocAndMap(&hFile, len);
if (!code){
goto out;
}
printf("[+] Shellcode alloc-ed at %p\n", code);
printf("[+] Triggering code\n");
WaitForSingleObject(CreateThread(NULL, 0, SpawnShellcode, code, 0, &pID), INFINITE);
VirtualFree(code, len+1, MEM_RELEASE);
out:
CloseHandle(hFile);
return;
}
int main(int argc, char** argv, char** envp)
{
if (argc < 2) {
printf("Syntax:\n");
printf("%s \\path\\to\\shellcode_file\n", argv[0]);
return -1;
}
MapShellcodeInMemory(argv[1]);
return 0;
}