栏目:经验分享 作者:小天 点击: 15,555 次
由于自己折腾的一些Linux小工具,有的功能需要登录网站才能够获得,遇到这种问题一般就是用Chrome自带的F12找到并分析一下登录包,一般登录协议都是类似表单的提交,使用HTTP POST协议,然后将登录协议使用Linux下命令行curl工具尝试一下POST数据(-d “登录数据”)看看返回包是否与浏览器登录的结果一致。如果不一致,就尝试增加一些HTTP头,比如Referer或者User-Agent头,有的后端会检查这些字段,最坏的可能就是把所有浏览器中发过去的HTTP头都带上,这样一般情况下都可以登录成功。
但是由于一些网站登录时在登录包中会发一些特殊的标记字符串过去,我们并不了解它发过去的字符串是什么含义,也不那么容易找到这个字符串是怎么计算出来的。为了更加方便地说明问题,我随便找了一个安全系数非常高的淘宝网,淘宝网的登录相信不少人已经研究过了,这篇文章不打算深入研究。
使用Chrome抓到淘宝的登录协议,需要勾选Preserve log防止页面跳转清空抓到的包,得到的请求表单如下,相关的隐私数据我已经替换成*或者其他的内容。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
TPL_username:********* TPL_password: ncoSig: ncoSessionid: ncoToken:2ddf7542c91184d8510a5891da207685e1930b49 slideCodeShow:false lang:zh_CN loginsite:0 newlogin:0 TPL_redirect_url:https://www.taobao.com/ from:tb fc:default style:default css_style: keyLogin:false qrLogin:true newMini:false newMini2:false tid: loginType:3 minititle: minipara: pstrong: sign: need_sign: isIgnore: full_redirect: sub_jump: popid: callback: guf: not_duplite_str: need_user_id: poy: gvfdcname:10 gvfdcre:2542524D888FD32F4A51A6D96331D5EAA300358C7A0FAF168D22CAB69BCA05D296A2D779A52CDEE643D5BD9B06B60A7846C4529834C98124F89A0F6C113AE3FF279AF1E6C6286BBBB743FC2A7213949F25593C51CBEE64F4FBAFD6948B6529CAA2B78E476359875FD1BC138D22D4F78150C6E808345C2CC649EE93D50739C656E94EC88A32C7FFEFA from_encoding: sub: TPL_password_2:e620eeb69bd166c8be89fb8f2fc57d6f63a9ee7e2f21a26fa522e3b62a53da1a6fbfc0bd96b0a2ea295157c67493bd72ceff21059c3d924d175182a52a5e65fc3c43f409550dfad1f904fb0771132ab2531a40c9941375ed8e29066a7c1d5cb75ef2542524d888fd32f4a51a6d96331d5eaa300358c7a0faf168d22cab69b loginASR:1 loginASRSuc:1 allp: oslanguage:zh-CN sr:1920*1080 osVer:windows|6.1 naviVer:chrome|53.02785116 miserHardInfo: appkey: um_token:2fc57d6f63a9ee7e2f21a26fa522e3b62a53da1a ua:ZTYyMGVlYjY5YmQxNjZjOGJlODlmYjhmMmZjNTdkNmY2M2E5ZWU3ZjFhMjZmYTUyMmUzYjYyYTUzZGExYTZmYmZjMGJhZmRkOTZiMGEyZWEyOTUxNTdjNjc0OTNiZDcyY2VmZjIxMDU5YzNkOTI0ZDE3NTE4MmE1MmE1ZTY1ZmMzYzQzZjQwOTU1MGRmYWQxZjkwNGZiMDc3MTEzMmFiMjUzMWE0MGM5OTQxMzc1ZWQ4ZTI5MDY2YTdjMWQ1Y2I3NWVmMjU0MjUyNGQ4ODhmZDMyZjRhNTFhNmQ5NjMzMWQ1ZWFhMzAwMzU4YzdhMGZhZjE2OGQyMmNhYjY5YgDQrnoIHlgLwJ5a2X56ymCeeggeWAvAnlrZfnrKYJ56CB5YC8CeWtl+espgnnoIHlgLwJ5a2X56ymCeeggeWAvAnlrZfnrKYJ56CB5YC8CeWtl+espgnnoIHlgLwJ5a2X56ymCeeggeWAvAnlrZfnrKYNCjAJQQk4CUkJMTYJUQkyNAlZCTMyCWcJNDAJbwk0OAl3CTU2CTQNCjEJQgk5CUoJMTcJUgkyNQlaCTMzCWgJNDEJcAk0OQl4CTU3CTUNCjIJQwkxMAlLCTE4CVMJMjYJYQkzNAlpCTQyCXEJNTAJeQk1OAk2DQozCUQJMTEJTAkxOQlUCTI3CWIJMzUJagk0MwlyCTUxCXoJNTkJNw0KNAlFCTEyCU0JMjAJVQkyOAljCTM2CWsJNDQJcwk1MgkwCTYwCTgNCjUJRgkxMwlOCTIxCVYJMjkJZAkzNwlsCTQ1CXQJNTMJMQk2MQk5DQo2CUcJMTQJTwkyMglXCTMwCWUJMzgJbQk0Ngl1CTU0CTIJNjIJKw0KNwlICTE1CVAJMjMJWAkzMQlmCTM5CW4JNDcJdgk1NQkzCTYzCS8NClNFT+ebuOWFs+WFtuS7luW3peWFt+ebuOWFsU0VP55u45YWz5YW25LuW5bel5YW355u45YWz572R56uZ5aSH5qGIIFdhcOmAgumFjSDnvZHnq5nmtYvpgJ9QSyDov4fmnJ/ln5/lkI3mn |
可以看到登录协议里有几个字段是经过编码或者是特殊标记的,比如ncoToken,gvfdcre,TPL_password_2,um_token和ua。如果需要分析出淘宝的登录协议,需要找到这几个字段是否必须,如何计算。
如果遇到验证码或者一些特殊的验证方式,比如拼图之类的,自动登录就难上加难了。
为了绕过这种登录协议比较复杂的网站的登录,可以采用浏览器人工登录,然后导入Cookie的方式,不过这样也会带来几个比较严重的问题:
如果接受上述两个条件的话,那么就可以进入实操阶段了。
Chrome有很多插件用于编辑、导入和导出Cookie,我试用了几款,最终选择了EditThisCookie这款功能比较多的插件。
安装好插件后,可以在选项中设置导出Cookie的格式,我们选择Netscape HTTP Cookie File,因为curl支持这种格式。
现在登录淘宝,完成后点击插件,然后点击导出Cookie按钮:
导出的Cookie文件内容大约是这样,其中隐私内容已经被替换掉。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# Netscape HTTP Cookie File # http://curl.haxx.se/rfc/cookie_spec.html # This file was generated by EditThisCookie # URL: https://www.taobao.com/ .taobao.com FALSE / FALSE 1506653985.701691 _cc_ VTcL3FSpdA%3D%3D .taobao.com FALSE / FALSE _l_g_ Ug%3D%3D .taobao.com FALSE / FALSE _nk_ ********* .taobao.com FALSE / FALSE _tb_token_ adb108c61f3f .taobao.com FALSE / FALSE 1694321569.582931 ali_ab 121.14.96.125.1378961569676.2 .taobao.com FALSE / FALSE 1689302468.418356 cna bd83adb108c61f3fd48f12fa .taobao.com FALSE / FALSE cookie1 BD83ADB108C61F3FD48F12FA7C88A7F5FD01D .taobao.com FALSE / FALSE cookie17 e1f32d9046be1 .taobao.com FALSE / FALSE cookie2 9eb686bf3eb5bd83d9373eadf6504 .taobao.com FALSE / FALSE existShop 7E2F21A26FA522E3B62A53DA .taobao.com FALSE / FALSE 1490669988 isg E6FCB8463CEA70880A44EB981AA55984651B2DC16275A24D .taobao.com FALSE / FALSE 1896134400 l a1ae760668b6273d38c832c153 .taobao.com FALSE / FALSE 1477709985.701448 lgc ********* .taobao.com FALSE / FALSE linezing_session A7F5FD01DF3DA75F5A8337C6699916182B6F7C10D .taobao.com FALSE / FALSE 1741000114 lzstat_uv 56465465165498764635465|546456456@645634563@4564564 .taobao.com FALSE / FALSE 1556675522.081623 miid 5646546513216548455 .taobao.com FALSE / FALSE 1475722787.16596 mt ci=3_1&cyk=0_0 .taobao.com FALSE / FALSE sg z4e .taobao.com FALSE / FALSE skt 5be3ebd0677ec73 .taobao.com FALSE / FALSE 1482893985.701661 t f64f3368d7171580c616474894631 .taobao.com FALSE / FALSE 1529117985.70172 tg 0 .taobao.com FALSE / FALSE 1491454562.08636 thw cn .taobao.com FALSE / FALSE 1506653985.701475 tracknick ********* .taobao.com FALSE / FALSE uc1 cookie14=VTcL3FSpdA%3D%3D%3D%3D&lng=zh_CN&cookie16=VTcL3FSpdA%3D%3D&existShop=false&cookie21=VTcL3FSpdA%3D%3D&tag=4&cookie15=Vq8l%VTcL3FSpdA%3D%3D%2F65A%3D%3D&pas=0 .taobao.com FALSE / FALSE 1475980524.633674 uc2 wuf= .taobao.com FALSE / FALSE 1477709985.701309 uc3 sg2=A7F5FD01DF3DA75F5A8337C6699916182B6F7C10D&nk2=EEcRYV4%3D&id2=VTcL3FSpdA%3D&vt3=VTcL3FSpdA%3D&lg2=VTcL3FSpdA%3D%3D .taobao.com FALSE / FALSE unb 52306844 .taobao.com FALSE / FALSE 1477709985.701416 uss 84F696E31D07F55CD45CC3C9E52F3B87AA60E6E440%EEcRYV4 .taobao.com FALSE / FALSE v 0 .taobao.com FALSE / FALSE whl -1%260%260%465432164685456 .taobao.com FALSE / FALSE 1506650777 x e%3D1%26p%3D*%26s%3D0%26c%3D0%26f%3D0%26g%3D0%26t%3D0%26__ll%3D-1%26_ato%3D0 www.taobao.com TRUE / FALSE 1752814524 _med dw:1920&dh:1080&pw:1920&ph:1080&ist:0 www.taobao.com TRUE / FALSE 1486103083 CNZZDATA1257476316 5465432135-456456786-https%253A%252F%252Fwww.google.com.hk%252F%4564534532 www.taobao.com TRUE / FALSE JSESSIONID 5A8337C6699916182B6F7C10D& |
把这些内容保存成Linux下的一个文件,名字随便取,我取成taobao.cookie好了。
要注意的是,Netscape Cookies文件的格式是这样的,每一行代表一个Cookie值,每一行从左到右分别由下面的字段组成,中间使用Tab分隔,来源Netscape’s cookie.txt file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
3.5 What are all those entries in my cookies.txt file? The layout of Netscape's cookies.txt file is such that each line contains one name-value pair. An example cookies.txt file may have an entry that looks like this: .netscape.com TRUE / FALSE 946684799 NETSCAPE_ID 100103 Each line represents a single piece of stored information. A tab is inserted between each of the fields. From left-to-right, here is what each field represents: domain - The domain that created AND that can read the variable. flag - A TRUE/FALSE value indicating if all machines within a given domain can access the variable. This value is set automatically by the browser, depending on the value you set for domain. path - The path within the domain that the variable is valid for. secure - A TRUE/FALSE value indicating if a secure connection with the domain is needed to access the variable. expiration - The UNIX time that the variable will expire on. UNIX time is defined as the number of seconds since Jan 1, 1970 00:00:00 GMT. name - The name of the variable. value - The value of the variable. |
从Chrome中导出Cookie后,可以使用下面的命令请求一下淘宝个人主页,加-L是为了让curl跟随页面跳转:
1 |
$curl -b taobao.cookie -L https://i.taobao.com |
可以看到返回了页面内容,但是仔细看的话,会发现其中并没有我们的淘宝用户名,页面的状态还是未登录的。这是因为EditThisCookie导出的Cookies文件其实不符合curl的规范的,目前有2个问题:
为了更加方便得获得curl可用的Cookies文件,我临时写了一个简单的PHP对EditThisCookie导出的Json格式(在EditThisCookie的配置中可以配置成JSON格式导出)Cookies进行转换,文件名为cookies.php,它会将提交的json格式的Cookies文件内容转换为curl可识别的Cookies文件,保存在/tmp/cookies路径中:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<head> <title>Cookies提交</title> <meta http-equiv="Content-Type" content="text/html;" /> </head> <body onLoad="document.forms.form_code.code.focus()"> <form name="form_code" action="cookies.php" method="post"> Cookies: <input type="text" name="code" /> <input type="submit" value="提交" /> </form> <br /> <xmp> <?php if($_POST["code"]!="") { $cookies = json_decode($_POST["code"]); $count_json = count($cookies); $fp = fopen("/tmp/cookies", "w"); for ($i = 0; $i < $count_json; $i++) { //.($cookies[$i]->hostOnly?"TRUE":"FALSE")."\t" $line = $cookies[$i]->domain."\tTRUE\t" .$cookies[$i]->path."\t" .($cookies[$i]->httpOnly?"TRUE":"FALSE")."\t" .($cookies[$i]->expirationDate?$cookies[$i]->expirationDate:2147483647)."\t" .($cookies[$i]->name)."\t" .($cookies[$i]->value)."\t" ."\n"; print_r($line); fwrite($fp,$line); } fclose($fp) ; } ?> </xmp> </body> |
做了修改后,Cookies文件看起来是这样的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
.taobao.com TRUE / FALSE 1506662109.7911 _cc_ VTcL3FSpdA%3D%3D .taobao.com TRUE / FALSE 0 _l_g_ Ug%3D%3D .taobao.com TRUE / FALSE 0 _nk_ ********* .taobao.com TRUE / FALSE 0 _tb_token_ adb108c61f3f .taobao.com TRUE / FALSE 1694321569.5829 ali_ab 121.14.96.125.1378961569676.2 .taobao.com TRUE / FALSE 1689302468.4184 cna bd83adb108c61f3fd48f12fa .taobao.com TRUE / TRUE 0 cookie1 BD83ADB108C61F3FD48F12FA7C88A7F5FD01D .taobao.com TRUE / TRUE 0 cookie17 e1f32d9046be1%3D .taobao.com TRUE / TRUE 0 cookie2 9eb686bf3eb5bd83d9373eadf6504 .taobao.com TRUE / FALSE 0 existShop 7E2F21A26FA522E3B62A53DA .taobao.com TRUE / FALSE 1490678194 isg E6FCB8463CEA70880A44EB981AA55984651B2DC16275A24D .taobao.com TRUE / FALSE 1896134400 l a1ae760668b6273d38c832c153 .taobao.com TRUE / FALSE 1477718109.7909 lgc ********* .taobao.com TRUE / FALSE 0 linezing_session A7F5FD01DF3DA75F5A8337C6699916182B6F7C10D .taobao.com TRUE / FALSE 1741000114 lzstat_uv 56465465165498764635465|546456456@645634563@4564564 .taobao.com TRUE / FALSE 1556675522.0816 miid 5646546513216548455 .taobao.com TRUE / FALSE 1475730909.791 mt np=&ci=3_1&cyk=0_0 .taobao.com TRUE / FALSE 0 sg z4e .taobao.com TRUE / TRUE 0 skt 5be3ebd0677ec73 .taobao.com TRUE / FALSE 1482902109.7911 t f64f3368d7171580c616474894631 .taobao.com TRUE / FALSE 1529126109.7912 tg 0 .taobao.com TRUE / FALSE 1491454562.0864 thw cn .taobao.com TRUE / FALSE 1506662109.7909 tracknick ********* .taobao.com TRUE / FALSE 0 uc1 cookie14=VTcL3FSpdA%3D%3D%3D%3D&lng=zh_CN&cookie16=VTcL3FSpdA%3D%3D&existShop=false&cookie21=VTcL3FSpdA%3D%3D&tag=4&cookie15=Vq8l%VTcL3FSpdA%3D%3D%2F65A%3D%3D&pas=0 .taobao.com TRUE / TRUE 1475980524.6337 uc2 wuf=http%3A%2F%2Fpub.alimama.com%2Fmyunion.htm%3Fspm%3Da219t.7473494.1998155389.3.MIzP39 .taobao.com TRUE / TRUE 1477718109.7908 uc3 sg2=A7F5FD01DF3DA75F5A8337C6699916182B6F7C10D&nk2=EEcRYV4%3D&id2=VTcL3FSpdA%3D&vt3=VTcL3FSpdA%3D&lg2=VTcL3FSpdA%3D%3D .taobao.com TRUE / TRUE 0 unb 52306844 .taobao.com TRUE / TRUE 1477718109.7909 uss 84F696E31D07F55CD45CC3C9E52F3B87AA60E6E440%EEcRYV4 .taobao.com TRUE / FALSE 0 v 0 .taobao.com TRUE / FALSE 0 whl -1%260%260%465432164685456 .taobao.com TRUE / FALSE 1506650777 x e%3D1%26p%3D*%26s%3D0%26c%3D0%26f%3D0%26g%3D0%26t%3D0%26__ll%3D-1%26_ato%3D0 i.taobao.com TRUE / TRUE 1475129702.8599 _mw_us_time_ 1475126103000 i.taobao.com TRUE / FALSE 1750509295.2222 ucn center |
再使用curl请求,此时就可以从回包中得到用户名了,说明Cookies已经成功导出并且可以使用,至于后面要抢月饼还是拉取数据,就看个人需要咯。
其中**表示登录的淘宝用户名:
1 2 3 |
$ curl -b /tmp/cookies -L https://i.taobao.com/ -s |grep ********* 193: value="*********" 316: <a href="https://passport.taobao.com/ac/password_find.htm?from_site=0&lang=zh_CN&login_id=*********" tabindex="6" class="forget-pwd" target="_blank"> |
来自源文:http://godmoon.wicp.net/blog/index.php/post_340.html
声明: 本文由( 小天 )原创编译,转载请保留链接: 从Chrome导出Cookie给curl使用
------====== 本站公告 ======------
欢迎使用趣域网域名百科,我们将长期提供域名使用相关知识和投资域名的方法和技巧;并提供主机、服务器的解决问题和网站运营方法。