-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathcompressed.py
128 lines (106 loc) · 4.78 KB
/
compressed.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""Creates a compressed (as small as possible) version of TinyTor.
NOTICE:
This was created to be used in EvilOSX (https://github.com/Marten4n6/EvilOSX).
Please DO NOT USE this, instead use pip (as usual)!
"""
__author__ = "Marten4n6"
__license__ = "GPLv3"
import math
from base64 import b64encode
from hashlib import sha256
from os import path
from sys import exit
from zlib import compress
from tinytor import __version__
def convert_size(size_bytes):
"""Converts a byte size to a human readable format.
:type size_bytes: int
:rtype: str
"""
if size_bytes == 0:
return "0B"
size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
i = int(math.floor(math.log(size_bytes, 1024)))
p = math.pow(1024, i)
s = round(size_bytes / p, 2)
return "%s %s" % (s, size_name[i])
def main():
source_path = path.join(path.dirname(__file__), "tinytor.py")
with open(source_path, "r") as input_file:
print("[WARNING] This was created to be used in EvilOSX (https://github.com/Marten4n6/EvilOSX).")
print("[WARNING] Please DO NOT USE this, instead use pip (as usual)!")
print("[INFO] Compressing TinyTor (%s)..." % source_path)
modified_source = ""
previous_line = ""
middle_of_docstring = False
for line in input_file:
if line.strip().startswith("def main():"):
# Skip everything under this, we won't be using it from the command line.
break
elif line.strip().startswith("# "):
# Strip out comments.
# print("[DEBUG] Skipping: " + line.replace("\n", ""))
continue
elif line.strip() == "#":
# Strip out empty comments.
# print("[DEBUG] Skipping: " + line.replace("\n", ""))
continue
elif line.strip().startswith('"""') or middle_of_docstring:
# Strip out docstrings.
if len(line.split('"""')) == 3:
# Two occurrences found, this is a single line docstring.
# print("[DEBUG] Skipping: " + line.replace("\n", ""))
continue
else:
previous_line_last_char = previous_line.replace("\n", "")[(len(previous_line) - 2):]
if line.strip().startswith('"""'):
if previous_line_last_char == ":":
# Start of a docstring.
# print("[DEBUG] Skipping: " + line.replace("\n", ""))
middle_of_docstring = True
previous_line = line
continue
elif middle_of_docstring:
# End of a docstring.
# print("[DEBUG] Skipping: " + line.replace("\n", ""))
middle_of_docstring = False
previous_line = line
continue
if middle_of_docstring:
# print("[DEBUG] Skipping: " + line.replace("\n", ""))
continue
else:
modified_source += line
else:
modified_source += line
previous_line = line
compressed_and_encoded = b64encode(compress(modified_source.encode())).decode()
bytes_hash = sha256()
bytes_hash.update(compressed_and_encoded.encode())
print("[INFO] Old size: " + convert_size(path.getsize(source_path)))
print("[INFO] New size: " + convert_size(len(compressed_and_encoded)))
print("[INFO] Python code to dynamically load and use this library:")
print("======== BEGIN PYTHON CODE ========")
print("from base64 import b64decode")
print("from zlib import decompress")
print("")
print("# This is a compressed version of TinyTor v%s (https://github.com/Marten4n6/TinyTor)." % __version__)
print("# The source code which generated this is also available there.")
print("# It's understandable that you wouldn't trust a random string like this.")
print("# The SHA256 hash of this string is: " + bytes_hash.hexdigest())
print("tor = \"%s\"" % compressed_and_encoded)
print("tor_dict = {}")
print("exec(decompress(b64decode(tor)), tor_dict)")
print("")
print("# Sends a HTTP request over Tor.")
print('exec("tor = TinyTor()", tor_dict)')
print('exec(\'tor.http_get("http://example.onion")\', tor_dict)')
print("======== END PYTHON CODE ========")
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print("\nInterrupted.")
exit(0)