elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Usando Git para manipular el directorio de trabajo, el índice y commits (segunda parte)


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación C/C++ (Moderadores: Eternal Idol, Littlehorse, K-YreX)
| | |-+  HTTP multipart/form-data no sube archivo correctamente.
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: HTTP multipart/form-data no sube archivo correctamente.  (Leído 1,943 veces)
Kaxperday


Desconectado Desconectado

Mensajes: 702


The man in the Middle


Ver Perfil WWW
HTTP multipart/form-data no sube archivo correctamente.
« en: 21 Marzo 2016, 17:10 pm »

Bueno, pues estoy tratando de subir un archivo a una página web, que corre un script PHP..

El caso que he probado de todas las maneras posibles sin éxito alguno, he probado a hacer POST enviando en una variable el nombre del archivo y en otra el contenido del archivo con application/x-www-form-urlencoded y recibía el nombre correctamente pero el contenido mal (los bytes del jpg), me conseguía crear el archivo en el servidor pero estaba corrupto, en el PHP simplemente habría el archivo con ese nombre y escribía el contenido en binario, pero ya os digo el archivo ocupaba 80kb y escribió solo 1,9kbs si no mal recuerdo.

También probé a usar lo mismo pero con application/octet-stream, y bueno en este caso tenía que cambiar el script PHP, pero no me interesó realmente y no le dí muchas vueltas a este método, porque quiero subirlo o bien con POST con  application/x-www-form-urlencoded o con POST con multipart/form-data.

Visto que el primero escribía el 2% del archivo como que lo dejé y pase a multipart/form-data.
Copiando os datos de firefox, analizando paquetes de firefox con los de mi aplicación, como suben ambos el archivo, y SON JODIDAMENTE IGUALES EL BODY SOLO OJO, pero uno lo sube y otro no.

Os dejo el código de la función que sube un archivo y del PHP.

Código
  1. <?php
  2. if($_FILES["archivo"]["name"]){    
  3.      if ($_FILES["archivo"]["error"] > 0) {
  4.        echo $_FILES["archivo"]["error"] . "<br/>";
  5.      } else {
  6.          if (file_exists("" . $_FILES["archivo"]["name"])) {
  7.            echo $_FILES["archivo"]["name"] . " ya existe. ";
  8.          } else {
  9.            move_uploaded_file($_FILES["archivo"]["tmp_name"],
  10.            "" . $_FILES["archivo"]["name"]);
  11.            echo "Archivo Subido <br />";
  12.          }
  13.      }
  14. }
  15. ?>
  16.  

Código
  1. bool tracker::SendFile(std::string _path)
  2. {
  3. HINTERNET hInternet;
  4.  
  5. if ((hInternet = InternetOpenA(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0)) == NO_ERROR){
  6. return false;
  7. }
  8.  
  9. if ((hInternet = InternetConnectA(hInternet, domain.c_str(), INTERNET_DEFAULT_HTTP_PORT,
  10. NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0)) == NO_ERROR){
  11. InternetCloseHandle(hInternet);
  12. return false;
  13. }
  14.  
  15. DWORD requestFlags = INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP |
  16. INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS |
  17. INTERNET_FLAG_KEEP_CONNECTION |
  18. INTERNET_FLAG_NO_AUTO_REDIRECT |
  19. INTERNET_FLAG_NO_COOKIES |
  20. INTERNET_FLAG_NO_CACHE_WRITE |
  21. INTERNET_FLAG_NO_UI |
  22. INTERNET_FLAG_RELOAD;
  23.  
  24. if ((hInternet = HttpOpenRequestA(hInternet,
  25. "POST",
  26. "/catcher.php",
  27. "HTTP/1.1",
  28. NULL, NULL,
  29. requestFlags, 0)) == NO_ERROR){
  30. InternetCloseHandle(hInternet);
  31. return false;
  32. }
  33.  
  34. std::ifstream reader("C:\\Users\\User\\Desktop\\ciclista.jpg", std::ifstream::ate | std::ifstream::binary);
  35. int size = reader.tellg();
  36. char* buffer = new char[size];
  37. reader.seekg(0, std::ios::beg);
  38. reader.read(buffer, size);
  39. reader.close();
  40.  
  41. std::string content;
  42. content = std::string(buffer, size);
  43. delete[] buffer;
  44.  
  45. std::string body;
  46. std::string boundary = "-----------------------------268991947030948";
  47.  
  48. /*Body*/
  49. body += boundary + "\r\n";
  50. body += "Content-Disposition: form-data; name=\"archivo\"; filename=\"ciclista.jpg\"\r\n";
  51. body += "Content-Type: image/jpeg\r\n\r\n";
  52. body += content + "\r\n";
  53. body += boundary + "\r\n";
  54. body += "Content-Disposition: form-data; name=\"boton\"\r\n\r\n";
  55. body += "Enviar archivo\r\n";
  56. body += boundary + "--\r\n";
  57.  
  58. /*Header*/
  59. std::string headers = "Content-Type: multipart/form-data; boundary=" + boundary + "\r\n";
  60. HttpAddRequestHeadersA(hInternet, headers.c_str(), headers.length(), HTTP_ADDREQ_FLAG_ADD);
  61.  
  62. /*Body*/
  63. std::cout << body.substr(0, 600) << body.substr(body.length() - 200, 200);
  64. std::cout << body.length() << std::endl;
  65.  
  66. if (HttpSendRequestA(hInternet, NULL, 0, (LPVOID)body.c_str(), body.length()) == NO_ERROR){
  67. InternetCloseHandle(hInternet);
  68. return false;
  69. }
  70.  
  71. std::string response;
  72. DWORD dwBytes;
  73. char ch;
  74.  
  75. while (InternetReadFile(hInternet, &ch, 1, &dwBytes))
  76. {
  77. if (dwBytes != 1) break;
  78. response += ch;
  79. }
  80.  
  81. InternetCloseHandle(hInternet);
  82. std::cout << response;
  83. }
  84.  

Bueno me he vuelto loco comparando el contenido de los paquetes, y solo puede fallar la cabecera (luego a saber que será), pero el Content-Length es igual en firefox que en mi función, luego si cambia algo sería la cabecera que tiene HOST, CONTENT-LENGTH, CONTENT-TYPE, CONNECTION KEEP ALIVE, CACHE-CONTROL NO CACHE. Al acabar el header \r\n\r\n como siempre y mando el body, ahí está el código, cuando recibo la respuesta del server recibo solo el HTML sin el "se ha subido correctamente" o "ya existe". Pero no hay errores en el PHP.

¿Qué pasa?.

Saludos :X


En línea

Cuando el poder económico parasita al político ningún partido ni dictador podrá liberarnos de él. Se reserva el 99% ese poder.
Kaxperday


Desconectado Desconectado

Mensajes: 702


The man in the Middle


Ver Perfil WWW
Re: HTTP multipart/form-data no sube archivo correctamente.
« Respuesta #1 en: 25 Marzo 2016, 01:18 am »

En serio, volviendo al tema que me tiene loco:

Esta vez he intentado subir un simple archivo de texto txt llamado logger.txt con este contenido:

Citar
hola que buenos esto es una jodida pruebaaa

FF lo manda perfecto como sieeempre, y yo mando prácticamente lo mismo pero no me funciona, el body (los datos que mando) son IGUALES. El content-length es el mismo Y HE ABIERTO 2 WIRESHARK PARA DIVIDIR LA PANTALLA Y COMPROBAR BYTE A BYTE EL BODY, Y SI SON ** IGUALES.

Tambien el header HTTP es igual que el de FF, solo que yo incluyo un header mas el de Cache-Control= no-cache, que supongo es algo totalmente irrelevante.

Peeero si que capta una diferencia el wireshark, y es que si nos vamos al campo frame del paquete en el de firefox pone:

Citar
eth:ethertype:ip:tcp:http:mime_multipart:data-text-lines:data

Y en el mío pone:

Citar
eth:ethertype:ip:tcp:http:mime_multipart:data

Es como que se come el data-text que es un protocolo dentro de http que esta dentro de mime_multipart, ¿pero si lo que mando es igual, excepto una cabecera más indiferente, y si todo igual, porqué eso es distinto porqueeee? :@

¿Será la winapi? ¿será la magia? ¿qué sera esta vez?.

Saludos y PD siento el doble post, tomadlo como una respuesta a mi mismo XD.

Y para decorar el pastel os paso el código actualizado:

Código
  1. bool Tracker::SendFile(std::wstring _path)
  2. {
  3. HINTERNET hInternet;
  4.  
  5. if ((hInternet = InternetOpenA(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0)) == NO_ERROR){
  6. return false;
  7. }
  8.  
  9. if ((hInternet = InternetConnectA(hInternet, domain.c_str(), INTERNET_DEFAULT_HTTP_PORT,
  10. NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0)) == NO_ERROR){
  11. InternetCloseHandle(hInternet);
  12. return false;
  13. }
  14.  
  15. if ((hInternet = HttpOpenRequestA(hInternet, "POST", "/catcher.php", "HTTP/1.1",
  16. NULL, NULL, NULL, 0)) == NO_ERROR){
  17. InternetCloseHandle(hInternet);
  18. return false;
  19. }
  20.  
  21. HANDLE handle;
  22. DWORD size;
  23.  
  24. if ((handle = CreateFile(_path.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_HIDDEN, 0)) == INVALID_HANDLE_VALUE){
  25. InternetCloseHandle(hInternet);
  26. return false;
  27. }
  28.  
  29. if ((size = GetFileSize(handle, NULL)) == INVALID_FILE_SIZE){
  30. CloseHandle(handle);
  31. InternetCloseHandle(hInternet);
  32. return false;
  33. }
  34.  
  35. char *buffer = new char[size]();
  36.  
  37. if (ReadFile(handle, buffer, size, NULL, NULL) == 0){
  38. CloseHandle(handle);
  39. InternetCloseHandle(hInternet);
  40. return false;
  41. }
  42.  
  43. CloseHandle(handle);
  44. std::string data = std::string(buffer, size);
  45. delete[] buffer;
  46.  
  47. std::string boundary = "-----------------------------68991947030948";
  48. std::string headers;
  49.  
  50. headers += "Host: web.com\r\n";
  51. headers += "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0\r\n";
  52. headers += "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n";
  53. headers += "Accept-Language: es-ES,es;q=0.8,en-US;q=0.5,en;q=0.3\r\n";
  54. headers += "Accept-Encoding: gzip, deflate\r\n";
  55. headers += "DNT: 1\r\n";
  56. headers += "Referer: http://web.com/catcher.php\r\n";
  57. headers += "Connection: keep-alive\r\n";
  58. headers += "Content-Type: multipart/form-data; boundary=" + boundary + "\r\n";
  59. HttpAddRequestHeadersA(hInternet, headers.c_str(), headers.length(), HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE);
  60.  
  61. std::string body;
  62. body += boundary + "\r\n";
  63. body += "Content-Disposition: form-data; name=\"archivo\"; filename=\"logger.txt\"\r\n";
  64. body += "Content-Type: text/plain\r\n\r\n";
  65. body += data;
  66. body += "\r\n" + boundary + "\r\n";
  67. body += "Content-Disposition: form-data; name=\"boton\"\r\n\r\n";
  68. body += "Enviar archivo";
  69. body += "\r\n" + boundary + "--\r\n";
  70.  
  71. std::cout << body;
  72. //std::cout << body.substr(0, 800) << body.substr(body.length() - 600, 600);
  73. //std::cout << body.length() << std::endl;
  74.  
  75. if (HttpSendRequestA(hInternet, NULL, 0, (LPVOID)body.c_str(), body.length()) == NO_ERROR){
  76. InternetCloseHandle(hInternet);
  77. return false;
  78. }
  79.  
  80. std::string response;
  81. DWORD dwBytes;
  82. char ch;
  83.  
  84. while (InternetReadFile(hInternet, &ch, 1, &dwBytes))
  85. {
  86. if (dwBytes != 1) break;
  87. response += ch;
  88. }
  89.  
  90. InternetCloseHandle(hInternet);
  91. std::cout << response;
  92. }
  93.  

El PHP sigue igual y me carga el html de la página pero no me sale el mensajito de "se ha subido el archivo" ni "ya  hay otro con ese nombre", algo falla.

Y es que en wireshark la propia descripción del paquete en el de FF pone:

Citar
POST /catcher.php HTTP/1.1  (text/plain)

Y en el mío:

Citar
POST /catcher.php HTTP/1.1

EDITO: Hola lo he solucionado, adios. ¬¬.



« Última modificación: 27 Marzo 2016, 23:31 pm por Kaxperday » En línea

Cuando el poder económico parasita al político ningún partido ni dictador podrá liberarnos de él. Se reserva el 99% ese poder.
Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
En brutus AET2,¿Que diferencia hay entre HTTP (basic auth) y HTTP(form)?
Hacking
robertito_prado 1 3,181 Último mensaje 7 Julio 2012, 13:48 pm
por robertito_prado
sqlmap inyección con --data del tipo multipart/form-data
Hacking
ukraniano 1 3,280 Último mensaje 24 Mayo 2014, 06:37 am
por zxero22
conectar archivo php con la sentencia Data::getAll();
PHP
pablohellman 1 2,433 Último mensaje 22 Noviembre 2016, 20:45 pm
por SetzerFF
Guardar datos correctamente en un archivo
Scripting
Meta 2 2,370 Último mensaje 11 Marzo 2022, 18:31 pm
por Serapis
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines