Code:
#include <windows.h>
#include <winsock.h>
#include <stdio.h>
#pragma comment( lib, "ws2_32.lib" )
char* ReadLog(const char * Filename)//Wrote this to Read Text file to char* -->there are many other ways to do this
{
HANDLE hFile =CreateFile(Filename,GENERIC_READ,NULL,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if(hFile != INVALID_HANDLE_VALUE)
{
printf("%s\n","Valid Handle To File\n");
}
DWORD num_to_read = (DWORD)GetFileSize(hFile,NULL);
DWORD num_read = -1;
char *buffer = new char[num_to_read];
//byte *bytes = new byte[num_to_read];
//char *buffer = (char*)bytes;
if(!ReadFile(hFile,buffer,num_to_read,&num_read,NULL)){return NULL;};
CloseHandle(hFile);
//buffer=(char*)bytes;
return (char*)buffer;
}
void HTTP_UPLOAD_REQUEST(char * Server,int Port,char * uploadscript,char* boundary,char*filetoup,char* data)
{
//Create Socket for sending data
WSADATA wsaData;
WSAStartup( MAKEWORD( 1,1 ), &wsaData );
SOCKET Socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
LPHOSTENT hostEntry;
hostEntry = gethostbyname( Server );//Get ip from Server by hostname
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list);
addr.sin_port = htons( Port ); //Set Port
if(connect( Socket, (LPSOCKADDR) &addr, sizeof(struct sockaddr) ) != SOCKET_ERROR)
{
printf("Connected\n");
}
else
{
printf("Not Connected\n");
}
//Socket created and connected to Server!
//Create HTTP POST Request
//construct POST Header
char header[512]="";
sprintf( header, "POST %s HTTP/1.0\r\nContent-Type: application/octet-stream; boundary=%s\r\nContent-Length: %u\r\n\r\n",uploadscript, boundary,2*strlen(data));
//construct Body(data part) of HTTP POST
char* body=new char[strlen(data)+4000];
sprintf( body, "--%s\r\nContent-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\n\r\n %s\r\n--%s\r\n",boundary,"upfile",filetoup,data,boundary);
//Put Header and Body together into Request
char * Request=new char[strlen(header)+strlen(body)+strlen(data)];
sprintf( Request, "%s%s", header,body );
printf("%s %s\n","Request",Request);
//int bytestosend = strlen(Request);
int bytessend =send( Socket, Request, strlen(Request), 0 );
printf("%s %d\n","Bytes Sent = ",bytessend);
char RecvBuffer[1024];
int bytesrecv = recv(Socket,RecvBuffer,sizeof(RecvBuffer),0);
printf("%s %d\n","Bytes Recieved = ",bytesrecv);
printf("%s\n",RecvBuffer);
closesocket(Socket);//cleanup --File isnt transmitted if not called!
}
int main(int argc, char* argv[])
{
char * buffer; //Buffer to Save data into
buffer=ReadLog("C:\\test.exe");//use absolute path! //Reads File to char*
HTTP_UPLOAD_REQUEST("scanner.novirusthanks.org",80,"","---------------------------281452328116827","test.exe",buffer);
// useage: (Server,Port,(path and)uploadscript,bundary(u can use any string u like-> its just to seperate data block in HTTP POST from anything else),Filename an Server,data to send)
return 1;
}
The actual packet looks like this in LiveHttpHeaders, a firefox addon that shows the packets sent to the web server with firefox.