基于python模擬TCP3次握手連接及發(fā)送數(shù)據(jù)
更新時間:2020年11月06日 16:17:15 作者:掙扎的豬
這篇文章主要介紹了基于python模擬TCP3次握手連接及發(fā)送數(shù)據(jù),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
源碼如下
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 #源隨機端口
s_seq = ans[TCP].ack #源序列號(其實初始值已經(jīng)被服務(wù)端加1)
d_seq = ans[TCP].seq + 1 #確認號,需要把服務(wù)端的序列號加1
#第三次握手,發(fā)送ACK確認包
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 '[-]有錯誤,請注意檢查!'
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請求
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)
運行結(jié)果如下
# python exp3.py <meta http-equiv="Pragma" content="no-cache"> <meta http-equiv="Expires" content="wed, 26 Feb 1997 08:21:57 GMT"> <html><head><title>505 HTTP Version not supported</title></head><body><center><h1>505 HTTP Version not supported</h1></center></body></html>�p�-1���-1��2��2��D��o�p�-1��`��D
wireshark抓包截圖如下:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
wxPython中wx.gird.Gird添加按鈕的實現(xiàn)
本文主要介紹了wxPython中wx.gird.Gird添加按鈕的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
pycharm?使用conda虛擬環(huán)境的詳細配置過程
這篇文章主要介紹了pycharm?使用conda虛擬環(huán)境,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03

