今天就跟大家聊聊有關(guān)使用python模擬TCP連接并實(shí)現(xiàn)發(fā)送數(shù)據(jù),可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

源碼如下
from scapy.all import *
import logging
logging.getLogger('scapy.runtime').setLevel(logging.ERROR)
target_ip = '192.168.1.1'
target_port = 80
data = 'GET / HTTP/1.0 \r\n\r\n'
def start_tcp(target_ip,target_port):
global sport,s_seq,d_seq #主要是用于TCP3此握手建立連接后繼續(xù)發(fā)送數(shù)據(jù)
try:
#第一次握手,發(fā)送SYN包
ans = sr1(IP(dst=target_ip)/TCP(dport=target_port,sport=RandShort(),seq=RandInt(),flags='S'),verbose=False)
sport = ans[TCP].dport #源隨機(jī)端口
s_seq = ans[TCP].ack #源序列號(hào)(其實(shí)初始值已經(jīng)被服務(wù)端加1)
d_seq = ans[TCP].seq + 1 #確認(rèn)號(hào),需要把服務(wù)端的序列號(hào)加1
#第三次握手,發(fā)送ACK確認(rèn)包
send(IP(dst=target_ip)/TCP(dport=target_port,sport=sport,ack=d_seq,seq=s_seq,flags='A'),verbose=False)
except Exception,e:
print '[-]有錯(cuò)誤,請(qǐng)注意檢查!'
print e
def trans_data(target_ip,target_port,data):
#先建立TCP連接
start_tcp(target_ip=target_ip,target_port=target_port)
#print sport,s_seq,d_seq
#發(fā)起GET請(qǐng)求
ans = sr1(IP(dst=target_ip)/TCP(dport=target_port,sport=sport,seq=s_seq,ack=d_seq,flags=24)/data,verbose=False)
#ans.show()
#讀取服務(wù)端發(fā)來的數(shù)據(jù)
rcv = ans[Raw]
print rcv
if __name__ == '__main__':
#start_tcp(target_ip,target_port)
trans_data(target_ip,target_port,data)
新聞標(biāo)題:使用python模擬TCP連接并實(shí)現(xiàn)發(fā)送數(shù)據(jù)-創(chuàng)新互聯(lián)
URL鏈接:http://chinadenli.net/article6/ceodog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊(cè)、面包屑導(dǎo)航、微信公眾號(hào)、外貿(mào)網(wǎng)站建設(shè)、響應(yīng)式網(wǎng)站、網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)