· 6 years ago · May 28, 2019, 04:33 PM
1#!/usr/bin/python3
2
3'''Ambit, by Oelin.
4
5Ambit is a simple tool which exposes open ports on network connected machines.
6
7Usage
8 ambit [hosts:ports]
9
10Format
11 Ambit uses a defined format for specifying targets. Here's some examples.
12
13
14
15 Check one host for one port.
16
17 ambit 192.168.1.1:22
18
19
20
21 Check two host for two ports, using commas.
22
23 ambit 192.168.1.1,192.168.1.2:22,80
24
25
26
27 Check a range of hosts for a range of ports.
28
29 ambit 192.168.1.1-192.168.1.100:100-300
30
31
32
33 You can combine any of these formats.
34
35 ambit 192.168.1.1,192.168.1.5-192.168.1.200:20-23,80,8000,8080,500-1000
36
37
38
39 You can also have multiple groups.
40
41 ambit 192.168.1.1-192.168.1.2:9100,515,80 96.60.22.10:80,443,20-23'''
42
43
44
45from socket import *
46from sys import argv
47
48
49
50def addresses(start, end):
51 addresses = []
52 address = start
53
54 while (address != end):
55 addresses.append(address)
56
57 octets = [int(octet) for octet in address.split('.')]
58 octets[-1] += 1
59
60 for index in range(3, 1, -1):
61 if (octets[index] > 255):
62 octets[index] = 0
63 octets[index - 1] += 1
64
65 octets = [str(octet) for octet in octets]
66 address = '.'.join(octets)
67
68 return addresses
69
70
71
72def hosts(segments):
73 segments = segments.split(',')
74 hosts = []
75
76 for segment in segments:
77 if ('-' in segment):
78 scope = segment.split('-')
79 start = scope[0]
80 end = scope[1]
81 hosts += addresses(start, end)
82
83 else:
84 hosts.append(segment)
85
86 return hosts
87
88
89
90def ports(segments):
91 segments = segments.split(',')
92 ports = []
93
94 for segment in segments:
95 if ('-' in segment):
96 scope = segment.split('-')
97 start = int(scope[0])
98 end = int(scope[1])
99 ports += range(start, end)
100
101 else:
102 ports.append(int(segment))
103
104 return ports
105
106
107
108def check(host, port):
109 _socket = socket()
110 _socket.settimeout(0.1)
111
112 try:
113 _socket.connect((host, port))
114 return True
115
116 except:
117 return False
118
119
120
121def examine(hosts, ports):
122 for host in hosts:
123 print('\n[+] Checking ports on %s' % host)
124
125 for port in ports:
126 open = check(host, port)
127
128 if (open):
129 print('[+] %s, open' % port)
130
131# else:
132# print('[-] %s, closed' % port)
133
134
135
136def main():
137 if (len(argv) == 2):
138 targets = argv[1].split(':')
139
140 _hosts = hosts(targets[0])
141
142 _ports = ports(targets[1])
143
144 print('\n[+] Starting examination')
145
146 examine(_hosts, _ports)
147
148 else:
149 print(__doc__)
150
151
152
153main()