-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathfindneighbors.py
executable file
·177 lines (141 loc) · 5.62 KB
/
findneighbors.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
#!/usr/bin/env python
from sploitego.maltego.message import Netblock, Label, Field, IPv4Address, MaltegoException
from scapy.all import arping, sr, sr1, TCP, IP, ICMP, sniff, ARP
from sploitego.scapytools.route import route, traceroute2
from sploitego.iptools.ip import IPNetwork, IPAddress
from sploitego.framework import configure, superuser
from sploitego.xmltools.objectify import objectify
from sploitego.iptools.arin import whoisip
from sploitego.maltego.utils import debug
from sploitego.config import config
__author__ = 'Nadeem Douba'
__copyright__ = 'Copyright 2012, Cygnos Corporation'
__credits__ = ['Nadeem Douba']
__license__ = 'GPL'
__version__ = '0.1'
__maintainer__ = 'Nadeem Douba'
__email__ = '[email protected]'
__status__ = 'Development'
__all__ = [
'dotransform'
]
@superuser
@configure(
label='To Neighbors [Active Scan]',
description='This transform attempts to identify hosts that are directly attached to the same router as the target',
uuids=[ 'sploitego.v2.IPv4AddressToNeighbors_ActiveScan' ],
inputs=[ ( "Reconnaissance", IPv4Address ) ],
)
def dotransform(request, response):
r = route(request.value)
if r is None:
raise MaltegoException('Network is unavailable')
elif not r['nexthop']:
return findlocalneighbors(r['network'], response)
return findremoteneighbors(IPAddress(request.value), response)
def findlocalneighbors(network, response):
debug('ARP sweeping %s' % network.netblock)
e = Netblock(network.netblock)
e += Label('CIDR Notation', repr(network))
e += Label('Network Mask', network.netmask)
e += Label('Number of Hosts', int(~network.netmask) - 1)
response += e
ans = arping(
repr(network),
timeout=config['scapy/sr_timeout'],
verbose=config['scapy/sr_verbose']
)[0]
for i in ans:
e = IPv4Address(i[1].psrc)
e.internal = True
e += Field('ethernet.hwaddr', i[1].hwsrc, displayname='Hardware Address')
response += e
if len(ans) <= 1:
passivescan(network, response)
return response
def passivescan(network, response):
nodes = {}
debug('Sniffing network traffic for more hosts.')
ans = sniff(count=config['scapy/sniffcount'], timeout=config['scapy/snifftimeout'])
debug('Analyzing traffic.')
for i in ans:
src = None
dst = None
if IP in i:
src = i[IP].src
dst = i[IP].dst
elif ARP in i:
src = i[ARP].psrc
dst = i[ARP].pdst
else:
continue
if src in network and src not in nodes:
nodes[src] = True
e = IPv4Address(src, internal=True)
e += Field('ethernet.hwaddr', i.src, displayname='Hardware Address')
response += e
if dst in network and dst not in nodes and i.dst != 'ff:ff:ff:ff:ff:ff':
nodes[dst] = True
e = IPv4Address(dst, internal=True)
e += Field('ethernet.hwaddr', i.dst, displayname='Hardware Address')
response += e
def findremoteneighbors(ip, response):
debug('Doing an ARIN whois lookup...')
w = objectify(whoisip(ip, accept='application/xml'))
network = IPNetwork([w.startAddress, w.endAddress])
e = Netblock(network.netblock)
e += Label('CIDR Notation', repr(network))
e += Label('Network Mask', network.netmask)
e += Label('Number of Hosts', int(~network.netmask) - 1)
response += e
if network.cidrlen < 24:
debug('According to ARIN, the CIDR length is %d, reducing it to 24 for the scan...' % network.cidrlen)
network.netblock = '%s/24' % ip
debug('Probing the host on TCP ports 0-1024...')
r = sr1(
IP(dst=str(ip))/TCP(dport=(0,1024)),
timeout=config['scapy/sr_timeout'],
verbose=config['scapy/sr_verbose'],
retry=config['scapy/sr_retries']
)
if r is not None and r.src == ip:
dport = r.sport
debug('Performing a traceroute to destination %s' % ip)
ans = traceroute2(
str(ip),
TCP(dport=dport),
timeout=config['scapy/sr_timeout'],
verbose=config['scapy/sr_verbose'],
retry=config['scapy/sr_retries']
)
l_hop = ans[-1]
sl_hop = ans[-2]
if sl_hop['ttl'] != l_hop['ttl'] - 1:
debug(
"It takes %d hops to get to %s but we could only find the router at hop %d (%s)." %
(l_hop['ttl'], ip, sl_hop['ttl'], sl_hop['ip'])
)
debug("Can't find second last hop... aborting...")
else:
debug('It takes %d hops to get to %s and it is attached to router %s...' % (l_hop['ttl'], ip, sl_hop['ip']))
debug('Sending probe packets to %s with ttl %d...' % (network, sl_hop['ttl']))
ans = sr(
IP(dst=repr(network), ttl=sl_hop['ttl'])/TCP(dport=dport),
timeout=config['scapy/sr_timeout'],
verbose=config['scapy/sr_verbose'],
retry=config['scapy/sr_retries']
)[0]
for r in ans:
if r[1].src == sl_hop['ip']:
debug('%s is attached to the same router...' % r[0].dst)
e = IPv4Address(r[0].dst)
alive = sr1(
IP(dst=r[0].dst)/TCP(dport=dport),
timeout=config['scapy/sr_timeout'],
verbose=config['scapy/sr_verbose'],
retry=config['scapy/sr_retries']
)
if alive is not None:
e += Field('alive', 'true')
response += e
return response