Python 2.7 通过Python在没有任何外部模块的情况下欺骗IP地址

Python 2.7 通过Python在没有任何外部模块的情况下欺骗IP地址,python-2.7,ip,ip-address,spoofing,Python 2.7,Ip,Ip Address,Spoofing,我想问,如何欺骗IP地址,但没有任何外部模块?到目前为止,我一直在使用Scapy模块,但我想自己制作,看看它是如何完成的,也许还能学到一些新东西。以下是Scapy模块的代码: from scapy.all import * SPOOFED_PACKET = IP(src=SRC_IP, dst=DST_IP) / TCP(sport=SRC_PORT, dport=DST_PORT) / PAYLOAD send(SPOOFED_PACKET) 要包含IPv4标头,请执行以下操作: """D

我想问,如何欺骗IP地址,但没有任何外部模块?到目前为止,我一直在使用Scapy模块,但我想自己制作,看看它是如何完成的,也许还能学到一些新东西。以下是Scapy模块的代码:

from scapy.all import *

SPOOFED_PACKET = IP(src=SRC_IP, dst=DST_IP) / TCP(sport=SRC_PORT, dport=DST_PORT) / PAYLOAD
send(SPOOFED_PACKET)
要包含IPv4标头,请执行以下操作:

"""Demonstrates how to construct and send raw Ethernet packets on the
network.

You probably need root privs to be able to bind to the network interface,
e.g.:

    $ sudo python sendeth.py
"""

from socket import *

def sendeth(ethernet_packet, payload, interface = "eth0"):
  """Send raw Ethernet packet on interface."""
  s = socket(AF_PACKET, SOCK_RAW)

  # From the docs: "For raw packet
  # sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])"
  s.bind((interface, 0))
  return s.send(ethernet_packet + payload)

def pack(byte_sequence):
  """Convert list of bytes to byte string."""
  return b"".join(map(chr, byte_sequence))

if __name__ == "__main__":
      # Note that this example contains HARDCODED packets, meaning that
      # it will ONLY work on the system it was designed for.

      # I got these values by sending a ping while running Wireshark.
      # You can do so yourself.  Another way to construct these manually is to use
      # the impacket library (sudo pip install impacket)

      # src=fe:ed:fa:ce:be:ef, dst=52:54:00:12:35:02, type=0x0800 (IP)
      ethernet_packet = [0x52, 0x54, 0x00, 0x12, 0x35, 0x02, 0xfe, 0xed, 0xfa,
                         0xce, 0xbe, 0xef, 0x08, 0x00]

      # src=10.0.2.15, dst=195.88.54.16 (vg.no), checksum, etc.
      ipv4_header = [0x45, 0x00, 0x00, 0x54, 0x05, 0x9f, 0x40, 0x00, 0x40, 0x01,
                     0x2f, 0x93, 0x0a, 0x00, 0x02, 0x0f, 0xc3, 0x58, 0x36, 0x10]

      # echo (ping) request, checksum 2b45, etc
      icmp_ping = [0x08, 0x00, 0x2b, 0x45, 0x11, 0x22, 0x00, 0x02, 0xa9, 0xf4, 0x5c,
                   0x53, 0x00, 0x00, 0x00, 0x00, 0xf5, 0x7b, 0x01, 0x00, 0x00, 0x00,
                   0x00, 0x00, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
                   0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
                   0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e,
                   0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37]

      payload = "".join(map(chr, ipv4_header + icmp_ping))

      # Construct Ethernet packet with an IPv4 ICMP PING request as payload
      r = sendeth(pack(ethernet_packet),
                  pack(ipv4_header + icmp_ping))
      print("Sent Ethernet w/IPv4 ICMP PING payload of length %d bytes" % r)

您可以从头开始构建数据包,以正确的顺序用正确的字节填充字节数组。然后将其发送到原始套接字。请看:关于此代码,我有三个问题。(1) 我应该在变量
eth\u type
中插入什么?(2) 这是什么?
print(“在eth0上发送%d字节以太网数据包”%sendeth(“\xFE\xED\xFA\xCE\xBE\xEF”、“\xFE\xED\xFA\xCE\xBE\xEF”、“\x7A\x05”、“你好”)
?(3) 变量
src
dst
是否应该是包含
(IP,PORT)
的元组?
eth_type
应该是与正确的EtherType()对应的rax十六进制。
print
语句同时发送和打印以太网帧。和
src
dst
是MAC地址,因为这是以太网层,而不是IP层,属于
eth_类型
,但我刚刚意识到,直到现在我还是有点误解了
以太网帧
,所以现在我知道,我正在寻找更改
有效负载中的
SRC_IP
DST_IP
。你能告诉我怎么做吗?谢谢你的回答,我添加了一个IP示例。您需要添加一个IP头以及您要查找的任何内容。
"""Demonstrates how to construct and send raw Ethernet packets on the
network.

You probably need root privs to be able to bind to the network interface,
e.g.:

    $ sudo python sendeth.py
"""

from socket import *

def sendeth(ethernet_packet, payload, interface = "eth0"):
  """Send raw Ethernet packet on interface."""
  s = socket(AF_PACKET, SOCK_RAW)

  # From the docs: "For raw packet
  # sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])"
  s.bind((interface, 0))
  return s.send(ethernet_packet + payload)

def pack(byte_sequence):
  """Convert list of bytes to byte string."""
  return b"".join(map(chr, byte_sequence))

if __name__ == "__main__":
      # Note that this example contains HARDCODED packets, meaning that
      # it will ONLY work on the system it was designed for.

      # I got these values by sending a ping while running Wireshark.
      # You can do so yourself.  Another way to construct these manually is to use
      # the impacket library (sudo pip install impacket)

      # src=fe:ed:fa:ce:be:ef, dst=52:54:00:12:35:02, type=0x0800 (IP)
      ethernet_packet = [0x52, 0x54, 0x00, 0x12, 0x35, 0x02, 0xfe, 0xed, 0xfa,
                         0xce, 0xbe, 0xef, 0x08, 0x00]

      # src=10.0.2.15, dst=195.88.54.16 (vg.no), checksum, etc.
      ipv4_header = [0x45, 0x00, 0x00, 0x54, 0x05, 0x9f, 0x40, 0x00, 0x40, 0x01,
                     0x2f, 0x93, 0x0a, 0x00, 0x02, 0x0f, 0xc3, 0x58, 0x36, 0x10]

      # echo (ping) request, checksum 2b45, etc
      icmp_ping = [0x08, 0x00, 0x2b, 0x45, 0x11, 0x22, 0x00, 0x02, 0xa9, 0xf4, 0x5c,
                   0x53, 0x00, 0x00, 0x00, 0x00, 0xf5, 0x7b, 0x01, 0x00, 0x00, 0x00,
                   0x00, 0x00, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
                   0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
                   0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e,
                   0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37]

      payload = "".join(map(chr, ipv4_header + icmp_ping))

      # Construct Ethernet packet with an IPv4 ICMP PING request as payload
      r = sendeth(pack(ethernet_packet),
                  pack(ipv4_header + icmp_ping))
      print("Sent Ethernet w/IPv4 ICMP PING payload of length %d bytes" % r)