send.py

!/usr/bin/env python

import argparse
import sys
import socket
import random
import struct

from scapy.all import sendp, send, get_if_list, get_if_hwaddr
from scapy.all import Packet
from scapy.all import Ether, IP, UDP, TCP

def get_if():
ifs=get_if_list()
iface=None # "h1-eth0"
for i in get_if_list():
if "eth0" in i:
iface=i
break;
if not iface:
print "Cannot find eth0 interface"
exit(1)
return iface

def main():

if len(sys.argv)<3:
    print 'pass 2 arguments: <destination> "<message>"'
    exit(1)

addr = socket.gethostbyname(sys.argv[1])
iface = get_if()

print "sending on interface %s to %s" % (iface, str(addr))
pkt =  Ether(src=get_if_hwaddr(iface), dst='ff:ff:ff:ff:ff:ff')
pkt = pkt /IP(dst=addr) / TCP(dport=1234, sport=random.randint(49152,65535)) / sys.argv[2]
pkt.show2()
sendp(pkt, iface=iface, verbose=False)

if name == 'main':
main()


!/usr/bin/env python

import argparse
import sys
import socket
import random
import struct

from scapy.all import sendp, send, get_if_list, get_if_hwaddr
from scapy.all import Packet
from scapy.all import Ether, IP, UDP, TCP

def get_if():
# scapy.interfaces.get_if_list()[source]
# Return a list of interface names
# get_if_list()
# ['lo', 'eth0']
ifs=get_if_list()
iface=None # "h1-eth0"
for i in get_if_list():
if "eth0" in i:
iface=i
break;
if not iface:
print "Cannot find eth0 interface"
exit(1)
return iface

def main():
#同Java:public static void main(String[] args) 中的 args,表示从命令行中输入的参数
# python send.py 后面必须附带两个参数,表示:目的地址 "信息内容"(给 谁 发 什么)
if len(sys.argv)❤️:
print 'pass 2 arguments: ""'
exit(1)
# 通过主机名获取 主机的IP地址
addr = socket.gethostbyname(sys.argv[1])#sys.argv[0] 是程序名,即:send.py;sys.argv[1]是第一个参数:
# 获取源主机的发送端口,从哪个端口把数据发出去
iface = get_if()

print "sending on interface %s to %s" % (iface, str(addr))
#通过端口获取源主机的MAC地址,
pkt =  Ether(src=get_if_hwaddr(iface), dst='ff:ff:ff:ff:ff:ff')
pkt = pkt /IP(dst=addr) / TCP(dport=1234, sport=random.randint(49152,65535)) / sys.argv[2]
pkt.show2()
sendp(pkt, iface=iface, verbose=False)

if name == 'main':
main()

posted @ 2021-12-16 11:03  没有任何出路  阅读(121)  评论(0)    收藏  举报