轉(zhuǎn)載自:http://mp.weixin.qq.com/s/Hx8ic80qpo0u71c8axw12g
姓名:梅金波? ? ? ? ? ? ? ? ? ? ? 學(xué)號:16010110036
【嵌牛導(dǎo)讀】年底各廠陸續(xù)舉辦年會,年會期間自然少不了紅包,只不過我廠年底搞了個APP專門進(jìn)行搶紅包,國際慣例,手快有,手慢無。于是萌生了利用腳本嘗試搶紅包的想法。
【嵌牛鼻子】APP分析, PyPcap簡介, 構(gòu)造請求等待搶紅包。
【嵌牛提問】是否可以利用腳本嘗試搶紅包呢?如何去實現(xiàn)?
【嵌牛正文】APP分析
APP是利用彈幕的形式將紅包,交流信息展現(xiàn)在公屏上,所有人看到紅包都可以去點,手快的人將獲得紅包。利用burpsuite代理獲取搶紅包的請求。
POST /usr/urm/v1/getPacket.do HTTP/1.1
[...]{“termId”:[],”appVersion”:”1.0.0″,”termTyp”:”IOS”,”channelId”:[],”osVersion”:”10.2″,”deviceId”:”abc”,”clientId”:”eeeeeeeee”,”packetId”:”201701122218100057″,”requestTm”:”20170112221811″,”tokenId”:[],”usrNo”:[]}
搶紅包需要對應(yīng)的紅包標(biāo)識packetId,是由毫秒級的時間戳生成的紅包標(biāo)識。在紅包未搶完之前,搶紅包的時間requestTm的接近程度則決定是否可以搶到紅包。只需要第一時間構(gòu)造請求便能妥妥的搶到紅包。構(gòu)造請求的關(guān)鍵是packetId,問題是如何獲取?查看所有的burpsuite請求未發(fā)現(xiàn)下發(fā)的packetId。
用wireshark試試,發(fā)現(xiàn)了packetId。
PyPcap簡介
Python上的抓包模塊,可以設(shè)置過濾器實時抓取網(wǎng)絡(luò)數(shù)據(jù)包,配合dpkt模塊可以完成對網(wǎng)絡(luò)數(shù)據(jù)包的分析。建議在linux下安裝,win上較復(fù)雜,這里使用kali linux運行如下命令即可,也可以從這里獲取PyPcap。
apt-get install libpcap-dev
pip install pypcap
監(jiān)聽指定IP數(shù)據(jù)包
import pcap
import dpkt
import datetime
import socket
from dpkt.compat import compat_ord
pc=pcap.pcap(‘eth0′)? ? #參數(shù)可為網(wǎng)卡名,如eth0
pc.setfilter(‘src host 192.168.2.5 or dst host 192.168.2.5′)? ? #設(shè)置監(jiān)聽過濾器,這里指定ip
def mac_addr(address): #轉(zhuǎn)換mac地址為字符串
? return ‘:’.join(‘%02x’ % compat_ord(b) for b in address)
def inet_to_str(inet): #轉(zhuǎn)換ip地址為字符串
? try:
? ? ? return socket.inet_ntop(socket.AF_INET, inet)
? except ValueError:
? ? ? return socket.inet_ntop(socket.AF_INET6, inet)
for timestamp,buf in pc:? ? #timestamp為收到時間,buf為收到數(shù)據(jù)
? eth=dpkt.ethernet.Ethernet(buf)?
? if not isinstance(eth.data, dpkt.ip.IP):# 確認(rèn)包含ip數(shù)據(jù)包
? ? ? print ‘Non IP Packet type not supported %s\n’ %eth.data.__class__.__name__
? ? ? continue
? ip = eth.data?
? if isinstance(ip.data, dpkt.tcp.TCP):# tcp數(shù)據(jù)包
? ? ? tcp = ip.data? ? ?
? ? ? try:? ? #解析http請求
? ? ? ? ? request = dpkt.http.Request(tcp.data)
? ? ? except (dpkt.dpkt.NeedData, dpkt.dpkt.UnpackError):
? ? ? ? ? continue
? ? ? #獲取數(shù)據(jù)包信息并打印
? ? ? do_not_fragment = bool(ip.off & dpkt.ip.IP_DF)
? ? ? more_fragments = bool(ip.off & dpkt.ip.IP_MF)
? ? ? fragment_offset = ip.off & dpkt.ip.IP_OFFMASK
? ? ? print ‘Timestamp: ‘, str(datetime.datetime.utcfromtimestamp(timestamp))
? ? ? print ‘Ethernet Frame: ‘, mac_addr(eth.src), mac_addr(eth.dst), eth.type
? ? ? print ‘IP: %s -> %s? (len=%dttl=%d DF=%d MF=%d offset=%d)’ % \
(inet_to_str(ip.src),inet_to_str(ip.dst), ip.len, ip.ttl, do_not_fragment, more_fragments,fragment_offset)
? ? ? print ‘HTTP request: %s\n’ % repr(request)
burpsuite代理和PyPcap抓包
啟動burpsuite,設(shè)置手機wifi代理指向burpsuite。
運行編寫好的抓包腳本,等待APP啟動抓包,所有源地址和目的地址為指定IP的數(shù)據(jù)包將被捕獲,效果圖如下:
構(gòu)造請求等待搶紅包
一旦檢測到源地址為服務(wù)器地址,且內(nèi)容包含參數(shù)packetId,獲取該參數(shù)值,使用當(dāng)前時間作為requestTm,隨后構(gòu)造請求第一時間提交進(jìn)行搶紅包。以下是構(gòu)造請求的方法。
def post_do(packetId):
? ? ? ? currenttime= time.strftime(‘%Y%m%d%H%M%S’,time.localtime(time.time()))
? ? ? ? requrl = ‘http://example.com/usr/urm/v1/getPacket.do‘
? ? ? ? header= {
? ? ? ? ’Host’ : ‘example.com’,
? ? ? ? ’Content-Type’ : ‘a(chǎn)pplication/json’,
? ? ? ? ’Connection ‘ : ‘close’,
? ? ? ? ’User-Agent’ : ‘MapSocial/1.0.0 (iPhone; iOS 10.2; Scale/3.00)’,
? ? ? ? ’Accept’: ‘*/*’,
? ? ? ? ’Accept-Encoding’ : ‘gzip, deflate’,
? ? ? ? ’Accept-Language’ : ‘zh-Hans-CN;q=1, en-CN;q=0.9, zh-Hant-CN;q=0.8′,
? ? ? ? ’Cookie’ : ‘SDJH_JSESSIONID=[]‘
? ? ? ? }
? ? ? ? body_value = {“termId”:[],”appVersion”:”1.0.0″,”termTyp”:”IOS”,”channelId”:[],”osVersion”:”10.2″,”deviceId”:”abc”,”clientId”:”eeeeeeeee”,”packetId”:packetId,”requestTm”:currenttime,”tokenId”:[],”usrNo”:[]}
? ? ? ? body_value_json = json.JSONEncoder().encode(body_value)?
? ? ? ? request = urllib2.Request(requrl, body_value_json, header)
? ? ? ? result = urllib2.urlopen(request).read()
? ? ? ? return result
結(jié)束語
這是針對我廠搶紅包APP的一個簡單分析過程,思路可能比較簡單。主要內(nèi)容還是利用PyPcap進(jìn)行實時網(wǎng)絡(luò)數(shù)據(jù)監(jiān)聽。至于搶了多少紅包,大家都懂的,畢竟月餅可不是那么好搶的。