-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebPayloadsEncodings.py
467 lines (363 loc) · 14.6 KB
/
WebPayloadsEncodings.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
###################
# This tool encodes Web payloads with some different encoding.
# Copyright (C) 2022, 2023 Maurice Lambert
# 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, see <https://www.gnu.org/licenses/>.
###################
r"""
This tool encodes Web payloads with some different encoding.
~# python3.11 WebPayloadsEncodings.py javascript 'this is my test'
String.fromCharCode(116,104,105,115,32,105,115,32,109,121,32,116,101,115,116)
~# python3.11 WebPayloadsEncodings.py pourcent 'this is my test'
%74%68%69%73%20%69%73%20%6d%79%20%74%65%73%74
~# python3.11 WebPayloadsEncodings.py 'this is my test'
'ms', 'sql server', 'sql_server', 'ms sql', 'ms_sql'
char(116)+char(104)+char(105)+char(115)+char(32)+char(105)+char(115)+char(32)+char(109)+char(121)+char(32)+char(116)+char(101)+char(115)+char(116)
'sqlite', 'oracle', 'oracle sql', 'oracle_sql', 'postgre', 'postgresql'
char(116)||char(104)||char(105)||char(115)||char(32)||char(105)||char(115)||char(32)||char(109)||char(121)||char(32)||char(116)||char(101)||char(115)||char(116)
'mysql', 'mariadb'
CONCAT(char(116),char(104),char(105),char(115),char(32),char(105),char(115),char(32),char(109),char(121),char(32),char(116),char(101),char(115),char(116))
'unicode'
\u{0074}\u{0068}\u{0069}\u{0073}\u{0020}\u{0069}\u{0073}\u{0020}\u{006d}\u{0079}\u{0020}\u{0074}\u{0065}\u{0073}\u{0074}
'javascript', 'script'
String.fromCharCode(116,104,105,115,32,105,115,32,109,121,32,116,101,115,116)
'html unicode', 'html_unicode', 'unicode html', 'unicode_html'
\u{0074}\u{0068}\u{0069}\u{0073}\u{0020}\u{0069}\u{0073}\u{0020}\u{006d}\u{0079}\u{0020}\u{0074}\u{0065}\u{0073}\u{0074}
'hexa escaping', 'hexadecimal escaping', 'hexa_escaping', 'hexadecimal_escaping', 'hexa escape', 'hexa_escape', 'hexadecimal escape', 'hexadecimal_escape'
\x74\x68\x69\x73\x20\x69\x73\x20\x6d\x79\x20\x74\x65\x73\x74
'octal escaping', 'octal_escaping', 'octal escape', 'octal_escape'
\164\150\151\163\040\151\163\040\155\171\040\164\145\163\164
'hexadecimal', 'hexa'
0x74686973206973206d792074657374
'html', 'html_decimal', 'html decimal'
this is my test
'html hexadecimal', 'html_hexadecimal', 'html hexa', 'html_hexa'
this is my test
'url', 'uri', 'pourcent'
%74%68%69%73%20%69%73%20%6d%79%20%74%65%73%74
'2pourcent', 'pourcent2', 'double pourcent', 'double_pourcent'
%2574%2568%2569%2573%2520%2569%2573%2520%256d%2579%2520%2574%2565%2573%2574
~#
>>> code = payloads_encodings("html", 'this is my test')
this is my test
>>> code = payloads_encodings("all", 'this is my test')
'ms', 'sql server', 'sql_server', 'ms sql', 'ms_sql'
char(116)+char(104)+char(105)+char(115)+char(32)+char(105)+char(115)+char(32)+char(109)+char(121)+char(32)+char(116)+char(101)+char(115)+char(116)
'sqlite', 'oracle', 'oracle sql', 'oracle_sql', 'postgre', 'postgresql'
char(116)||char(104)||char(105)||char(115)||char(32)||char(105)||char(115)||char(32)||char(109)||char(121)||char(32)||char(116)||char(101)||char(115)||char(116)
'mysql', 'mariadb'
CONCAT(char(116),char(104),char(105),char(115),char(32),char(105),char(115),char(32),char(109),char(121),char(32),char(116),char(101),char(115),char(116))
'unicode'
\u{0074}\u{0068}\u{0069}\u{0073}\u{0020}\u{0069}\u{0073}\u{0020}\u{006d}\u{0079}\u{0020}\u{0074}\u{0065}\u{0073}\u{0074}
'javascript', 'script'
String.fromCharCode(116,104,105,115,32,105,115,32,109,121,32,116,101,115,116)
'html unicode', 'html_unicode', 'unicode html', 'unicode_html'
\u{0074}\u{0068}\u{0069}\u{0073}\u{0020}\u{0069}\u{0073}\u{0020}\u{006d}\u{0079}\u{0020}\u{0074}\u{0065}\u{0073}\u{0074}
'hexa escaping', 'hexadecimal escaping', 'hexa_escaping', 'hexadecimal_escaping', 'hexa escape', 'hexa_escape', 'hexadecimal escape', 'hexadecimal_escape'
\x74\x68\x69\x73\x20\x69\x73\x20\x6d\x79\x20\x74\x65\x73\x74
'octal escaping', 'octal_escaping', 'octal escape', 'octal_escape'
\164\150\151\163\040\151\163\040\155\171\040\164\145\163\164
'hexadecimal', 'hexa'
0x74686973206973206d792074657374
'html', 'html_decimal', 'html decimal'
this is my test
'html hexadecimal', 'html_hexadecimal', 'html hexa', 'html_hexa'
this is my test
'url', 'uri', 'pourcent'
%74%68%69%73%20%69%73%20%6d%79%20%74%65%73%74
'2pourcent', 'pourcent2', 'double pourcent', 'double_pourcent'
%2574%2568%2569%2573%2520%2569%2573%2520%256d%2579%2520%2574%2565%2573%2574
>>>
Tests:
~# python3.11 -m doctest -v WebPayloadsEncodings.py
23 tests in 18 items.
23 passed and 0 failed.
Test passed.
"""
__version__ = "0.0.2"
__author__ = "Maurice Lambert"
__author_email__ = "[email protected]"
__maintainer__ = "Maurice Lambert"
__maintainer_email__ = "[email protected]"
__description__ = (
"This tool encodes Web payloads with some different encoding."
)
license = "GPL-3.0 License"
__url__ = "https://github.com/mauricelambert/WebPayloadsEncodings"
copyright = """
WebPayloadsEncodings Copyright (C) 2022, 2023 Maurice Lambert
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions.
"""
__license__ = license
__copyright__ = copyright
__all__ = ["payloads_encodings"]
print(copyright)
from collections import defaultdict
from typing import Union, Iterable
from binascii import hexlify
from sys import argv, stderr
def encode(string: Union[str, bytes]) -> bytes:
"""
This function encodes strings.
>>> encode('abc')
b'abc'
>>> encode(b'abc')
b'abc'
>>>
"""
if isinstance(string, str):
return string.encode()
return string
def pourcent_encoding(string: Union[str, bytes]) -> str:
"""
This function encodes string with pourcent encoding (URL encoding).
>>> pourcent_encoding('abc')
'%61%62%63'
>>>
"""
return "%" + hexlify(encode(string), "%").decode()
def double_pourcent_encoding(string: Union[str, bytes]) -> str:
"""
This function double encodes string with pourcent encoding (URL encoding).
>>> double_pourcent_encoding('abc')
'%2561%2562%2563'
>>>
"""
return pourcent_encoding(string).replace("%", "%25")
def html_decimal(string: Union[str, bytes], leading_zeros: int = 2) -> str:
"""
This function encodes strings in HTML decimal encoding.
>>> html_decimal('abc')
'abc'
>>> html_decimal('abc', leading_zeros=4)
'abc'
>>>
"""
return (
"&#"
+ ";&#".join(
[str(char).rjust(leading_zeros, "0") for char in encode(string)]
)
+ ";"
)
def html_hexadecimal(string: Union[str, bytes], leading_zeros: int = 2) -> str:
"""
This function encodes strings in HTML hexadecimal encoding.
>>> html_hexadecimal('abc')
'abc'
>>> html_hexadecimal('abc', leading_zeros=4)
'abc'
>>>
"""
return (
"&#x"
+ ";&#x".join(
[
hex(char)[2:].rjust(leading_zeros, "0")
for char in encode(string)
]
)
+ ";"
)
def sqlite_char(string: Union[str, bytes]) -> str:
"""
This function returns a SQLite payload to write string without quotes.
>>> sqlite_char('abc')
'char(97)||char(98)||char(99)'
>>>
"""
return sql_char(string, delimiter="||")
postgresql_char = oracle_sql_char = sqlite_char
def sql_char(string: Union[str, bytes], delimiter: str = "||") -> str:
"""
This function returns a SQL payload to write string without quotes.
>>> sql_char('abc')
'char(97)||char(98)||char(99)'
>>> sql_char('abc', delimiter='+')
'char(97)+char(98)+char(99)'
>>>
"""
return delimiter.join(f"char({char})" for char in encode(string))
def ms_sql_char(string: Union[str, bytes]) -> str:
"""
This function returns a MS SQL Server payload to write string without quotes.
>>> ms_sql_char('abc')
'char(97)+char(98)+char(99)'
>>>
"""
return sql_char(string, delimiter="+")
def mariadb_sql_char(string: Union[str, bytes]) -> str:
"""
This function returns a MS SQL Server payload to write string without quotes.
>>> mariadb_sql_char('abc')
'CONCAT(char(97),char(98),char(99))'
>>>
"""
return "CONCAT(" + sql_char(string, delimiter=",") + ")"
mysql_char = mariadb_sql_char
def hexa_number(string: Union[str, bytes]) -> str:
"""
This function returns a long hexadecimal number from strings.
>>> hexa_number('abc')
'0x616263'
>>>
"""
return "0x" + hexlify(encode(string)).decode()
def javascript_char(string: Union[str, bytes]) -> str:
"""
This function returns a javascript payload to write string without quotes.
>>> javascript_char('abc')
'String.fromCharCode(97,98,99)'
>>>
"""
return (
"String.fromCharCode("
+ ",".join(str(char) for char in encode(string))
+ ")"
)
def hexa_escaping(string: Union[str, bytes]) -> str:
r"""
This function returns a hexadecimal payload to bypass basic filters.
>>> print(hexa_escaping('abc'))
\x61\x62\x63
>>>
"""
return r"\x" + r"\x".join(f"{char:0>2x}" for char in encode(string))
def octal_escaping(string: Union[str, bytes]) -> str:
r"""
This function returns a octal payload to bypass basic filters.
>>> print(octal_escaping('abc'))
\141\142\143
>>>
"""
return "\\" + "\\".join(f"{char:0>3o}" for char in encode(string))
def unicode(string: Union[str, bytes], leading_zeros: int = 4) -> str:
r"""
This function returns a hexadecimal payload to bypass basic filters.
>>> print(unicode('abc'))
\u{0061}\u{0062}\u{0063}
>>> print(unicode('abc', 8))
\u{00000061}\u{00000062}\u{00000063}
>>>
"""
return (
r"\u{"
+ r"}\u{".join(
hex(char)[2:].rjust(leading_zeros, "0") for char in encode(string)
)
+ "}"
)
def unicode_html_escaping(
string: Union[str, bytes], leading_zeros: int = 4
) -> str:
r"""
This function returns a hexadecimal payload to bypass basic filters.
>>> unicode_html_escaping('abc')
'\u{0061}\u{0062}\u{0063}'
>>> unicode_html_escaping('abc', 8)
'\u{00000061}\u{00000062}\u{00000063}'
>>>
"""
return unicode(string, leading_zeros).replace("\\", "\")
def payloads_encodings(encoding: str, *payloads: Iterable[str]) -> int:
"""
This function encodes payloads.
"""
encodings = {
"ms": ms_sql_char,
"sqlite": sqlite_char,
"oracle": oracle_sql_char,
"oracle sql": oracle_sql_char,
"oracle_sql": oracle_sql_char,
"postgre": postgresql_char,
"postgresql": postgresql_char,
"sql server": ms_sql_char,
"sql_server": ms_sql_char,
"ms sql": ms_sql_char,
"ms_sql": ms_sql_char,
"mysql": mysql_char,
"mariadb": mariadb_sql_char,
"unicode": unicode,
"javascript": javascript_char,
"script": javascript_char,
"html unicode": unicode_html_escaping,
"html_unicode": unicode_html_escaping,
"unicode html": unicode_html_escaping,
"unicode_html": unicode_html_escaping,
"hexa escaping": hexa_escaping,
"hexadecimal escaping": hexa_escaping,
"hexa_escaping": hexa_escaping,
"hexadecimal_escaping": hexa_escaping,
"hexa escape": hexa_escaping,
"hexa_escape": hexa_escaping,
"hexadecimal escape": hexa_escaping,
"hexadecimal_escape": hexa_escaping,
"octal escaping": octal_escaping,
"octal_escaping": octal_escaping,
"octal escape": octal_escaping,
"octal_escape": octal_escaping,
"hexadecimal": hexa_number,
"hexa": hexa_number,
"html": html_decimal,
"html_decimal": html_decimal,
"html decimal": html_decimal,
"html hexadecimal": html_hexadecimal,
"html_hexadecimal": html_hexadecimal,
"html hexa": html_hexadecimal,
"html_hexa": html_hexadecimal,
"url": pourcent_encoding,
"uri": pourcent_encoding,
"pourcent": pourcent_encoding,
"2pourcent": double_pourcent_encoding,
"pourcent2": double_pourcent_encoding,
"double pourcent": double_pourcent_encoding,
"double_pourcent": double_pourcent_encoding,
}
function = encodings.get(encoding.lower())
if encoding == "all":
functions = defaultdict(list)
for name, function in encodings.items():
functions[function].append(repr(name))
for function, names in functions.items():
print(", ".join(names))
for payload in payloads:
print(" ", function(payload))
elif function:
for payload in payloads:
print(function(payload))
else:
print("Error: encoding not found.", file=stderr)
print("Encodings availables: ", list(encodings.values()), file=stderr)
return 5
return 0
def main() -> int:
"""
The main function to use this tool from command line arguments.
"""
if len(argv) == 1:
print(
"USAGES:\n WebPayloadsEncodings [payload]\n WebPayloadsEncodings [encoding] [payloads ...]",
file=stderr,
)
return 1
elif len(argv) == 2:
payloads = [argv[1]]
encoding = "all"
else:
encoding = argv[1]
payloads = argv[2:]
return payloads_encodings(encoding, *payloads)
if __name__ == "__main__":
exit(main())