-
Notifications
You must be signed in to change notification settings - Fork 101
/
knockknock.py
123 lines (93 loc) · 3.38 KB
/
knockknock.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
#!/usr/bin/env python
__author__ = "Moxie Marlinspike"
__email__ = "moxie@thoughtcrime.org"
__license__= """
Copyright (c) 2009 Moxie Marlinspike <moxie@thoughtcrime.org>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA
"""
import time, os, sys
import getopt
import subprocess
from struct import *
from knockknock.Profile import Profile
def usage():
print "Usage: knockknock.py -p <portToOpen> <host>"
sys.exit(2)
def parseArguments(argv):
try:
port = 0
host = ""
opts, args = getopt.getopt(argv, "h:p:")
for opt, arg in opts:
if opt in ("-p"):
port = arg
else:
usage()
if len(args) != 1:
usage()
else:
host = args[0]
except getopt.GetoptError:
usage()
if port == 0 or host == "":
usage()
return (port, host)
def getProfile(host):
homedir = os.path.expanduser('~')
if not os.path.isdir(homedir + '/.knockknock/'):
print "Error: you need to setup your profiles in " + homedir + '/.knockknock/'
sys.exit(2)
if not os.path.isdir(homedir + '/.knockknock/' + host):
print 'Error: profile for host ' + host + ' not found at ' + homedir + '/.knockknock/' + host
sys.exit(2)
return Profile(homedir + '/.knockknock/' + host)
def verifyPermissions():
if os.getuid() != 0:
print 'Sorry, you must be root to run this.'
sys.exit(2)
def existsInPath(command):
def isExe(fpath):
return os.path.exists(fpath) and os.access(fpath, os.X_OK)
for path in os.environ["PATH"].split(os.pathsep):
exeFile = os.path.join(path, command)
if isExe(exeFile):
return exeFile
return None
def main(argv):
(port, host) = parseArguments(argv)
verifyPermissions()
profile = getProfile(host)
port = pack('!H', int(port))
packetData = profile.encrypt(port)
knockPort = profile.getKnockPort()
(idField, seqField, ackField, winField) = unpack('!HIIH', packetData)
hping = existsInPath("hping3")
if hping is None:
print "Error, you must install hping3 first."
sys.exit(2)
command = [hping, "-S", "-c", "1",
"-p", str(knockPort),
"-N", str(idField),
"-w", str(winField),
"-M", str(seqField),
"-L", str(ackField),
host]
try:
subprocess.call(command, shell=False, stdout=open('/dev/null', 'w'), stderr=subprocess.STDOUT)
print 'Knock sent.'
except OSError:
print "Error: Do you have hping3 installed?"
sys.exit(3)
if __name__ == '__main__':
main(sys.argv[1:])