如何在scapy中的HTTP GET请求后创建html文件

时间:2016-07-10 13:40:52

标签: python html http scapy packets

我需要一些scapy和python的帮助。 我发送获取请求到特定网站...然后使用嗅探和HTTP过滤器我过滤相关数据包然后我想只获取HTML代码,但我不知道如何做到这一点......

os.system('iptables -A OUTPUT -p tcp --tcp-flags RST RST -j DROP')
os.system('iptables -L')

randport = random.randint(1024,65535)
syn = IP(dst=ip) / TCP(sport = randport, dport=80, flags='S')

syn_ack = sr1(syn)   #getting the ack
getstr = 'GET / HTTP/1.1\r\nHost:' + url + '\r\n\r\n'
ack = IP(dst=ip) / TCP(dport=80, sport=syn_ack[TCP].dport,
                seq=syn_ack[TCP].ack, ack=syn_ack[TCP].seq + 1, flags='A') / getstr

send(ack)
packets = sniff(count=0, lfilter=http_filter, timeout=20)
http = open(url + ".html", "w")

for p in packets:
    if p[IP].src == ip:
         #what i need to do here?

帮助我,我只需保存HTML代码,整个代码需要做什么?保存没有HTTP标头的代码,只有我可以打开的HTML,它会像网上一样加载我的网站

2 个答案:

答案 0 :(得分:0)

from scapy. all import *
def stopfilter(x):
 if x[IP].dst == 'src_ip': #src_ip
    return True
 else:
    return False

get = 'GET /index.html HTTP/1.1\n\n'
syn_ip  = IP(src='src_ip', dst='dst_ip')  #enter your ip's here
syn_syn = TCP(sport = 5558, dport=80, flags='S',seq = 1000)
syn_ack = sr1(syn_ip/syn_syn,verbose=0)

if syn_ack:
 temp = syn_ack.seq
 myack  = temp + 1
 ack_packet = TCP(sport=5558,dport=80,flags='A',seq=syn_ack.ack,ack=myack)
 send(syn_ip/ack_packet,verbose=0)
 payload_packet = TCP(sport=5558,dport=80,flags='A',seq=syn_ack.ack, ack=myack)
 p = syn_ip/payload_packet/get
 server_resp = sr1(p,verbose=0)
 a = sniff(iface = "eth9",filter = "tcp port 80",stop_filter=stopfilter)

 for packet in a:
     if packet.getlayer(Raw):
        l = packet.getlayer(Raw).load
        rawr=Raw(l)
        rawr.show()

答案 1 :(得分:-1)

这样的东西?

import urllib2
re = urllib2.urlopen("http://www.website.org")
html = re.read()
f = open("html.html", "w")
f.write(html)
f.close()