-
Notifications
You must be signed in to change notification settings - Fork 0
/
Junky.py
328 lines (304 loc) · 14.4 KB
/
Junky.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
import os
import random
import string
import time
class Junky():
def __init__(self, path, file, rejunk):
self.file = os.path.join(path + '\\' + file)
self.filename = file
self.rejunk = rejunk
def junkify(self):
file = open(self.file, "r")
file_raw = file.readlines()
file.close()
delete = junk_token = skip_file = False
new_file = []
for line in file_raw:
line = line.strip()
if line == "/* SKIP FILE */":
skip_file = True
break
elif line == "/* This file has been junkified by JunkyPy https://github.com/Kixiron/JunkyPy */":
junk_token = True
elif line == "/* INSERT JUNK FUNCTION */":
new_file.append(self.generate_function())
elif line == "/* INSERT JUNK CLASS */":
new_file.append(self.generate_class())
elif line == "/* INSERT JUNK PRIVATE VARIABLES */":
new_file.append(self.generate_private_vars())
elif line == "/* INSERT JUNK IMPORTS */":
new_file.append(self.generate_imports())
elif line == "/* START JUNK */":
delete = True
elif line == "/* END JUNK FUNCTION */":
new_file.append("/* INSERT JUNK FUNCTION */")
delete = False
elif line == "/* END JUNK CLASS */":
new_file.append("/* INSERT JUNK CLASS */")
delete = False
elif line == "/* END JUNK PRIVATE VARS */":
new_file.append("/* INSERT JUNK PRIVATE VARIABLES */")
delete = False
elif line == "/* END JUNK IMPORTS */":
new_file.append("/* INSERT JUNK IMPORTS */")
delete = False
else:
if delete == False:
new_file.append(line)
if skip_file:
return "Skipped File {}".format(self.file)
pass
else:
if junk_token == False:
new_file.insert(0, "/* This file has been junkified by JunkyPy https://github.com/Kixiron/JunkyPy */")
return_msg = "Junked File {}".format(self.filename)
else:
return_msg = "Un-junked File {}".format(self.filename)
file = open(self.file, "w")
file.close()
file = open(self.file, "w")
file.write('\n'.join(new_file).replace("}}", "}").replace("{{", "{"))
file.close()
if self.rejunk and junk_token:
self.junkify_safe()
return_msg = "Un-junked and re-junked {}".format(self.filename)
return return_msg
def junkify_safe(self):
file = open(self.file, "r")
file_raw = file.readlines()
file.close()
junk_token = skip_file = False
new_file = []
for line in file_raw:
line = line.strip()
if line == "/* This file has been junkified by JunkyPy https://github.com/Kixiron/JunkyPy */":
junk_token = True
elif line == "/* INSERT JUNK FUNCTION */":
new_file.append(self.generate_function())
elif line == "/* INSERT JUNK CLASS */":
new_file.append(self.generate_class())
elif line == "/* INSERT JUNK PRIVATE VARIABLES */":
new_file.append(self.generate_private_vars())
elif line == "/* INSERT JUNK IMPORTS */":
new_file.append(self.generate_imports())
else:
new_file.append(line)
else:
if junk_token == False:
new_file.insert(0, "/* This file has been junkified by JunkyPy https://github.com/Kixiron/JunkyPy */")
file = open(self.file, "w")
file.close()
file = open(self.file, "w")
file.write('\n'.join(new_file).replace("}}", "}").replace("{{", "{"))
file.close()
def generate_class(self):
junk = "/* START JUNK */\n"
junk += "class {}_class\n".format(self.generate_var())
junk += "{{\n"
for i in range(random.randint(1, 3)):
junk += self.generate_global_var()
for i in range(random.randint(1, 3)):
junk += self.generate_enum()
for i in range(random.randint(1, 2)):
junk += self.generate_class_function()
junk += "}}\n"
junk += "/* END JUNK CLASS */"
return junk
def generate_class_function(self):
junk = "void {}_func()\n".format(self.generate_var())
junk += "{{\n"
for i in range(random.randint(1, 3)):
junk += self.generate_if()
for i in range(random.randint(0, 2)):
junk += self.generate_for()
junk += "}}\n"
return junk
def generate_function(self):
junk = "/* START JUNK */\n"
junk += "void {}_func()\n".format(self.generate_var())
junk += "{{\n"
for i in range(random.randint(1, 3)):
junk += self.generate_if()
for i in range(random.randint(0, 3)):
junk += self.generate_for()
junk += "}}\n"
junk += "/* END JUNK FUNCTION */"
return junk
def generate_imports(self):
imports = [ "System", "System.CodeDom", "System.Collections", "System.ComponentModel", "System.Configuration", "System.Deployment", "System.Diagnostics", "System.Diagnostics.CodeAnalysis", "System.Diagnostics.Contracts", "System.Diagnostics.Eventing", "System.Diagnostics.Tracing", "System.Diagnostics.PerformanceData", "System.Dynamic", "System.Globalization", "System.IO", "System.Linq", "System.Management", "System.Media", "System.Net", "System.Reflection", "System.Timers","System.Timers","System.Threading","System.Text","System.Text.RegularExpressions", "System.Security", "System.Runtime", "System.Resources", "System.Windows" ]
junk = "/* START JUNK */\n"
for i in range(random.randint(1, 5)):
junk += "using " + random.choice(imports) + ";\n"
junk += "/* END JUNK IMPORTS */"
return junk
def generate_if(self):
operand_types = [ "==", "<=", ">=", ">", "<", "!=" ]
conditionals = [ "&&", "||" ]
values = [ "int", "uint", "long", "ulong", "byte", "short", "bool" ]
condition = ""
for i in range(random.randint(1, 3)):
condition += " ((ulong){} {} {}) ".format(self.generate_ulong(), random.choice(operand_types), self.generate_ulong())
condition = condition.replace(" ", " " + random.choice(conditionals) + " ").strip()
junk = "if ({})".format(condition)
junk += "{{\n"
for i in range(random.randint(1, 4)):
value = random.choice(values)
if value == "int":
junk += "{} {} = {};\n".format(value, self.generate_var() , self.generate_int())
elif value == "uint":
junk += "{} {} = {};\n".format(value, self.generate_var() , self.generate_uint())
elif value == "long":
junk += "{} {} = {};\n".format(value, self.generate_var() , self.generate_long())
elif value == "ulong":
junk += "{} {} = {};\n".format(value, self.generate_var() , self.generate_ulong())
elif value == "byte":
junk += "{} {} = {};\n".format(value, self.generate_var() , self.generate_byte())
elif value == "short":
junk += "{} {} = {};\n".format(value, self.generate_var() , self.generate_short())
elif value == "bool":
junk += "{} {} = {};\n".format(value, self.generate_var() , self.generate_bool())
for i in range(random.randint(0, 2)):
junk += self.generate_safe_for()
junk += "}}\n"
return junk
def generate_safe_if(self):
operand_types = [ "==", "<=", ">=", ">", "<", "!=" ]
conditionals = [ "&&", "||" ]
values = [ "int", "uint", "long", "ulong", "byte", "short", "bool" ]
condition = ""
for i in range(random.randint(1, 4)):
condition += " ((ulong){} {} {}) ".format(self.generate_ulong(), random.choice(operand_types), self.generate_ulong())
condition = condition.replace(" ", " " + random.choice(conditionals) + " ").strip()
junk = "if ({})".format(condition)
junk += "{{\n"
for i in range(random.randint(1, 4)):
value = random.choice(values)
if value == "int":
junk += "{} {} = {};\n".format(value, self.generate_var() , self.generate_int())
elif value == "uint":
junk += "{} {} = {};\n".format(value, self.generate_var() , self.generate_uint())
elif value == "long":
junk += "{} {} = {};\n".format(value, self.generate_var() , self.generate_long())
elif value == "ulong":
junk += "{} {} = {};\n".format(value, self.generate_var() , self.generate_ulong())
elif value == "byte":
junk += "{} {} = {};\n".format(value, self.generate_var() , self.generate_byte())
elif value == "short":
junk += "{} {} = {};\n".format(value, self.generate_var() , self.generate_short())
elif value == "bool":
junk += "{} {} = {};\n".format(value, self.generate_var() , self.generate_bool())
junk += "}}\n"
return junk
def generate_for(self):
operand_types = [ "<=", ">=", ">", "<" ]
for_var = self.generate_var()
junk = "for (var {} = 0; (uint){} {} {}; {}++)\n".format(for_var, for_var, random.choice(operand_types), self.generate_ulong(), for_var)
junk += "{{\n"
for i in range(random.randint(1, 3)):
junk += self.generate_if()
junk += "}}\n"
return junk
def generate_safe_for(self):
operand_types = [ "<=", ">=", ">", "<" ]
for_var = self.generate_var()
junk = "for (var {} = 0; (uint){} {} {}; {}++)\n".format(for_var, for_var, random.choice(operand_types), self.generate_ulong(), for_var)
junk += "{{\n"
for i in range(random.randint(1, 2)):
junk += self.generate_safe_if()
junk += "}}\n"
return junk
def generate_enum(self):
values = [ "int", "uint", "long", "ulong", "byte", "short" ]
junk = "[Flags]\n"
value = random.choice(values)
junk += "private enum {}_enum : {}\n".format(self.generate_var(), value)
junk += "{{\n"
for i in range(random.randint(1, 10)):
if value == "int":
generator = self.generate_int()
elif value == "uint":
generator = self.generate_uint()
elif value == "long":
generator = self.generate_long()
elif value == "ulong":
generator = self.generate_ulong()
elif value == "byte":
generator = self.generate_byte()
elif value == "short":
generator = self.generate_short()
junk += "{} = {},\n".format(self.generate_var(), generator)
junk += "}}\n"
return junk
def generate_private_vars(self):
junk = "/* START JUNK */\n"
for i in range(random.randint(1, 7)):
junk += self.generate_global_var()
junk += "/* END JUNK PRIVATE VARS */"
return junk
def generate_global_var(self):
var_types = [ "string", "string[]", "int", "int[]", "long", "long[]", "ulong", "ulong[]", "UIntPtr", "UIntPtr[]", "IntPtr", "IntPtr[]", "bool", "bool[]", "sbyte", "sbyte[]", "short", "short[]", "byte", "byte[]", "ushort", "ushort[]", "uint", "uint[]", "char", "char[]", "float", "float[]", "double", "double[]", "decimal", "decimal[]", "object", "object[]" ]
value = random.choice(var_types)
if value == "int":
return "private static {} {} = {};\n".format(value, self.generate_var() , self.generate_int())
elif value == "uint":
return "private static {} {} = {};\n".format(value, self.generate_var() , self.generate_uint())
elif value == "long":
return "private static {} {} = {};\n".format(value, self.generate_var() , self.generate_long())
elif value == "ulong":
return "private static {} {} = {};\n".format(value, self.generate_var() , self.generate_ulong())
elif value == "byte":
return "private static {} {} = {};\n".format(value, self.generate_var() , self.generate_byte())
elif value == "short":
return "private static {} {} = {};\n".format(value, self.generate_var() , self.generate_short())
elif value == "bool":
return "private static {} {} = {};\n".format(value, self.generate_var() , self.generate_bool())
else:
return "private static {} {};\n".format(value, self.generate_var())
def generate_var(self):
return ''.join([random.choice(string.ascii_letters) for n in range(random.randint(5, 50))])
def generate_int(self):
return random.randint(-2147483647, 2147483647)
def generate_uint(self):
return random.randint(0, 4294967295)
def generate_long(self):
return random.randint(0, 9223372036854775807)
def generate_ulong(self):
return random.randint(0, 18446744073709551615)
def generate_byte(self):
return random.randint(0, 255)
def generate_short(self):
return random.randint(-32768, 32768)
def generate_bool(self):
return random.choice([ "true", "false" ])
def main():
path = input("File Directory:\n")
if os.path.exists(path) == False:
print("That isn't a valid path")
exit()
rejunk = False
rejunk_string = input("Do you want to re-junk already junked files? [yes/no]\n")
if rejunk_string.lower().strip() == "yes" or rejunk_string.lower().strip() == "y":
rejunk = True
print("Files will be re-junked")
else:
rejunk = False
print("Files will not be re-junked")
file_list = []
random.seed()
for file in os.listdir(path):
if file.endswith(".cs"):
if file.endswith(".csproj"):
pass
file_list.append(file)
if len(file_list) == 1:
print("{} file found".format(len(file_list)))
elif len(file_list) < 0:
print("No files found, quitting")
exit()
else:
print("{} files found".format(len(file_list)))
for file in file_list:
junker = Junky(path, file, rejunk)
status = junker.junkify()
print(status)
main()