我尝试将文件上传到一个流行的网站,我使用 Chrome 开发者工具的“复制为 curl”功能篡改了 http POST 请求。上传开始时,我收到此 curl 命令:
curl 'http://example.com/cgi-bin/upload.pl?upload_id=869ab8e228f7bd92bfaebc6d6bf31470' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' -H 'Referer: http://example.com/upload.php?action=gallery&gid=0&title=My+precious+car' -H 'Origin: http://example.com' -H 'User-Agent: Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36' -H 'Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryAh0xuOK8vajmXWbY' --data-binary $'------WebKitFormBoundaryAh0xuOK8vajmXWbY\r\nContent-Disposition: form-data; name="type"\r\n\r\n1\r\n------WebKitFormBoundaryAh0xuOK8vajmXWbY\r\nContent-Disposition: form-data; name="gid"\r\n\r\n0\r\n------WebKitFormBoundaryAh0xuOK8vajmXWbY\r\nContent-Disposition: form-data; name="title"\r\n\r\nMy precious car\r\n------WebKitFormBoundaryAh0xuOK8vajmXWbY\r\nContent-Disposition: form-data; name="resize"\r\n\r\n\r\n------WebKitFormBoundaryAh0xuOK8vajmXWbY\r\nContent-Disposition: form-data; name="private"\r\n\r\n\r\n------WebKitFormBoundaryAh0xuOK8vajmXWbY\r\nContent-Disposition: form-data; name="cat1"\r\n\r\n24\r\n------WebKitFormBoundaryAh0xuOK8vajmXWbY\r\nContent-Disposition: form-data; name="cat2"\r\n\r\n65\r\n------WebKitFormBoundaryAh0xuOK8vajmXWbY\r\nContent-Disposition: form-data; name="cat3"\r\n\r\n13\r\n------WebKitFormBoundaryAh0xuOK8vajmXWbY\r\nContent-Disposition: form-data; name="upfile_0"; filename="1.zip"\r\nContent-Type: application/zip\r\n\r\n\r\n------WebKitFormBoundaryAh0xuOK8vajmXWbY\r\nContent-Disposition: form-data; name="upfile_1"; filename=""\r\nContent-Type: application/octet-stream\r\n\r\n\r\n------WebKitFormBoundaryAh0xuOK8vajmXWbY--\r\n' --compressed
或者看这里:http://pastebin.com/3Jaj0mSs
当我尝试从命令行使用它时(登录并获取 cookies、upload_id 等后),文件上传进度没有启动。我尝试向 curl 命令添加选项“-T 1.zip”或“--data-binary @1.zip”或创建文件名如“<1.zip”,但没有成功,我总是收到错误消息:“发送失败:管道损坏”。文件名 1.zip 位于长命令的“--data-binary”部分的末尾。在这种情况下,我该怎么做才能使 curl 正常工作并正确完成文件上传?
答案1
我使用 Fiddler 查看了它,问题在于 Chrome 会从其生成的 curl 请求中删除文件内容。因此您必须将其重新插入。
我让它工作http://www.zippyshare.com/。
以下是 Chrome 生成的 curl 请求
curl "http://www72.zippyshare.com/upload" -H "Origin: http://www.zippyshare.com" -H "Accept-Encoding: gzip,deflate,sdch" -H "Accept-Language: en-US,en;q=0.8" -H "User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36" -H "Content-Type: multipart/form-data; boundary=----------KM7cH2GI3Ef1gL6GI3ei4Ef1Ij5KM7" -H "Accept: */*" -H "Referer: http://www.zippyshare.com/" -H "Cookie: __utma=46003887.80665104.1404275690.1404275690.1404275690.1; __utmb=46003887.1.10.1404275690; __utmc=46003887; __utmz=46003887.1404275690.1.1.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not"%"20provided); ziplocale=en" -H "Proxy-Connection: keep-alive" --data-binary '------------KM7cH2GI3Ef1gL6GI3ei4Ef1Ij5KM7
Content-Disposition: form-data; name="Filename"
Giant_Panda_Tai_Shan.jpg
------------KM7cH2GI3Ef1gL6GI3ei4Ef1Ij5KM7
Content-Disposition: form-data; name="uploadify"
true
------------KM7cH2GI3Ef1gL6GI3ei4Ef1Ij5KM7
Content-Disposition: form-data; name="embPlayerValues"
null
------------KM7cH2GI3Ef1gL6GI3ei4Ef1Ij5KM7
Content-Disposition: form-data; name="Filedata"; filename="Giant_Panda_Tai_Shan.jpg"
Content-Type: application/octet-stream
------------KM7cH2GI3Ef1gL6GI3ei4Ef1Ij5KM7
Content-Disposition: form-data; name="Upload"
Submit Query
------------KM7cH2GI3Ef1gL6GI3ei4Ef1Ij5KM7--' --compressed
我将文件所在的数据分成两部分。
第1部分:
------------KM7cH2GI3Ef1gL6GI3ei4Ef1Ij5KM7
Content-Disposition: form-data; name="Filename"
Giant_Panda_Tai_Shan.jpg
------------KM7cH2GI3Ef1gL6GI3ei4Ef1Ij5KM7
Content-Disposition: form-data; name="uploadify"
true
------------KM7cH2GI3Ef1gL6GI3ei4Ef1Ij5KM7
Content-Disposition: form-data; name="embPlayerValues"
null
------------KM7cH2GI3Ef1gL6GI3ei4Ef1Ij5KM7
Content-Disposition: form-data; name="Filedata"; filename="Giant_Panda_Tai_Shan.jpg"
Content-Type: application/octet-stream
.
(那个 . 不在末尾,我刚刚添加它是为了让代码块识别文件末尾有两行空行。)
第2部分:
.
Submit Query
------------KM7cH2GI3Ef1gL6GI3ei4Ef1Ij5KM7--
(再次使用 。)
您还需要确保文件采用 CRLF 格式。
现在你可以运行
cat part1.txt Giant_Panda_Tai_Shan.jpg part2.txt > complete
然后运行它
curl "http://www72.zippyshare.com/upload" -H "Referer: http://www.zippyshare.com/" -H "Origin: http://www.zippyshare.com" -H "User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36" -H "Content-Type: multipart/form-data; boundary=----------KM7cH2GI3Ef1gL6GI3ei4Ef1Ij5KM7" --data-binary @complete --compressed
它运行(上传需要几秒钟)并返回确认上传成功的 html。