溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用python模擬TCP連接并實(shí)現(xiàn)發(fā)送數(shù)據(jù)

發(fā)布時(shí)間:2020-11-07 14:38:14 來源:億速云 閱讀:469 作者:Leah 欄目:開發(fā)技術(shù)

今天就跟大家聊聊有關(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   #源序列號(其實(shí)初始值已經(jīng)被服務(wù)端加1)
    d_seq = ans[TCP].seq + 1 #確認(rèn)號,需要把服務(wù)端的序列號加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 '[-]有錯誤,請注意檢查!'
    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)

運(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><h2>505 HTTP Version not supported</h2></center></body></html>&#65533;p&#65533;-1&#65533;&#65533;&#65533;-1&#65533;&#65533;2&#65533;&#65533;2&#65533;&#65533;D&#65533;&#65533;o&#65533;p&#65533;-1&#65533;&#65533;`&#65533;&#65533;D

wireshark抓包截圖如下:

使用python模擬TCP連接并實(shí)現(xiàn)發(fā)送數(shù)據(jù)

看完上述內(nèi)容,你們對使用python模擬TCP連接并實(shí)現(xiàn)發(fā)送數(shù)據(jù)有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI