Repository navigation
Expand file tree
/
Copy pathflipit.py
More file actions
335 lines (249 loc) · 9.75 KB
/
Copy pathflipit.py
File metadata and controls
335 lines (249 loc) · 9.75 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/usr/bin/env python
#
# Blatantly ripped off in large part from
#
# http://svn.osgdc.org/browse/~raw,r=4748/kusu/sandbox/kusu-2.1-upgradability/src/kits/base/packages/kusu-power/src/powerplugins/dellrac.py
#
import time
import httplib
import base64
import re
import sys
from getpass import getpass
import xml.sax
from hashlib import md5
class RCHandler( xml.sax.ContentHandler ):
# Static codes
rc_decode = {'x':'Unknown error!'}
rc_decode['0x0' ] = 'Success'
rc_decode['0x408' ] = 'Session Timeout'
rc_decode['0x43' ] = 'Unknown command'
rc_decode['0x140000' ] = 'Too many sessions'
rc_decode['0x140004' ] = 'Invalid password'
rc_decode['0x140005' ] = 'Invalid username'
rc_decode['0x160006' ] = 'Error in SID?'
rc_decode['0x170003' ] = 'Missing content in POST ?'
rc_decode['0x20308' ] = 'Console not available'
rc_decode['0x30003' ] = 'Console not active'
rc_decode['0x3000a' ] = 'Console active'
rc_decode['0xe0003' ] = 'Unknown serveraction'
elements = ['RC']
bool_responscode = 'false'
responsecode = ''
# ----------------------------------------
# Parser API
# ----------------------------------------
def processingInstriction(self, target, data):
pass
def startDocument(self):
bool_responscode = 'false'
responsecode = ''
def startElement(self, name, attrs):
if name == self.elements[0]:
self.bool_responscode = 'true'
def characters(self, string):
if self.bool_responscode == 'true':
self.responsecode = str(string)
self.bool_responscode == 'false'
def endElement(self, name):
if name == self.elements[0]:
self.bool_responscode = 'false'
# ----------------------------------------
# Public API
# ----------------------------------------
def get_rc(self):
ret_dict = ''
try:
ret_dict = {'code' : self.responsecode, \
'msg' : self.rc_decode[self.responsecode]}
except:
ret_dict = {'code' : self.responsecode, \
'msg' : 'Unknown error from RAC server'}
return ret_dict
class RACHandler( xml.sax.ContentHandler ):
elements = ['CHALLENGE', 'PROPTEXT', 'CMDOUTPUT']
bool_challenge = 'false'
challenge = ''
bool_proptext = 'false'
proptext = ''
bool_cmdoutput = 'false'
cmdoutput = ''
# ----------------------------------------
# Parser API
# ----------------------------------------
def processingInstriction(self, target, data):
pass
def startDocument(self):
self.bool_challenge = 'false'
self.challenge = ''
self.bool_proptext = 'false'
self.proptext = ''
self.bool_cmdoutput = 'false'
self.cmdoutput = ''
def startElement(self, name, attrs):
if name == self.elements[0]:
self.bool_challenge = 'true'
elif name == self.elements[1]:
self.bool_proptext = 'true'
elif name == self.elements[2]:
self.bool_cmdoutput = 'true'
def characters(self, string):
if self.bool_challenge == 'true':
self.bool_challenge = 'false'
self.challenge = string
elif self.bool_proptext == 'true' and string != '\n':
self.proptext = string
elif self.bool_cmdoutput == 'true' and string != '\"':
self.cmdoutput = self.cmdoutput + string
def endElement(self, name):
if name == self.elements[0]:
self.bool_challenge = 'false'
elif name == self.elements[1]:
self.bool_proptext = 'false'
elif name == self.elements[2]:
self.bool_cmdoutput = 'false'
def get_challenge(self):
return self.challenge
class RAC( object ):
def __init__( self, host, user, pwd ):
self._auth = { 'USER' : user,
'PASS' : pwd }
self._host = host
self._challenge = None
self._sessionID = self.login()
def __del__( self ):
pass
def crc16( self, hash ):
str = hash
crc = 0L
t = len(str)
s = ''
for k in range(0, t):
s = str[k:k+1] # substring
s = ord(s) # numeric ASCII value of first character
s = s << 8 # shift left 8
crc = crc ^ s # bitwise exclusive OR operator
for x in range(0, 8):
if ((crc & 0x8000) == 32768):
crc = long((crc<<1) ^ 0x1021)
else:
crc = long(crc<<1)
crc = crc & 0xFFFF
return crc
def hashGen( self, password, challenge ):
c_bytes = ''
p_bytes = ''
challenge_bytes = []
pwd_hash = []
xor_bytes = []
c_bytes = base64.decodestring(challenge)
challenge_bytes = list(c_bytes)
challenge_bytes = challenge_bytes[0:16]
# Convert to unsigned char:
for i in range(0, 16):
challenge_bytes[i] = ord( challenge_bytes[i] )
m = md5()
m.update(password)
p_bytes = m.digest()
pwd_hash = list(p_bytes)
pwd_hash = pwd_hash[0:16]
# Convert to unsigned char:
for i in range(0, 16):
pwd_hash[i] = ord(pwd_hash[i])
# XOR
for i in range(0, 16):
xor_bytes.append(challenge_bytes[i] ^ pwd_hash[i])
# List to string (16)
hb = ''
for i in range(0, 16):
hb = hb + chr(xor_bytes[i])
mm = md5()
mm.update(hb)
hash = mm.digest()
crc = self.crc16( hash )
hash = hash + chr(crc & 0xff) + chr(crc >> 8 & 0xff)
hash = base64.encodestring(hash)
return re.sub("\n", "", hash)
def login( self ):
XML_handler = RACHandler()
RC_handler = RCHandler()
conn = httplib.HTTPSConnection( self._host )
conn.putrequest( 'GET', '/cgi/challenge' )
conn.putheader( 'User-Agent', 'Scali/1.0' )
conn.endheaders()
http_response = conn.getresponse()
if http_response.status != 200:
raise Exception, "%d %s" % (http_response.status, str(http_response.reason))
cookie = http_response.getheader('Set-Cookie')
xml_content = http_response.read()
conn.close()
xml.sax.parseString(xml_content, RC_handler)
if RC_handler.get_rc()['code'] != str('0x0'):
raise Exception, "Failed with error code {0}, {1}".format( RC_handler.get_rc()['code'], RC_handler.get_rc()['msg'] )
xml.sax.parseString(xml_content, XML_handler)
challenge = XML_handler.get_challenge()
self._challenge = challenge
if re.match( 'sid=', cookie ):
session_id = cookie.split()[0]
session_id = re.sub('sid=', '', session_id)
session_id = re.sub(';', '', session_id)
else:
raise Exception, "Response from RAC server contained no Session ID"
# Phase 2. (of 3) Making password hash
hash = self.hashGen( self._auth['PASS'], challenge )
if not hash:
raise Exception, "Hash string empty"
# Phase 3. (of 3) Connecting with hashed password
conn = httplib.HTTPSConnection(self._host)
conn.putrequest( 'GET', '/cgi/login?user={0}&hash={1}'.format( self._auth['USER'], hash ) )
conn.putheader( 'Cookie', 'sid=' + session_id )
conn.putheader( 'User-Agent', 'Scali/1.0' )
conn.endheaders()
http_response = conn.getresponse()
if http_response.status != 200:
raise Exception, "{0} {1}".format( http_response.status, str(http_response.reason) )
cookie = http_response.getheader( 'Set-Cookie' )
xml_content = http_response.read()
conn.close()
xml.sax.parseString(xml_content, RC_handler)
if RC_handler.get_rc()['code'] != str('0x0'):
raise Exception, "Failed with error code {0}, {1}".format( RC_handler.get_rc()['code'], RC_handler.get_rc()['msg'] )
return session_id
def command(self, xml_command):
conn = httplib.HTTPSConnection(self.host)
headers = { "Content-type": "application/x-www-form-urlencoded",
"Cookie": "sid=" + self._sessionID,
"User-Agent": "Scali/1.0" }
conn.request( 'POST', "/cgi/bin", xml_command, headers )
http_response = conn.getresponse()
if http_response.status != 200:
raise Exception, "{0} {1}".format( http_response.status, str( http_response.reason ) )
cookie = http_response.getheader( 'Set-Cookie' )
xml_content = http_response.read()
conn.close()
return xml_content
class Telnet( object ):
def __init__( self, rac ):
self._RAC = rac
def __del__( self ):
pass
def payload( self, cmd ):
return '<?XML version="1.0"?><?RMCXML version="1.0"?><RMCSEQ><REQ CMD="xml2cli2"><CMDINPUT>{0}</CMDINPUT></REQ></RMCSEQ>'.format( cmd )
def ON( self ):
cmd = 'd3debug propset ENABLE_TELNET=TRUE'
self._RAC.command( self.payload( cmd ) )
def OFF( self ):
cmd = 'd3debug propset ENABLE_TELNET=FALSE'
self._RAC.command( self.payload( cmd ) )
if __name__ == "__main__":
state = raw_input( 'Type "On" or "Off": ' )
cred = []
cred.append( raw_input('Node IP: ') )
cred.append( raw_input('Username: ') )
cred.append( getpass('Password: ') )
NODE = RAC( cred[0], cred[1], cred[2] )
TELNET = Telnet( NODE )
if state == 'On':
TELNET.ON()
if state == 'Off':
TELNET.OFF()