-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtestAp.py
212 lines (177 loc) · 6.62 KB
/
testAp.py
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env python
# coding=utf-8
# code by 92ez.com
import scapy.all as scapy
import subprocess
import threading
import socket
import signal
import struct
import fcntl
import time
import sys
import os
DN = open(os.devnull, 'w')
def get_isc_dhcp_server():
if not os.path.isfile('/usr/sbin/dhcpd'):
install = raw_input('[*] isc-dhcp-server not found in /usr/sbin/dhcpd, install now? [y/n] ')
if install == 'y':
os.system('apt-get -y install isc-dhcp-server')
else:
sys.exit('[*] isc-dhcp-server not found in /usr/sbin/dhcpd')
def iwconfig():
monitors = []
interfaces = {}
proc = subprocess.Popen(['iwconfig'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
for line in proc.communicate()[0].split('\n'):
if len(line) == 0: continue # Isn't an empty string
if line[0] != ' ': # Doesn't start with space
#ignore_iface = re.search('eth[0-9]|em[0-9]|p[1-9]p[1-9]|at[0-9]', line)
#if not ignore_iface: # Isn't wired or at0 tunnel
iface = line[:line.find(' ')] # is the interface name
if 'Mode:Monitor' in line:
monitors.append(iface)
elif 'IEEE 802.11' in line:
if "ESSID:\"" in line:
interfaces[iface] = 1
else:
interfaces[iface] = 0
return monitors, interfaces
def rm_mon():
monitors, interfaces = iwconfig()
for m in monitors:
if 'mon' in m:
subprocess.Popen(['airmon-ng', 'stop', m], stdout=DN, stderr=DN)
else:
subprocess.Popen(['ifconfig', m, 'down'], stdout=DN, stderr=DN)
subprocess.Popen(['iw', 'dev', m, 'mode', 'managed'], stdout=DN, stderr=DN)
subprocess.Popen(['ifconfig', m, 'up'], stdout=DN, stderr=DN)
def internet_info(interfaces):
'''return the internet connected iface'''
inet_iface = None
proc = subprocess.Popen(['/sbin/ip', 'route'], stdout=subprocess.PIPE, stderr=DN)
def_route = proc.communicate()[0].split('\n')#[0].split()
for line in def_route:
if 'default via' in line:
line = line.split()
inet_iface = line[4]
ipprefix = line[2][:2] # Just checking if it's 192, 172, or 10
if inet_iface:
return inet_iface, ipprefix
else:
sys.exit('[*] No active internet connection found. Exiting')
def AP_iface(interfaces, inet_iface):
useable_iface = []
for i in interfaces:
if i != inet_iface:
useable_iface.append(i)
return useable_iface
def iptables(inet_iface):
os.system('iptables -X')
os.system('iptables -F')
os.system('iptables -t nat -F')
os.system('iptables -t nat -X')
os.system('iptables -t nat -A POSTROUTING -o %s -j MASQUERADE' % inet_iface)
os.system('echo 1 > /proc/sys/net/ipv4/ip_forward')
def start_monitor(ap_iface, channel):
proc = subprocess.Popen(['airmon-ng', 'start', ap_iface, channel], stdout=subprocess.PIPE, stderr=DN)
# todo: cleanup
proc_lines = proc.communicate()[0].split('\n')
# Old airmon-ng
for line in proc_lines:
if "monitor mode vif enabled" in line:
line = line.split()
mon_iface = line[6].split(']')[1]
return mon_iface
sys.exit('[-] Monitor mode not found. Paste output of `airmon-ng start [interface]` to github issues\n'
'https://github.com/DanMcInerney/fakeAP/issues')
def get_mon_mac(mon_iface):
'''http://stackoverflow.com/questions/159137/getting-mac-address'''
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', mon_iface[:15]))
mac = ''.join(['%02x:' % ord(char) for char in info[18:24]])[:-1]
return mac
def start_ap(mon_iface, channel, essid,key):
print '[*] Starting the fake access point...'
subprocess.Popen(['airbase-ng', '-P', '-c', channel, '-e', essid,'-w',key, mon_iface], stdout=DN, stderr=DN)
print '[*] Waiting for 6 seconds...'
time.sleep(6)
subprocess.Popen(['ifconfig', 'at0', 'up', '10.0.0.1', 'netmask', '255.255.255.0'], stdout=DN, stderr=DN)
subprocess.Popen(['ifconfig', 'at0', 'mtu', '1400'], stdout=DN, stderr=DN)
def cleanup(signal, frame):
os.system('echo 0 > /proc/sys/net/ipv4/ip_forward')
os.system('iptables -F')
os.system('iptables -X')
os.system('iptables -t nat -F')
os.system('iptables -t nat -X')
os.system('pkill airbase-ng')
os.system('pkill dhcpd')
rm_mon()
sys.exit('\n[+] Cleaned up')
def dhcp_conf(ipprefix):
config = ('default-lease-time 300;\n'
'max-lease-time 360;\n'
'ddns-update-style none;\n'
'authoritative;\n'
'log-facility local7;\n'
'subnet %s netmask 255.255.255.0 {\n'
'range %s;\n'
'option routers %s;\n'
'option domain-name-servers %s;\n'
'}')
if ipprefix == '19' or ipprefix == '17':
with open('/tmp/dhcpd.conf', 'w') as dhcpconf:
# subnet, range, router, dns
dhcpconf.write(config % ('10.0.0.0', '10.0.0.2 10.0.0.100', '10.0.0.1', '114.114.114.114'))
elif ipprefix == '10':
with open('/tmp/dhcpd.conf', 'w') as dhcpconf:
dhcpconf.write(config % ('172.16.0.0', '172.16.0.2 172.16.0.100', '172.16.0.1', '114.114.114.114'))
return '/tmp/dhcpd.conf'
def dhcp(dhcpconf, ipprefix):
os.system('echo > /var/lib/dhcp/dhcpd.leases')
dhcp = subprocess.Popen(['dhcpd', '-cf', dhcpconf], stdout=subprocess.PIPE, stderr=DN)
if ipprefix == '19' or ipprefix == '17':
os.system('route add -net 10.0.0.0 netmask 255.255.255.0 gw 10.0.0.1')
else:
os.system('route add -net 172.16.0.0 netmask 255.255.255.0 gw 172.16.0.1')
if __name__ == "__main__":
# check if root user
if os.geteuid() != 0:
sys.exit('[Error!!!] You must run this script as root')
# check dhcpd service
get_isc_dhcp_server()
# check if mon interface
monitors, interfaces = iwconfig()
# rm mon interface
rm_mon()
# get active internet interface
inet_iface, ipprefix = internet_info(interfaces)
# get useable ap_iface
ap_iface = AP_iface(interfaces, inet_iface)
# can not find any useable ap_iface
if not ap_iface:
sys.exit('[*] Found internet connected interface in '+inet_iface+'. Please bring up a wireless interface to use as the fake access point.')
# set iptables firewall
ipf = iptables(inet_iface)
print '[*] Cleared leases, started DHCP, set up iptables'
userSetAp = ap_iface[0]
userSetChannel = '6'
fadeESSID = '测试阿斯达收到'
usersetkey = '1234567890'
# get monitor iface
mon_iface = start_monitor(userSetAp,userSetChannel)
# get monitor mac
mon_mac1 = get_mon_mac(mon_iface)
# start
start_ap(mon_iface, userSetChannel, fadeESSID,usersetkey)
dhcpconf = dhcp_conf(ipprefix)
dhcp(dhcpconf, ipprefix)
print '[*] '+fadeESSID+' set up on channel '+userSetChannel+' via '+mon_iface+' on '+userSetAp
subprocess.Popen(['xterm','-e','python',sys.path[0]+'/sniffer.py'], stdout=DN, stderr=DN)
while 1:
signal.signal(signal.SIGINT, cleanup)
os.system('clear')
proc = subprocess.Popen(['cat', '/var/lib/dhcp/dhcpd.leases'], stdout=subprocess.PIPE, stderr=DN)
for line in proc.communicate()[0].split('\n'):
print line
time.sleep(1)