在python错误中发送ip raw包

时间:2018-04-29 17:20:47

标签: python sockets networking raw-sockets

为什么我无法发送数据包?

我想发送原始ip数据包

用法:python ip.py {Source-IP} {Destination-IP} {IFACE}

这是我的代码:

#! /usr/bin/python
import socket
import struct
import sys


def Create_SOCKET():
    global ST
    try:
        ST = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW)
    except socket.error as MSG:
        print "[ERROR] Socket creation error:\n", MSG
        sys.exit()


def IP_HEADER(Source_IP, Destination_IP):
    global IP_PACKET

    #Variable validation
    IP_VER = 4
    IP_IHL = 5
    IP_TOS = 0
    IP_TOL = 60
    IP_IDT = 54331
    IP_FLG = 2 # 0x02 DF
    IP_FOS = 0
    IP_TTL = 64
    IP_PROTO = socket.IPPROTO_TCP
    IP_CHECKSUM = 0

    IP_IHL_VER = (IP_VER << 4) + IP_IHL

    SRC_IP = socket.inet_aton(Source_IP)
    DST_IP = socket.inet_aton(Destination_IP)

    IP_PACKET = struct.pack("!BBHHHBBH4s4s", IP_IHL_VER, IP_TOS, IP_TOL, IP_IDT, IP_FOS, IP_TTL, IP_PROTO, IP_CHECKSUM, SRC_IP, DST_IP)



def Send_PACKET(IFACE):
    try:
        Create_SOCKET()
        ST.bind((IFACE, 0))
        ST.send(IP_PACKET)
        print "[SUCESS] Packet was sent."
    except socket.error as MSG:
        print "[ERROR] Packet can't send:\n", MSG
        sys.exit()


IP_HEADER(sys.argv[1], sys.argv[2])
Send_PACKET(sys.argv[3])

这是输出:

Traceback (most recent call last):
  File "ip.py", line 52, in <module>
    Send_PACKET(sys.argv[3])
  File "ip.py", line 43, in Send_PACKET
    ST.bind((IFACE, 0))
  File "/usr/lib/python2.7/socket.py", line 228, in meth
    return getattr(self._sock,name)(*args)
socket.gaierror: [Errno -2] Name or service not known

如何解决?

我认为这个错误来自绑定功能端口。

我可以用这个堡垒发送它:

ST.sendto(IP_PACKET, ("192.168.1.100", 0))

为什么我不能使用send()函数?

感谢您的帮助//

0 个答案:

没有答案