This repository has been archived by the owner on Jan 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 71
/
honsshInteraction.py
executable file
·281 lines (239 loc) · 9.08 KB
/
honsshInteraction.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/usr/bin/env python
# Copyright (c) 2016 Thomas Nicholson <tnnich@googlemail.com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. The names of the author(s) may not be used to endorse or promote
# products derived from this software without specific prior written
# permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
from twisted.internet import reactor, protocol, threads
import json
import sys
import time
import base64
import argparse
import re
import datetime
import tty
import termios
class HonsshProtocol(protocol.Protocol):
lost = False
def getch(self):
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
def connectionMade(self):
if self.factory.command == 'list':
self.cmdList()
elif self.factory.command == 'view':
self.cmdView()
elif self.factory.command == 'disconnect':
self.cmdDisconnect()
def connectionLost(self, message):
self.lost = True
if not self.factory.command == 'list':
print '\rConnection lost\n\r'
try:
reactor.stop()
except:
pass
def cmdList(self):
self.sendData({'command': 'list'})
def cmdView(self):
self.sendData({'command': 'view', 'uuid': self.factory.uuid})
escCheck = threads.deferToThread(self.escapeCheck, False)
escCheck.addCallback(self.escaped)
def cmdDisconnect(self):
self.sendData({'command': 'disconnect', 'uuid': self.factory.uuid})
def escapeCheck(self, sendOn):
print
print('Connecting to ' + self.factory.uuid + ' - Press Ctrl+A + D to quit')
print
test = True
while test and not self.lost:
k = self.getch()
if k == '\x01':
k2 = self.getch()
if k2 == 'd':
test = False
elif sendOn:
self.sendData(k)
return test
def escaped(self, input):
print('\rEscape sequence detected - Disconnecting')
reactor.stop()
def dataReceived(self, data):
datagrams = data.split('_')
for i in range(0, len(datagrams) / 3):
datagram = datagrams[3 * i:(3 * i) + 3]
if datagram[0] == 'honssh' and datagram[1] == 's':
self.theData = datagram[2]
self.parsePacket()
else:
print('Received incorrect packet - Disconnecting')
reactor.stop()
def sendData(self, theJson):
theData = base64.b64encode(json.dumps(theJson))
self.transport.write('honssh_c_' + theData + '_')
def getData(self, theData):
return json.loads(base64.b64decode(theData))
def parsePacket(self):
theJson = self.getData(self.theData)
if isinstance(theJson, dict):
if theJson['msg']:
print theJson['msg']
reactor.stop()
else:
if self.factory.command == 'list':
if self.factory.style in ['pretty', 'plain']:
self.printPrettyTable(theJson)
elif self.factory.style == 'json':
print json.dumps(theJson, indent=4)
reactor.stop()
elif self.factory.command in ['view']:
sys.stdout.write(theJson)
sys.stdout.flush()
def printPrettyTable(self, theJson):
sensorLength = 10
for sensor in theJson:
i = len(sensor['sensor_name'])
if i > sensorLength:
sensorLength = i
if self.factory.style == 'pretty':
print "UUID".ljust(34) + "Sensor Name".ljust(sensorLength + 2) + "PeerIP".ljust(17) + "Name".ljust(
9) + "Uptime"
for sensor in theJson:
for session in sensor['sessions']:
if len(session['channels']) == 0:
print 'AUTHENTICATING'.ljust(34) + sensor['sensor_name'].ljust(sensorLength + 2) + session[
'peer_ip'].ljust(17)
else:
for channel in session['channels']:
if 'end_time' not in channel:
dt = datetime.datetime.strptime(channel['start_time'], "%Y%m%d_%H%M%S_%f")
now = datetime.datetime.now()
totalTime = time.gmtime((now - dt).total_seconds())
print channel['uuid'].ljust(34) + sensor['sensor_name'].ljust(sensorLength + 2) + \
session['peer_ip'].ljust(17) + channel['name'].ljust(9) + time.strftime("%H:%M:%S",
totalTime)
class HonSSHInteractFactory(protocol.ClientFactory):
protocol = HonsshProtocol
def clientConnectionFailed(self, connector, reason):
print "Failed to connect"
reactor.stop()
def portType(input):
if not input.isdigit():
raise argparse.ArgumentTypeError('Port must be a number between 0 and 65535')
input = int(input)
if 0 <= input <= 65535:
return input
else:
raise argparse.ArgumentTypeError('Port must be a number between 0 and 65535')
def ipType(input):
m = re.match('(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)', input)
if m:
return input
else:
raise argparse.ArgumentTypeError('IP Must be a valid IP address')
def uuidType(input):
m = re.match('^[a-z0-9]{32}$', input)
if m:
return input
else:
raise argparse.ArgumentTypeError('UUID must be 32 Lower Alphanumeric Characters')
def parse_args():
"""Defines the command line arguments. """
parser = argparse.ArgumentParser('HonSSH Interaction Utility', usage='''
List connections
-c list [-o]
View a connection
-c view -u <uuid>
Disconnect a connection
-c disconnect -u <uuid>
''')
fmt = 'pretty'
ipa = '127.0.0.1'
cop = '5123'
conn = parser.add_argument_group('Connection Options')
conn.add_argument(
'-i',
dest='ip',
help='The HonSSH Interaction IP address to connect to (default: {0})'.format(ipa),
type=ipType,
default=ipa
)
conn.add_argument(
'-p',
dest='port',
help='The HonSSH Interaction port to connect to (default: {0})'.format(cop),
type=portType,
default=cop
)
command = parser.add_argument_group('Command Options')
command.add_argument(
'-c',
choices=['list', 'view', 'disconnect'],
dest='cmd',
help='Command to execute',
required=True
)
command.add_argument(
'-u',
dest='uuid',
help='UUID of connection to interact with. Required for \'view\' and \'disconnect\' commands',
type=uuidType
)
out = parser.add_argument_group('Output Options (Only for the \'list\' command)')
out.add_argument(
'-o',
dest='format',
choices=['pretty', 'plain', 'json'],
help='pretty = table with headers. plain = table without headers. json = JSON formatted. (default: {0})'.format(
fmt),
default=fmt
)
args = parser.parse_args()
return args
def process_args(args):
"""Process the command line arguments."""
if not args.cmd == 'list':
if not args.uuid:
print('UUID required for selected command')
sys.exit(1)
ifactory = HonSSHInteractFactory()
ifactory.command = args.cmd
ifactory.style = args.format
if args.cmd in ['view', 'disconnect']:
ifactory.uuid = args.uuid
reactor.connectTCP(args.ip, args.port, ifactory)
reactor.run()
def main():
args = parse_args()
process_args(args)
if __name__ == '__main__':
main()