int httpSendPost(string server, string path, string data) { char* buf = &data[0]; string url = path + data; //combine path + data wstring url1 = wstring(url.begin(), url.end()); // turn url into a wide string wstring server1 = wstring(server.begin(), server.end()); // turn server into wide string GetLastError(); HINTERNET hInternet, hConnect, hRequest; hInternet = InternetOpen(TEXT("My UserAgent"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, NULL); hConnect = InternetConnect(hInternet, server1.c_str(), INTERNET_DEFAULT_HTTP_PORT, 0, 0, INTERNET_SERVICE_HTTP, 0, 0); hRequest = HttpOpenRequest(hConnect, TEXT("POST"), url1.c_str(), 0, 0, 0, INTERNET_FLAG_RELOAD, 0); if (hRequest == NULL) cout << "HttpOpenRequest error code: " << GetLastError() << endl; if (!HttpSendRequest(hRequest, 0, 0, buf, data.length())) cout << "HttpSendRequest error code: " << GetLastError() << endl; InternetCloseHandle(hInternet); InternetCloseHandle(hConnect); InternetCloseHandle(hRequest); return 0; } int httpSendGet(string server, string path, string data) { string url = path + data; //combine path + data wstring url1 = wstring(url.begin(), url.end()); // turn url into a wide string wstring server1 = wstring(server.begin(), server.end()); // turn server into wide string GetLastError(); HINTERNET hInternet, hConnect, hRequest; hInternet = InternetOpen(TEXT("My UserAgent"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, NULL); hConnect = InternetConnect(hInternet, server1.c_str(), INTERNET_DEFAULT_HTTP_PORT, 0, 0, INTERNET_SERVICE_HTTP, 0, 0); hRequest = HttpOpenRequest(hConnect, TEXT("GET"), url1.c_str(), 0, 0, 0, INTERNET_FLAG_RELOAD, 0); if (hRequest == NULL) cout << "HttpOpenRequest error code: " << GetLastError() << endl; if (!HttpSendRequest(hRequest, 0, 0, NULL, NULL)) cout << "HttpSendRequest error code: " << GetLastError() << endl; InternetCloseHandle(hInternet); InternetCloseHandle(hConnect); InternetCloseHandle(hRequest); return 0; } int toTempFile(string fileName, string toFile) { wstring fileName1 = wstring(fileName.begin(), fileName.end()); wstring path; wchar_t wchPath[MAX_PATH]; if (GetTempPathW(MAX_PATH, wchPath)) path = wchPath; path = path.append(fileName1.c_str()); ofstream out(path, ios::app); out << toFile << endl; out.close(); return 0; }
Friday, April 28, 2017
C++ Helper Functions for Output to File/HTTP GET/HTTP POST
Here's the combination of helper functions I'm using to send data to either files or an HTTP server, variable filename, server, URI path, etc...
Wednesday, April 26, 2017
C++ Function For HTTP GET
Also often used function for sending HTTP GET requests...
int httpSendGet(string data) { string url = "/hi/there/" + data; wstring url1 = wstring(url.begin(), url.end()); GetLastError(); HINTERNET hInternet, hConnect, hRequest; hInternet = InternetOpen(TEXT("My UserAgent"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, NULL); hConnect = InternetConnect(hInternet, TEXT("www.site.com"), INTERNET_DEFAULT_HTTP_PORT, 0, 0, INTERNET_SERVICE_HTTP, 0, 0); hRequest = HttpOpenRequest(hConnect, TEXT("GET"), url1.c_str(), 0, 0, 0, INTERNET_FLAG_RELOAD, 0); if (hRequest == NULL) cout << "HttpOpenRequest error code: " << GetLastError() << endl; if (!HttpSendRequest(hRequest, 0, 0, NULL, NULL)) cout << "HttpSendRequest error code: " << GetLastError() << endl; InternetCloseHandle(hInternet); InternetCloseHandle(hConnect); InternetCloseHandle(hRequest); return 0; }
C++ Function For HTTP POST
This is a function I use often to create POST requests by passing in a string.
I should also probably have the "www.site.com" and "/hi/there" be passed in via parameters, but meh, it works.
int httpSendPost(string post) { char* buf = &post[0]; GetLastError(); HINTERNET hInternet, hConnect, hRequest; hInternet = InternetOpen(TEXT("My UserAgent"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, NULL); hConnect = InternetConnect(hInternet, TEXT("www.site.com"), INTERNET_DEFAULT_HTTP_PORT, 0, 0, INTERNET_SERVICE_HTTP, 0, 0); hRequest = HttpOpenRequest(hConnect, TEXT("POST"), TEXT("/hi/there"), 0, 0, 0, INTERNET_FLAG_RELOAD, 0); if (hRequest == NULL) cout << "HttpOpenRequest error code: " << GetLastError() << endl; if (!HttpSendRequest(hRequest, 0, 0, buf, post.length())) cout << "HttpSendRequest error code: " << GetLastError() << endl; InternetCloseHandle(hInternet); InternetCloseHandle(hConnect); InternetCloseHandle(hRequest); return 0; }
I should also probably have the "www.site.com" and "/hi/there" be passed in via parameters, but meh, it works.
Batch Script to Find Process Connecting to IP Address
Recently I had to find what process on a remote machine was connecting to a certain IP address, to determine if it was malicious or not, using only the command line. This is how I did it, by using a batch file to watch netstat for the connection. If it sees it, it'll write the line from netstat into a file. If that file ever changes size, it'll also run tasklist to get the process name from the PID. There's probably nicer ways of doing it, but this was slapped together quickly to get the results we needed.
The file started out as one byte (I just put an "a" in it). If the netstat | findstr >> file combo worked, it would trigger the :tasker call to write the tasklist into the same file. Then I just periodically checked to see if the file was ever more than 1 byte big, and ctrl-c'd the batch script.
If someone knows of a better way of doing it, I'm all ears.
:loop set file="netstat_hits.txt" netstat -ano | findstr 10.1.2.3 >> netstat_hits.txt netstat -ano | findstr ":80 " >> netstat_hits.txt FOR /F "usebackq" %%A IN ('%file%') DO set size=%%~zA IF %size% NEQ 1 goto tasker ping 127.0.0.1 goto loop :tasker tasklist >> netstat_hits.txt goto loop
The file started out as one byte (I just put an "a" in it). If the netstat | findstr >> file combo worked, it would trigger the :tasker call to write the tasklist into the same file. Then I just periodically checked to see if the file was ever more than 1 byte big, and ctrl-c'd the batch script.
If someone knows of a better way of doing it, I'm all ears.
Subscribe to:
Comments (Atom)