基于python模擬TCP3次握手連接及發(fā)送數(shù)據(jù)
更新時(shí)間:2020年11月06日 16:17:15 作者:掙扎的豬
這篇文章主要介紹了基于python模擬TCP3次握手連接及發(fā)送數(shù)據(jù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
源碼如下
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ā)來(lái)的數(shù)據(jù)
rcv = ans[Raw]
print rcv
if __name__ == '__main__':
#start_tcp(target_ip,target_port)
trans_data(target_ip,target_port,data)
運(yùn)行結(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抓包截圖如下:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python 如何做一個(gè)識(shí)別率百分百的OCR
最近在做游戲自動(dòng)化(測(cè)試),也就是游戲腳本了。主要有以下幾個(gè)需求識(shí)別率百分百、速度要快、模型要小,本文就來(lái)著手實(shí)現(xiàn)它2021-05-05
Python之Scrapy爬蟲(chóng)框架安裝及使用詳解
這篇文章主要為大家詳細(xì)介紹了Python Scrapy爬蟲(chóng)框架安裝及簡(jiǎn)單使用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
python字典翻轉(zhuǎn)的實(shí)現(xiàn)
本文主要介紹了python字典翻轉(zhuǎn)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05
wxPython中wx.gird.Gird添加按鈕的實(shí)現(xiàn)
本文主要介紹了wxPython中wx.gird.Gird添加按鈕的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
pycharm?使用conda虛擬環(huán)境的詳細(xì)配置過(guò)程
這篇文章主要介紹了pycharm?使用conda虛擬環(huán)境,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03

