-
Notifications
You must be signed in to change notification settings - Fork 0
/
private-client-knock.py
69 lines (55 loc) · 2.59 KB
/
private-client-knock.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
import sys
import argparse
import time
import socket
import select
class Knocker(object):
def __init__(self, args: list):
self._parse_args(args)
def _parse_args(self, args: list):
parser = argparse.ArgumentParser(description='Simple port-knocking client written in python3. '
'See more at https://github.com/grongor/knock')
parser.add_argument('-t', '--timeout', type=int, default=200,
help='How many milliseconds to wait on hanging connection. Default is 200 ms.')
parser.add_argument('-d', '--delay', type=int, default=200,
help='How many milliseconds to wait between each knock. Default is 200 ms.')
parser.add_argument('-u', '--udp', help='Use UDP instead of TCP by default.', action='store_true')
parser.add_argument('host', help='Hostname or IP address of the host to knock on. Supports IPv6.')
parser.add_argument('ports', metavar='port[:protocol]', nargs='+',
help='Port(s) to knock on, protocol (tcp, udp) is optional.')
args = parser.parse_args(args)
self.timeout = args.timeout / 1000
self.delay = args.delay / 1000
self.default_udp = args.udp
self.ports = args.ports
self.address_family, _, _, _, (self.ip_address, _) = socket.getaddrinfo(
host=args.host,
port=None,
flags=socket.AI_ADDRCONFIG
)[0]
def knock(self):
last_index = len(self.ports) - 1
for i, port in enumerate(self.ports):
use_udp = self.default_udp
if port.find(':') != -1:
port, protocol = port.split(':', 2)
if protocol == 'tcp':
use_udp = False
elif protocol == 'udp':
use_udp = True
else:
error = 'Invalid protocol "{}" given. Allowed values are "tcp" and "udp".'
raise ValueError(error.format(protocol))
s = socket.socket(self.address_family, socket.SOCK_DGRAM if use_udp else socket.SOCK_STREAM)
s.setblocking(False)
socket_address = (self.ip_address, int(port))
if use_udp:
s.sendto(b'', socket_address)
else:
s.connect_ex(socket_address)
select.select([s], [s], [s], self.timeout)
s.close()
if self.delay and i != last_index:
time.sleep(self.delay)
if __name__ == '__main__':
Knocker(sys.argv[1:]).knock()