forked from hugsy/stuff
/
pentestlib2.py
944 lines (727 loc) · 23.5 KB
/
pentestlib2.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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
"""
Ensemble de fonctions utiles pour les pentests.
Marche mieux pour Python 2.6+, marche quasi pas avec Python 3.x
"""
##################################################
#
# imports
#
try :
import re
from zlib import decompress as GUNZIP, error as ZLIB_ERROR
from urlparse import urlparse
from socket import socket, error as SOCKET_ERROR, SOCK_STREAM, AF_INET
from ssl import wrap_socket
from re import compile, match
from sys import stdout, stderr, version_info
from datetime import datetime
from httplib import HTTPSConnection, HTTPConnection
from inspect import stack
from random import randint
from itertools import izip, cycle
except ImportError, ie:
print("[-] Failed on import: %s" % ie)
exit(1)
except ImportWarning, iw:
print("[-] Critical: %s" % iw)
exit(1)
major, minor = version_info[0:2]
if major < 2 : raise ImportError ("Python version must be at least 2.7+")
if minor < 7 : raise ImportWarning ("Python version must be at least 2.7+")
##################################################
#
# HTTP/SQL/HTML Part
#
class HTTPWrapper :
"""
Wrapper pour les connexions HTTP. Gere toute la session HTTP. Peut etre
utilise dans l'interpreteur ou importe dans un script.
Utilisation basique
>>> h = HTTPWrapper("https://www.google.fr", proxy="localhost:8080")
>>> h.execute()
>>> print h.response_headers
>>> print h.response_body
Features:
- gestion du deflate par fingerprint (plus par header)
- la gestion du format data json
- gere le proxy http normal et ssl
- raw fuzzing functions
- http keep-alive
Todo:
- ameliorer le fuzz (base sur dico, etc.)
"""
def __init__(self, url, method="GET", proxy=None):
""" Initialize HTTPWrapper object, parse l'url, declare les fonctions obsoletes """
self.__name__ = "HTTPWrapper"
o = urlparse(url, "http")
self.method = method
self.proto = o.scheme
if self.proto == "https":
self.ssl = True
self.http = HTTPSConnection(o.netloc, timeout = 15)
else :
self.ssl = False
self.http = HTTPConnection(o.netloc, timeout = 15)
self.host, self.port = spliturl(o.netloc, self.ssl)
if o.path == '':
self.path = '/'
else :
self.path = o.path
self.proxy = proxy
self._headers = {}
self._get = []
self._post = []
self._response = {}
self._format = "plain"
self._keep_alive = False
self.setbasicheaders()
for elt in o.query.split("&"):
if len(elt) == 0: continue
try:
k,v = elt.split("=", 1)
except ValueError:
k = elt
v = "1"
self.addparam(k, html_decode(v))
def execute(self):
"""
execute la requete et stocke les resultats dans self.response_headers et
self.response_body
"""
if len(self._post) > 0:
self.method = "POST"
if self.method == "POST":
self.addheader("Content-Length", "%d" % len(self.post()))
self.addheader("Content-Type", "application/x-www-form-urlencoded")
if self.http.sock is None and self.proxy:
self.tunnel_connect()
self.http.request(method = self.method,
url = self.geturl(),
body = self.post(),
headers = self._headers)
try:
res = self.http.getresponse()
self._response = {
"headers": {
"version": res.version,
"status": res.status,
"reason": res.reason,
},
"body": res.read(),
}
self._response["headers"].update( [(k, v) for k,v in res.getheaders()] )
self.response_headers = self._response_headers()
self.response_body = self._response_body()
except Exception, e:
ERR("Exception in %s: %s" % (called(), e))
finally:
if not self._keep_alive or \
("connection" in self.response_headers.keys() \
and self.response_headers["connection"] == "close") :
self.close()
def close(self):
if self.http.sock is not None :
self.http.close()
self.http.sock = None
def tunnel_connect(self):
proxy_host, proxy_port = spliturl(self.proxy, self.ssl)
proxy_port = int(proxy_port)
try :
s = socket(AF_INET, SOCK_STREAM)
s.connect((proxy_host, proxy_port))
if self.ssl:
buf = "CONNECT %s:%s HTTP/1.0\r\n" % (self.host, self.port)
buf+= "Host: %s\r\n" % self.host
buf+= "User-Agent: Python HTTPWrapper\r\n"
buf+= "\r\n"
s.sendall(buf)
line = s.recv(256)
proto, code, msg = line.split(" ", 2)
code = int(code)
if code != 200 and not msg.lower().startswith("connection established") :
raise SOCKET_ERROR ("Proxy denied (%d) our request: %s" % (code, msg))
s = wrap_socket(s, self.http.key_file, self.http.cert_file)
except SOCKET_ERROR, msg:
ERR ("%s: Failed to proxy, using direct link. Reason: %s" % (called(), msg))
s.close()
del (s)
self.proxy = None
s = socket(AF_INET, SOCK_STREAM)
s.connect((self.host, self.port))
if self.ssl:
s = wrap_socket(s, self.http.key_file, self.http.cert_file)
self.close()
self.http.sock = s
return
def geturl(self):
""" @return l'url complete """
if self.proxy is None:
return "%s%s" % (self.path, self.get())
else:
return "%s://%s:%d%s%s" % (self.proto,
self.host,
self.port,
self.path,
self.get())
def addheader(self, param, value):
""" ajoute un header. si le header est un Cookie, il sera concatene. """
if param in self._headers and param == "Cookie" :
self._headers[param] += ";" + value
else :
self._headers[param] = value
def setbasicheaders(self):
""" add few basic headers """
self.addheader("Host", self.host)
self.addheader("User-Agent", "Mozilla/5.0 (compatible; MSIE 7.0; Windows NT 6.0; fr-FR)")
self.addheader("Accept-Encoding", "text/plain")
self.addheader("Connection", "Close")
# self.addheader("Accept-Encoding", "gzip,deflate")
def addparam(self, key, value):
found = False
if key in (None, ""):
return
ptr = None
if self.method == "GET" :
ptr = self._get
elif self.method == "POST":
ptr = self._post
else:
return
for a in ptr :
k,v = a
if k == key :
a[1] = value
found = True
if not found :
ptr.append([key, value.strip()])
def getparams(self, tab):
if self._format == "json":
parts = [ "%s:%s" % (k,v) for k,v in tab]
return '{' + ','.join(parts) + '}'
else:
return "&".join([ "%s=%s" % (html_encode(k), html_encode(v)) for k,v in tab])
def get(self):
if len(self._get) == 0: return ""
return "?" + self.getparams(self._get)
def post(self):
if (len(self._post) == 0): return ""
return self.getparams(self._post)
def _response_headers(self):
""" @return le hash des headers retournes par le serveur HTTP """
try :
return self._response['headers']
except :
return "No headers found"
def _response_body(self):
"""
@return string with HTTP response body.
Now uses magic number instead of being based on HTTP Header "Content-Encoding" (NC)
"""
try:
body = self._response['body']
except KeyError:
body = "No body found"
if body.startswith("\x1f\x8b\x08") :
try:
body = GUNZIP(body)
except ZLIB_ERROR, e :
ERR("Failed to gunzip: %s" % e)
body = ""
return body
def __str__(self):
""" @return string with object properties """
chaine = "Target: %s\n" % self.host
if self.proxy:
chaine += "Using proxy: %s\n" % self.proxy
if self.ssl:
chaine += "Using SSL\n"
chaine += "%s %s HTTP/1.1\n" % (self.method, self.geturl())
for k,v in self._headers.iteritems():
chaine += "%s: %s\n" % (k,v)
chaine += "\n%s\n" % self.post()
return chaine
def __desc__(self):
""" @return object description """
return self.__str__()
def set_keep_alive(self, boolean):
if boolean not in (True, False):
return
if boolean:
self.addheader("Connection", "Keep-Alive")
else:
self.addheader("Connection", "Close")
def fuzz(self, obj) :
""" HTTPWrapper object random fuzz """
try:
getattr(self, obj)
except AttributeError :
ERR("%s has no attribute '%s'" % (self.__name__, obj))
return
if obj in ("host", "port"):
ERR("Cannot fuzz '%s' object" % obj)
return
def _fuzz(o):
FUZZ_SPACE = 256
FUZZ_MAXLEN = 50
lg = randint(0, FUZZ_MAXLEN)
c = ""
while lg:
c += chr(randint(0, FUZZ_SPACE))
lg-= 1
return c
type_obj = type(getattr(self, obj)).__name__
if type_obj == 'list' :
setattr (self, obj, [ _fuzz(x) for x in obj ])
elif type_obj == 'dict':
setattr (self, obj, dict([ (_fuzz(k), _fuzz(v)) for k,v in obj.items()]))
else :
setattr (self, obj, _fuzz(obj))
def spliturl (netloc, ssl=False):
""" split hostname and port number (if specified). """
if netloc in (None,"") :
return None, None
try :
i = netloc.index(":")
host, port = netloc[:i], int(netloc[i+1:])
except ValueError :
host = netloc
if ssl:
port = 443
else :
port = 80
return host, port
def called():
""" @return le nom de la fonction appelee comme une string """
return stack()[1][3]
def called_line():
""" @return la ligne de la fonction appelee comme une string """
return stack()[1][4]
def caller_name():
""" @return le nom de la fonction appelante comme une string """
return stack()[2][3]
def caller_line():
""" @return la ligne de la fonction appelante comme une string """
return stack()[2][4]
def GET(url, proxy=None):
"""
Comme la commande Perl LWP GET
Affiche le code status et message
@return une liste (header de reponse, contenu de reponse)
"""
try:
http = HTTPWrapper(url=url, method="GET", proxy=proxy)
http.addheader("Accept-Charset", "ISO-8859-1,UTF-8;q=0.7,*;q=0.5")
http.execute()
h, b = (http.response_headers, http.response_body)
except Exception, e:
ERR("Function %s: %s" % (called(), e))
h, b = None, None
return (h,b)
def POST(url, data="", proxy=None):
"""
Comme la commande Perl POST
Affiche le code status et message
@return une liste (header de reponse, contenu de reponse)
"""
try:
http = HTTPWrapper(url=url, method="POST", proxy=proxy)
for o in data.split("&"):
if len(o) == 0: continue
k,v = o.split("=",1)
http.addparam(k,v)
http.execute()
h, b = (http.response_headers, http.response_body)
except Exception,e:
ERR("%s: %s" % (called(), e))
h, b = None, None
return (h,b)
def TRACE(url="/", max_fwd=1, proxy=None):
"""
Permet de detecter la presence de Reverse Proxy: envoie 2 requetes HTTP
en changeant le Max_Forward.
@return une liste avec les headers de 2 HTTPResponse
"""
h1, h2 = None, None
try:
http = HTTPWrapper(url=url, method="TRACE", proxy=proxy)
http.addheader("Max-Forwards", "0")
http.execute()
h1 = http.response_headers
except Exception,e:
ERR(e)
h1 = None
del(http)
try:
http = HTTPWrapper(url=url, method="TRACE", proxy=proxy)
http.addheader("Max-Forwards", str(max_fwd))
http.execute()
h2 = http.response_headers
except Exception,e:
ERR(e)
h2 = None
return (h1, h2)
def html_encode(chaine, format="html", full=False):
"""
encode une chaine dans un format donne
formats actuels supportes :
- html : html
- dec : decimal
- hex : hexadecimal
pour le decode, cf. html_decode
"""
res = ""
if format.lower() not in ("html", "dec", "hex"):
return res
for car in chaine :
if car == '_':
res += car
continue
if not full :
if '0' <= car <= '9' or \
'A' <= car <= 'Z' or\
'a' <= car <= 'z' :
res += car
continue
if format == "html" :
res += "%%%x" % ord(car)
elif format == "dec" :
res += "&#%s;" % ord(car)
elif format == "hex" :
res += "&#x%x;" % ord(car)
return res
def html_decode(chaine, format="html"):
"""
html_decode est l'inverse de la fonction html_encode
decode une chaine dans un format donne
formats actuels supportes :
- html : html
- dec : decimal
- hex : hexadecimal
"""
motifs = {'html':'%', 'dec':'&#', 'hex':'&#x'}
res = ""
if format not in motifs.keys() :
return res
if format in ('dec', 'hex'):
chaine = chaine.strip(';')
motif = motifs[format]
i = chaine.find(motif)
if i == -1:
return chaine
else:
res += chaine[:i]
while i != -1 :
j = i + len(motif) + 2
l = chaine[i+len(motif):j]
if format == "dec":
letter = int(l)
elif format in ("html", "hex"):
letter = int(l, 16)
res += chr(letter)
i = chaine[j:].find(motif)
if i != -1 :
i += j
res += chaine[j:i]
else:
res += chaine[j:]
return res
def ascii2mysql(chaine):
"""
Convertit chaine en forme MySQL CHAR()
"""
sql = [ "%d" % ord(c) for c in chaine ]
return "CHAR(" + (",".join(sql)) + ")"
def ascii2mssql (chaine):
"""
Convertit chaine en forme MS-SQL CHAR()
"""
sql = [ "CHAR(%d)" % ord(c) for c in chaine ]
return "+".join(sql)
from urlparse import urlparse
class HTTPReq:
"""
Classe de parsing de requete HTTP Raw -> Object.
Permet egalement de retourner cette requete Object -> Raw
"""
CRLF = "\r\n"
SEP = ": "
def __init__(self, req):
self.method = 'GET'
self.path = '/'
self.version = 'HTTP/1.1'
self.data = ''
self.headers = {}
elts = [ x for x in req.split(self.CRLF) if len(x) ]
self.method, self.path, self.version = elts.pop(0).split(" ")
if self.path.startswith("http://") or self.path.startswith("https://"):
o = urlparse(self.path)
if len(o.path):
self.path = o.path
if len(o.query):
self.path = o.path + "?" + o.query
if (req.startswith("POST ")):
self.data = elts.pop()
for header in elts:
try :
h,v = header.split(self.SEP, 1)
except ValueError:
h = header
v = ''
finally:
self.headers[h] = v
def is_header(self, chunk):
return self.SEP in chunk
def __str__(self):
first = ["%s %s %s" % (self.method, self.path, self.version)]
headers = ["%s%s%s" % (h,self.SEP,v) for (h,v) in self.headers.items()]
if self.method == 'POST':
return self.CRLF.join(first+headers+[self.data])+self.CRLF*2
else :
return self.CRLF.join(first+headers)+self.CRLF*2
##################################################
#
# Some Unicode converting functions
#
def ucs_string(chaine, format=2):
def ucs_and (a, b):
i = 0
while i < len(b):
a[i] |= b[i]
i += 1
res = []
for i in a :
res.append('%x' % i)
return res
def ucs2(c):
byte = int('%x' % ord(c), 16)
parts = []
parts.append(byte >> 6) #
parts.append(byte & 0x3f) # = 0011 1111
u = []
u.append(0xC0) # = 1100 0000
u.append(0x80) # = 1000 0000
return ucs_and(u, parts)
def ucs3(c):
byte = int('%x' % ord(c), 16)
parts = []
parts.append(0x00)
parts.append(byte >> 6) #
parts.append(byte & 0x3f) # = 0011 1111
u = []
u.append(0xE0) # = 1110 0000
u.append(0x80) # = 1000 0000
u.append(0x80) # = 1000 0000
return ucs_and(u, parts)
def ucs4(c):
byte = int('%x' % ord(c), 16)
parts = []
parts.append(0x00)
parts.append(0x00)
parts.append(byte >> 6)
parts.append(byte & 0x3f) # = 0011 1111
u = []
u.append(0xF0) # = 1111 0000
u.append(0x80) # = 1000 0000
u.append(0x80) # = 1000 0000
u.append(0x80) # = 1000 0000
return ucs_and(u, parts)
if format not in xrange(2,5):
return -1
res = []
for c in chaine :
if format == 2:
enc = ucs2(c)
elif format == 3:
enc = ucs3(c)
elif format == 4:
enc = ucs4(c)
else :
print ("Wtf ?")
return -1
res.extend(enc)
return res
##################################################
#
# Network functions
#
def expand_cidr(plage, out="/dev/stdout", mode='w'):
"""
Transforme une plage CIDR en une liste d'IP unique et concatene
le resultat dans le fichier "out"
"""
try:
from netaddr import iter_unique_ips
except ImportError as ie :
print("[-] %s" % ie)
return -1
regexp = compile("([0-9]{1,3}.){3}[0-9]{1,3}/[0-9]{0,2}")
if not match(regexp, plage):
return 0
with open(out, mode) as fd:
try:
for ip in iter_unique_ips(plage):
fd.write("%s\n" % str(ip))
except Exception as e:
print("[-] %s" % e)
return -1
return 0
def expand_cidr_file(_file, out="/dev/stdout"):
"""
Etend en IP unique toutes les plages d'adresses trouvees dans le
fichier passe en parametre.
"""
lines = [x.strip() for x in open(_file, 'r').xreadlines()]
for line in lines :
expand_cidr(line, out, 'a')
return 0
def ip2dword(ip):
b = [int(x) for x in ip.split(".")]
b.reverse()
d = 0
for i in range(len(b)):
d+= b[i] * (256**i)
return d
def ip2hex(ip):
b = ["%#x"%int(x) for x in ip.split(".")]
return ".".join(b)
def ip2octal(ip):
b = ["0%0o"%int(x) for x in ip.split(".")]
return ".".join(b)
##################################################
#
# Logging functions
#
def now():
"""Return the time of now, suitable for log"""
return datetime.now().strftime("%d/%m/%y %H:%M:%S")
def OK(msg):
""" Message display function"""
stdout.write("[+] %s : %s\n" % (now(), msg))
stdout.flush()
def INFO(msg):
""" Information (verbose mode) message display function"""
stdout.write("[*] %s : %s\n" % (now(), msg))
stdout.flush()
def ERR(msg):
""" Error message display function"""
stderr.write("[!] %s : %s\n" % (now(), msg))
stderr.flush()
##################################################
#
# Crypto functions
#
def Levenshtein(s,t):
"""
implementation de l'algo de distance de Levenshtein
http://en.wikibooks.org/wiki/Algorithm_implementation/Strings/Levenshtein_distance
"""
s = ' ' + s
t = ' ' + t
d = {}
S = len(s)
T = len(t)
for i in range(S):
d[i, 0] = i
for j in range (T):
d[0, j] = j
for j in range(1,T):
for i in range(1,S):
if s[i] == t[j]:
d[i, j] = d[i-1, j-1]
else:
d[i, j] = min(d[i-1, j] + 1,
d[i, j-1] + 1,
d[i-1, j-1] + 1)
return d[S-1, T-1]
def cesar(text, shift):
'''
cesar algo implementation
'''
cesar = ""
for c in text:
if not c.isalpha():
cesar += c
continue
l = ord(c)+shift
if (c.islower() and l > 122) or (c.isupper() and l > 90):
l -= 26
cesar += chr(l)
return cesar
def vigenere(plaintext, key, decrypt=False):
'''
vigenere algo implementation
'''
vigenere = ""
charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
if not decrypt: # encrypt
for i in range(len(plaintext)):
c = plaintext[i]
if not c.isalpha():
vigenere += c
continue
c = c.upper()
k = key[i%len(key)].upper()
# vigenere += chr(ord(c)^ord(k))
l = charset[(ord(c)-0x41+ord(k)-0x41)%26]
vigenere += l
else:
for i in range(len(plaintext)):
c = plaintext[i]
if not c.isalpha():
vigenere += c
continue
c = c.upper()
k = key[i%len(key)].upper()
new_charset = [charset[j%26] for j in range(ord(k)-0x41, ord(k)-0x41+26)]
vigenere += charset[new_charset.index(c)]
return vigenere
##################################################
#
# SQL
#
def sqlparse(req, encode=None):
"""
Parse une requete SQL et la retourne sous forme de dict()
"""
keywords = ['select', 'limit', 'from', 'where']
req = req.lower()
sql_blocks = {}
new_req = req
if encode in ("mysql", "MySQL") :
convert_func = ascii2mysql
elif encode in ("mssql", "SQLServer") :
convert_func = ascii2mssql
else:
convert_func = str
# convertion en CHAR()+CHAR()+...
for i in re.findall("'[^']'", req):
new_req = re.sub(i, convert_func(i.replace("'","")), new_req)
req = new_req
# creation des blocks
for keyword in keywords:
if keyword in req:
block_name = keyword
block_data = ""
for word in req[req.find(keyword)+len(keyword):].split(" ") :
if word in keywords : break
if word !='' : block_data += " " + word
sql_blocks[block_name] = block_data.strip()
return sql_blocks
##################################################
#
# Misc functions
#
# return an hexdump-style output of src buffer
def hexdump(src, length=0x10):
f=''.join([(len(repr(chr(x)))==3) and chr(x) or '.' for x in range(256)])
n=0
result=''
while src:
s,src = src[:length],src[length:]
hexa = ' '.join(["%02X"%ord(x) for x in s])
s = s.translate(f)
result += "%04X %-*s %s\n" % (n, length*3, hexa, s)
n+=length
return result
def xor_crypt_string(data, key):
return ''.join(chr(ord(x) ^ ord(y)) for (x,y) in izip(data, cycle(key)))