gpt4 book ai didi

python - 为什么我的 TCP 数据包看起来不像协议(protocol)分析器的 TCP 数据包?

转载 作者:可可西里 更新时间:2023-11-01 02:33:57 25 4
gpt4 key购买 nike

我正在尝试使用原始套接字向服务器发送 GET 请求。我正在使用原始套接字,因此我可以编辑数据包窗口大小。这是我的代码。

import socket, sys
from struct import *

def checksum(msg):
s = 0

for i in range(0, len(msg), 2):
w = ord(msg[i]) + (ord(msg[i+1]) << 8 )
s = s + w

s = (s>>16) + (s & 0xffff);
s = s + (s >> 16);

s = ~s & 0xffff

return s

#create a raw socket
try:
s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW)
except socket.error , msg:
print 'Socket could not be created. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
sys.exit()

# tell kernel not to put in headers, since we are providing it, when using IPPROTO_RAW this is not necessary
# s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)


packet = '';

source_ip = '192.168.2.7'
dest_ip = '216.86.145.50' # or socket.gethostbyname('www.google.com')

# ip header fields
ip_ihl = 5
ip_ver = 4
ip_tos = 0
ip_tot_len = 0 # kernel will fill the correct total length
ip_id = 54321 #Id of this packet
ip_frag_off = 0
ip_ttl = 255
ip_proto = socket.IPPROTO_TCP
ip_check = 0 # kernel will fill the correct checksum
ip_saddr = socket.inet_aton ( source_ip ) #Spoof the source ip address if you want to
ip_daddr = socket.inet_aton ( dest_ip )

ip_ihl_ver = (ip_ver << 4) + ip_ihl

ip_header = pack('!BBHHHBBH4s4s' , ip_ihl_ver, ip_tos, ip_tot_len, ip_id, ip_frag_off, ip_ttl, ip_proto, ip_check, ip_saddr, ip_daddr)

# tcp header fields
tcp_source = 1234 # source port
tcp_dest = 80 # destination port
tcp_seq = 454
tcp_ack_seq = 0
tcp_doff = 5
#tcp flags
tcp_fin = 0
tcp_syn = 1
tcp_rst = 0
tcp_psh = 0
tcp_ack = 0
tcp_urg = 0
tcp_window = socket.htons (2048)
tcp_check = 0
tcp_urg_ptr = 0

tcp_offset_res = (tcp_doff << 4) + 0
tcp_flags = tcp_fin + (tcp_syn << 1) + (tcp_rst << 2) + (tcp_psh <<3) + (tcp_ack << 4) + (tcp_urg << 5)


tcp_header = pack('!HHLLBBHHH' , tcp_source, tcp_dest, tcp_seq, tcp_ack_seq, tcp_offset_res, tcp_flags, tcp_window, tcp_check, tcp_urg_ptr)

user_data = "GET / HTTP/1.1\r\nHost:www.example.com\n\nUser-Agent:Mozilla 5.0\r\n\r\n"


source_address = socket.inet_aton( source_ip )
dest_address = socket.inet_aton(dest_ip)
placeholder = 0
protocol = socket.IPPROTO_TCP
tcp_length = len(tcp_header) + len(user_data)

psh = pack('!4s4sBBH' , source_address , dest_address , placeholder , protocol , tcp_length);
psh = psh + tcp_header + user_data;

tcp_check = checksum(psh)



tcp_header = pack('!HHLLBBH' , tcp_source, tcp_dest, tcp_seq, tcp_ack_seq, tcp_offset_res, tcp_flags, tcp_window) + pack('H' , tcp_check) + pack('!H' , tcp_urg_ptr)


packet = ip_header + tcp_header + user_data


s.sendto(packet, (dest_ip , 0 ))

我正在使用 Wireshark 查看正在发送的数据包。问题是输出不是 TCP GET 请求数据包。在 wire shark 中,输出是白色的,根据协议(protocol),它显示 IPv4,信息显示:144 Unknown(255)。我该如何解决这个问题?

最佳答案

就像@Sivir 说的,你需要建立一个三向握手,因为你想使用 TCP 协议(protocol)。理论上握手应该是这样的

  • YourProgram:发送一个SYN包
  • Server:发送SYNACK包
  • YourProgram:发送一个ACK包

可以找到有关此的更多信息 here .

关于python - 为什么我的 TCP 数据包看起来不像协议(protocol)分析器的 TCP 数据包?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33773668/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com