在我们使用Python进行爬虫时,由于信息的过度开发和泛滥,很容易泄露信息。本文就从代码出发,看看Python在网络安全中发挥了什么作用。
0x01:利用Python解析TCP数据包
首先导入socket(用于网络编程)、struct(用于拆包解析数据包结构)、binascii(方便格式的转化)创建socket时指定socket.SOCK_RAW以接收原始数据包
Pkt为获取到的数据包
Eth_hdr 为拆包得到的以太网头部,使用unpack解出
通过第一部分的目的地址,可以解出第二部分源地址,从而看到第三部分长度/类型,同理解出IP包头里的源IP和目的IP
本节所用代码如下:<code>#!python #!/usr/bin/python import socket import struct import binascii s=socket.socket(socket.PF_PACKET,socket.SOCK_RAW,socket.htons(0x0800)) pkt = s.recvfrom(2048); ethernetHeader=pkt[0][0:14] eth_hdr = struct.unpack("!6s6s2s",ethernetHeader) binascii.hexlify(eth_hdr[0]) binascii.hexlify(eth_hdr[1]) binascii.hexlify(eth_hdr[2]) ipHeader = pkt[0][14:34] ip_hdr = struct.unpack("!12s4s4s",ipHeader) print "Source IP address:"+socket.inet_ntoa(ip_hdr[1]) print "Destination IP address:"+socket.inet_ntoa(ip_hdr[2]) tcpHeader = pkt[0][34:54] tcp_hdr = struct.unpack("!HH16s",tcpHeader) </code>本节示例输出如下:
<code>#!bash <a class="__cf_email__" href="http://drops.xmd5.com/cdn-cgi/l/email-protection">[email protected]</a>:~/Desktop/wooyunPython# ./0x01.py Source IP address:123.125.125.99 Destination IP address:10.0.3.15 <a class="__cf_email__" href="http://drops.xmd5.com/cdn-cgi/l/email-protection">[email protected]</a>:~/Desktop/wooyunPython# python Python 2.7.3 (default, Jan 2 2013, 13:56:14) [GCC 4.7.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import socket >>> import struct >>> import binascii >>> s=socket.socket(socket.PF_PACKET,socket.SOCK_RAW,socket.htons(0x0800)) >>> pkt = s.recvfrom(2048); >>> ethernetHeader=pkt[0][0:14] >>> eth_hdr = struct.unpack("!6s6s2s",ethernetHeader) >>> binascii.hexlify(eth_hdr[0]) '080027bc6ecc' >>> binascii.hexlify(eth_hdr[1]) '525400123502' >>> binascii.hexlify(eth_hdr[2]) '0800' >>> ipHeader = pkt[0][14:34] >>> ip_hdr = struct.unpack("!12s4s4s",ipHeader) >>> print "Source IP address:"+socket.inet_ntoa(ip_hdr[1]) Source IP address:123.125.125.99 >>> print "Destination IP address:"+socket.inet_ntoa(ip_hdr[2]) Destination IP address:10.0.3.15 >>> tcpHeader = pkt[0][34:54] >>> tcp_hdr = struct.unpack("!HH16s",tcpHeader) </code>
0x02:利用Python发送TCP数据包
<code>#!python #!/usr/bin/python import socket import struct rawSocket = socket.socket(socket.PF_PACKET,socket.SOCK_RAW,socket.htons(0x0800)) rawSocket.bind(("eth0",socket.htons(0x0800))) packet = struct.pack("!6s6s2s",'\xaa\xaa\xaa\xaa\xaa\xaa','\xbb\xbb\xbb\xbb\xbb\xbb','\x08\x00') rawSocket.send(packet+"Hello there") </code>
首先socket类型选择raw,然后bind一个interface就可以发包了,由于发送的是原始的数据包,因此数据包需要自己通过struct.pack包装好
0x03:利用Python+Scapy嗅探数据包
<code>#!bash <a class="__cf_email__" href="http://drops.xmd5.com/cdn-cgi/l/email-protection">[email protected]</a>:~# scapy INFO: Can't import python gnuplot wrapper . Won't be able to plot. WARNING: No route found for IPv6 destination :: (no default route?) Welcome to Scapy (2.2.0) >>> ls() ARP : ARP </code>
使用sniff监听eth1接口的3个数据包
<code>#!bash >>> pkts <Sniffed: TCP:3 UDP:0 ICMP:0 Other:0> </code>直接输入pkts会显示嗅探的概况
<code>#!bash >>> pkts[0] <Ether dst=52:54:00:12:35:02 src=08:00:27:bc:6e:cc type=0x800 |<IP version=4L ihl=5L tos=0x0 len=116 id=30377 flags=DF frag=0L ttl=64 proto=tcp chksum=0xbdeb src=10.0.3.15 dst=123.125.125.99 options=[] |<TCP sport=49157 dport=http seq=2358039370 ack=1044674792 dataofs=5L reserved=0L flags=PA window=24120 chksum=0x656 urgptr=0 options=[] |<Raw load='GET /web_ime/patch.php HTTP/1.1\r\nHost: web.pinyin.sogou.com\r\nAccept: */*\r\n\r\n' |>>>> >>> pkts[0].show() </code>使用show显示更详细
<code>#!bash ###[ Ethernet ]### dst= 52:54:00:12:35:02 src= 08:00:27:bc:6e:cc type= 0x800 ###[ IP ]### version= 4L ihl= 5L tos= 0x0 len= 116 id= 30377 flags= DF frag= 0L ttl= 64 proto= tcp chksum= 0xbdeb src= 10.0.3.15 dst= 123.125.125.99 \options\ ###[ TCP ]### sport= 49157 dport= http seq= 2358039370 ack= 1044674792 dataofs= 5L reserved= 0L flags= PA window= 24120 chksum= 0x656 urgptr= 0 options= [] ###[ Raw ]### load= 'GET /web_ime/patch.php HTTP/1.1\r\nHost: web.pinyin.sogou.com\r\nAccept: */*\r\n\r\n' >>> >>> hexdump(pkts[1]) </code>写出到pcap文件
<code>#!bash >>> read_pkts=rdpcap("demo.pcap") </code>从pcap文件中读取
<code>#!bash >>> read_pkts <demo.pcap: TCP:3 UDP:0 ICMP:0 Other:0> </code>检查发现读出与写入是一致的
<code>#!bash >>> >>> pkts=sniff(iface="eth1",filter="icmp",count=3) </code>
|
以上就是“python教程网络安全篇”的详细内容,想要了解更多Python教程欢迎持续关注编程学习网 |
扫码二维码 获取免费视频学习资料
- 本文固定链接: http://phpxs.com/post/8993/
- 转载请注明:转载必须在正文中标注并保留原文链接
- 扫码: 扫上方二维码获取免费视频资料
查 看2022高级编程视频教程免费获取