简体   繁体   English

在python中使用原始套接字发送IP数据包

[英]Sending IP packet with raw socket in python

To summarize my problems:总结一下我的问题:
I have 2 problems:我有两个问题:

  1. I can't send IP_PACKET variable我无法发送 IP_PACKET 变量
  2. How do I set the flag in IP packet?如何在 IP 数据包中设置标志? (for send) (用于发送)

This is my code:这是我的代码:

#! /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])

Python version: 2.7 Python版本:2.7
Operation system: Ubuntu 16.04操作系统:Ubuntu 16.04
Thank you for helping me.感谢你们对我的帮助。

Your scoping is wrong, the function is defined inside some other function so it is only locally available:您的范围是错误的,该函数是在其他一些函数中定义的,因此它仅在本地可用:

def f1():
    pass

def f2():
    def f3():
        pass 
    f3() # works, local scope visible

f1() # works
f2() # works
try:    
    f3() # does not work, is not known, only inde f2()'s scope known
except NameError as ne:
    print(ne)

Output:输出:

name 'f3' is not defined

To fix your structure, make IP_HEADER(Source_IP, Destination_IP): return the constructed IP_PACKET ,store it into a variable and provide it to Send_PACKET(IFACE,data) - also unindent Send_PACKET(...) :要修复您的结构,请制作IP_HEADER(Source_IP, Destination_IP):返回构造的IP_PACKET ,将其存储到变量中并将其提供给Send_PACKET(IFACE,data) - 也未缩进Send_PACKET(...)

def IP_HEADER(Source_IP, Destination_IP):
    # your code
    return IP_PACKET

def Send_PACKET(IFACE,data):  # unindent 4 spaces so its in normal scope
    # your code, use data instead of IP_PACKET

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

The solution of the first problem is:第一个问题的解决方法是:

global IP_PACKET
IP_PACKET =[]

在此处输入图片说明

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

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