Advertisement
Guest User

Untitled

a guest
Apr 29th, 2016
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. #include <iostream>
  2. #include <winsock2.h>
  3. #include <string>
  4. #include <fstream>
  5. #include "windows.h"
  6. #include "stdio.h"
  7.  
  8. using namespace std;
  9.  
  10. #define PORT 80
  11. #define IP "127.0.0.1"
  12. #define HOST "locahost"
  13. #define RECEIVER "/up.php"
  14. #define COMPNAME "compname"
  15. #define PROGRAM "program"
  16. #define FILENAME "file"
  17. #define BOUNDARY "----------boundary"
  18. #define DUMMY_DATA "c2FzYXNhc2FzZGRmZGZkY2Q="
  19. #define DUMMY_FILE "ok.png"
  20.  
  21. //------------------------------------
  22. string constructBody(string args[2], string file[2]);
  23. string readFile(string fileName);
  24. //------------------------------------
  25.  
  26. int main() {
  27. // initiate the socket!
  28. SOCKET dataSock;
  29. WSADATA wsaData;
  30. int error = WSAStartup(0x0202, &wsaData);
  31. if (error != 0) {
  32. WSACleanup();
  33. exit(1); // oh shit, this shouldn't happen!
  34. }
  35. // all internets, engage!
  36. SOCKADDR_IN target;
  37. target.sin_family = AF_INET;
  38. target.sin_port = htons(PORT);
  39. target.sin_addr.s_addr = inet_addr(IP);
  40. dataSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  41. if (dataSock == INVALID_SOCKET) {
  42. exit(1); // Houston, we have a problem!
  43. }
  44. connect(dataSock, (SOCKADDR*)&target, sizeof(target));
  45.  
  46. string programNames[5][2] = {{"Browser", "Mozilla"}};
  47. string file[2] = {FILENAME, "Default.txt"};
  48.  
  49. for (int i = 0; i < 1; i++) {
  50. printf("Sending data for %s\n", (programNames[i][1]).c_str());
  51. string body = constructBody(programNames[i], file);
  52. char header[1024];
  53. sprintf(header, "POST %s HTTP 1.1\r\n"
  54. "Host: %s\r\n"
  55. "Content-Length: %d\r\n"
  56. "Connection: Keep-Alive\r\n"
  57. "Content-Type: multipart/form-data; boundary=%s\r\n"
  58. "\r\n", RECEIVER, IP, strlen(body.c_str()), BOUNDARY);
  59. // printf("%s\n\n", header);
  60. int p = send(dataSock, header, strlen(header), 0);
  61. // printf("p == %d\n", p);
  62. int k = send(dataSock, body.c_str(), strlen(body.c_str()), 0);
  63. // printf("k == %d\n", k);
  64.  
  65. char buff[1024];
  66. recv(dataSock, buff, 1024, 0);
  67. printf("%s\n\n", buff);
  68. }
  69.  
  70. closesocket(dataSock);
  71. WSACleanup();
  72. }
  73.  
  74. string readFile(string fileName) {
  75. string fileContents;
  76. ifstream tmp(fileName.c_str());
  77. getline(tmp, fileContents);
  78. tmp.close();
  79.  
  80. return fileContents;
  81. }
  82.  
  83.  
  84. string constructBody(string args[2], string file[2]) {
  85. string body;
  86. string CRLF = "\r\n";
  87.  
  88. // first we add the args
  89. body.append("--"+string(BOUNDARY)+CRLF);
  90. body.append("Content-Disposition: form-data; name=\""+string(COMPNAME)+"\""+CRLF);
  91. body.append(CRLF);
  92. body.append(args[0]+CRLF);
  93. body.append("--"+string(BOUNDARY)+CRLF);
  94. body.append("Content-Disposition: form-data; name=\""+string(PROGRAM)+"\""+CRLF);
  95. body.append(CRLF);
  96. body.append(args[1]+CRLF);
  97.  
  98. // now we add the file
  99. body.append("--"+string(BOUNDARY)+CRLF);
  100. body.append("Content-Disposition: form-data; name=\""+string(FILENAME)+"\"; filename=\""+string(DUMMY_FILE)+"\""+CRLF);
  101. body.append("Content-Type: media-type"+CRLF);
  102. body.append(CRLF);
  103. body.append(DUMMY_DATA+CRLF);
  104. body.append("--"+string(BOUNDARY)+"--"+CRLF);
  105. body.append(CRLF);
  106.  
  107. // printf(body.c_str()); exit(0);
  108.  
  109. return body;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement