起因
年底各厂陆续举办年会,年会期间自然少不了红包,只不过我厂年底搞了个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 %s\n’ %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: %s\n’ % 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进行实时网络数据监听。至于抢了多少红包,大家都懂的,毕竟月饼可不是那么好抢的。
上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理。