1. 程式人生 > 其它 >使用Burpsuite代理和pypcap抓包進行搶紅包的嘗試

使用Burpsuite代理和pypcap抓包進行搶紅包的嘗試

起因

年底各廠陸續舉辦年會,年會期間自然少不了紅包,只不過我廠年底搞了個APP專門進行搶紅包,國際慣例,手快有,手慢無。於是萌生了利用指令碼嘗試搶紅包的想法。

APP分析

APP是利用彈幕的形式將紅包,交流資訊展現在公屏上,所有人看到紅包都可以去點,手快的人將獲得紅包。利用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”:[]}

搶紅包需要對應的紅包標識packetId,是由毫秒級的時間戳生成的紅包標識。在紅包未搶完之前,搶紅包的時間requestTm的接近程度則決定是否可以搶到紅包。只需要第一時間構造請求便能妥妥的搶到紅包。構造請求的關鍵是packetId,問題是如何獲取?檢視所有的burpsuite請求未發現下發的packetId。

用wireshark試試,發現了packetId。

PyPcap簡介

Python上的抓包模組,可以設定過濾器實時抓取網路資料包,配合dpkt模組可以完成對網路資料包的分析。建議在linux下安裝,win上較複雜,這裡使用kali linux執行如下命令即可,也可以從這裡獲取PyPcap。

apt-get install libpcap-dev pip install pypcap

監聽指定IP資料包

import pcap
import dpkt
import datetime
import socket
from dpkt.compat import compat_ord
 
pc=pcap.pcap(‘eth0′)    #引數可為網絡卡名,如eth0
pc.setfilter(‘src host 192.168.2.5 or dst host 192.168.2.5′)    #設定監聽過濾器,這裡指定ip
def mac_addr(address): #轉換mac地址為字串
   return ‘:’.join(‘%02x’ % compat_ord(b) for b in address)
def inet_to_str(inet): #轉換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為收到資料
   eth=dpkt.ethernet.Ethernet(buf)   
   if not isinstance(eth.data, dpkt.ip.IP):# 確認包含ip資料包
       print ‘Non IP Packet type not supported %sn’ %eth.data.__class__.__name__
       continue
   ip = eth.data   
   if isinstance(ip.data, dpkt.tcp.TCP):# tcp資料包
       tcp = ip.data       
       try:    #解析http請求
           request = dpkt.http.Request(tcp.data)
       except (dpkt.dpkt.NeedData, dpkt.dpkt.UnpackError):
           continue
       #獲取資料包資訊並列印
       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: %sn’ % repr(request)

burpsuite代理和PyPcap抓包

啟動burpsuite,設定手機wifi代理指向burpsuite。

執行編寫好的抓包指令碼,等待APP啟動抓包,所有源地址和目的地址為指定IP的資料包將被捕獲,效果圖如下:

構造請求等待搶紅包

一旦檢測到源地址為伺服器地址,且內容包含引數packetId,獲取該引數值,使用當前時間作為requestTm,隨後構造請求第一時間提交進行搶紅包。以下是構造請求的方法。

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’ : ‘application/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

結束語

這是針對我廠搶紅包APP的一個簡單分析過程,思路可能比較簡單。主要內容還是利用PyPcap進行實時網路資料監聽。至於搶了多少紅包,大家都懂的,畢竟月餅可不是那麼好搶的。