简体   繁体   English

使用原始套接字制作 TCP 数据包

[英]Making TCP packet using raw socket

I'm trying to make a SYN packet using a RAW socket.我正在尝试使用 RAW 套接字制作 SYN 数据包。 But every time I get this error.但每次我收到这个错误。 I tried many ways to solve this error but haven't got a solution.我尝试了很多方法来解决这个错误,但还没有找到解决方案。

server.sendto(packet, (dst_ip, 0)) OSError: [WinError 10022] An invalid argument was supplied server.sendto(packet, (dst_ip, 0)) OSError: [WinError 10022] 提供了无效参数

This is my complete code.这是我的完整代码。

import socket
import struct


def checksum_calculator(header, size):

    checksum = 0
    pointer = 0

    while size > 1:
        checksum += int((str("%02x" % (header[pointer],)) +
                      str("%02x" % (header[pointer+1],))), 16)
        size -= 2
        pointer += 2

    if size: 
        checksum += header[pointer]

    checksum = (checksum >> 16) + (checksum & 0xffff)
    checksum += (checksum >> 16)

    return (~checksum) & 0xFFFF


# Create a raw socket.
server = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP)

# Source IP and desination IP.
src_ip = socket.gethostbyname(socket.gethostname())
dst_ip = socket.gethostbyname('www.google.com')

# Let's create TCP header.
src_port = 4790
dst_port = 80
seq = 0
ack_numb = 0
doff = 80
placeholder = 0
protocol = socket.IPPROTO_TCP
tcp_len = 20

# TCP flags.
fin = 0
syn = 1
rst = 0
psh = 0
ack = 0
urg = 0

window = socket.htons(5840)
checksum = 0
urg_ptr = 0

tcp_flags = fin + (syn << 1) + (rst << 2) + \
    (psh << 3) + (ack << 4) + (urg << 5)

tcp_header = struct.pack('!4s4sBBHHHLLBBHHH', socket.inet_aton(src_ip), socket.inet_aton(
    dst_ip), placeholder, protocol, tcp_len, src_port, dst_port, seq, ack_numb, doff, tcp_flags, window, checksum, urg_ptr)

# For calculate real checksum value.
tcp_checksum = checksum_calculator(tcp_header, len(tcp_header))

packet = struct.pack('!HHLLBBHHH', src_port, dst_port, seq,
                     ack_numb, doff, tcp_flags, window, tcp_checksum, urg_ptr)

server.sendto(packet, (dst_ip, 0))

Please help me to solve this error or give me some resources to read more about this.请帮我解决这个错误,或者给我一些资源来阅读更多相关信息。

The problem is that the destination address must be an IP address in dotted decimal notation.问题是目标地址必须是点分十进制表示法中的 IP 地址。 You are passing it as a host name which will not work.您将其作为主机名传递,这将不起作用。 Try changing your code like so:尝试像这样更改您的代码:

dst_ip = socket.gethostbyname('www.google.com')

to this:对此:

dst_ip = socket.gethostbyname('74.125.228.18')

That should fix the issue for you.那应该可以为您解决问题。 If that doesn't work then try one of these two options:如果这不起作用,请尝试以下两个选项之一:

Change the destination port number from 80 to another unused port such as 22.将目标端口号从 80 更改为另一个未使用的端口,例如 22。

Change the source port number from 4790 to something else.将源端口号从 4790 更改为其他内容。 It seems like there might be a conflict with port 4790 somewhere along the line.看起来可能与沿线某处的端口 4790 发生冲突。

Hopefully those suggestions will resolve your issue.希望这些建议能解决您的问题。 Good luck!祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM