-
Notifications
You must be signed in to change notification settings - Fork 0
/
mkt
676 lines (536 loc) · 21.1 KB
/
mkt
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
#! /bin/python3
import sys,os,shutil
isDebug = False
# mkt help and version
def MKT_HELP():
print(r"""
__ __ .__
_____ | | ___/ |_ ____ ____ | | ______
/ \| |/ /\ __\/ _ \ / _ \| | / ___/
| Y Y \ < | | ( <_> | <_> ) |__\___ \
|__|_| /__|_ \ |__| \____/ \____/|____/____ >
\/ \/ \/
By @manesec, Version: 20241011 Beta
https://github.com/manesec/maketoolkit
Usage:
# Base install and uninstall tools.
mkt install [<tools name>, <.mkt files>]
mkt uninstall/remove <tools name>
mkt reinstall <tools name>
mkt upgrade <tools name>
mkt cd <tools name>
# Uhm, No Recommand.
mkt upgrade all
List:
# List install the tools name.
mkt list install
mkt list installable
Script:
# List build-in script
mkt script list
mkt script info
mkt script help
# Run the script
mkt script <script_name>
mkt script <script_index>
DB and res:
mkt db installable
mkt db install <db_name>
mkt db uninstall <db_name>
mkt db upgrade <db_name> / all
mkt db reindex
Search DB and find in file:
mkt s(earch) <string>
mkt f(ind) <string>
Other:
# Update the mkt core, will delete all the tools,
# You need to reinstall all the tools ;(
sudo mkt-update""")
def MKT_CLEARUP():
""" delete all `__pycache__` files """
CheckUID()
os.system("find /var/lib/mkt/Tools/ -type d -name '__pycache__' | xargs -I $ rm -rf $")
os.system("chmod -R 755 /var/lib/mkt/")
# ==========================================================================
# Parser
# ==========================================================================
def Debug(mess):
""" simple Debug Options """
if isDebug:
print(mess)
def Parser_DB_String(inputString):
""" parser user input to db """
input_str = inputString.strip()
# pre-load installed tools
toolsList = []
for path,subpaths,files in os.walk("/var/lib/mkt/Res/Source"):
for file in files:
if file[-3:] == ".py":
module_name = (path + "/"+ file)[:-3]
module_name = module_name.replace("/var/lib/mkt/Res/Source/","")
toolsList.append(module_name)
Debug("toolsList => " + str(toolsList))
finded_Tools = []
for tools in toolsList:
if (input_str.lower() in tools.lower()):
Debug("Hit => " + tools)
finded_Tools.append(tools)
if (len(finded_Tools) == 0):
print("[ERROR] No found !")
sys.exit(1)
if (len(finded_Tools) > 1):
# When more than one
Debug(finded_Tools)
def show_list():
print(" Found more than one db, please select.")
print("--------------------------------------------------------")
for index,tools in enumerate(finded_Tools):
print("[%s] %s" % (index,tools))
read_input = None
show_list()
print("Tips: input 'l' can list again")
while True:
user_input = input(" Select number to install : ").strip()
if user_input == "l" : show_list()
if user_input.isdigit():
if (int(user_input) >= 0) and (int(user_input) <= len(finded_Tools) - 1):
print ("[*] Selected: %s" % (finded_Tools[int(user_input)]))
abs_file = "/var/lib/mkt/Res/Source/" + finded_Tools[int(user_input)] + ".py"
break
elif (len(finded_Tools) == 1):
# When have one
Debug("Return => " + finded_Tools[0])
abs_file = "/var/lib/mkt/Res/Source/" + finded_Tools[0] + ".py"
Debug("abs_file =>" + abs_file)
return abs_file
def Parser_DB_String_When_Installed(inputString):
""" parser user input to db """
input_str = inputString.strip()
# pre-load installed tools
toolsList = []
for path,subpaths,files in os.walk("/var/lib/mkt/Res/Install"):
for file in files:
if file[-3:] == ".py":
module_name = (path + "/"+ file)[:-3]
module_name = module_name.replace("/var/lib/mkt/Res/Install/","")
toolsList.append(module_name)
Debug("toolsList => " + str(toolsList))
finded_Tools = []
for tools in toolsList:
if (input_str.lower() in tools.lower()):
Debug("Hit => " + tools)
finded_Tools.append(tools)
if (len(finded_Tools) == 0):
print("[ERROR] No found !")
sys.exit(1)
if (len(finded_Tools) > 1):
# When more than one
Debug(finded_Tools)
def show_list():
print(" Found more than one db, please select.")
print("--------------------------------------------------------")
for index,tools in enumerate(finded_Tools):
print("[%s] %s" % (index,tools))
read_input = None
show_list()
print("Tips: input 'l' can list again")
while True:
user_input = input(" Select number to install : ").strip()
if user_input == "l" : show_list()
if user_input.isdigit():
if (int(user_input) >= 0) and (int(user_input) <= len(finded_Tools) - 1):
print ("[*] Selected: %s" % (finded_Tools[int(user_input)]))
abs_file = "/var/lib/mkt/Res/Install/" + finded_Tools[int(user_input)] + ".py"
break
elif (len(finded_Tools) == 1):
# When have one
Debug("Return => " + finded_Tools[0])
abs_file = "/var/lib/mkt/Res/Install/" + finded_Tools[0] + ".py"
Debug("abs_file =>" + abs_file)
return abs_file
# ==========================================================================
# Base
# ==========================================================================
# mkt install <module>
def MKT_INSTALL():
input_str = None
if (len(sys.argv) != 3):
input_str = None
else:
input_str = sys.argv[2]
from importlib.machinery import SourceFileLoader
SourceFileLoader("manesec","/var/lib/mkt/Bin/func_tools.py").load_module().install(input_str)
# mkt uninstall <module>
def MKT_UNINSTALL():
input_str = None
if (len(sys.argv) != 3):
input_str = None
else:
input_str = sys.argv[2]
from importlib.machinery import SourceFileLoader
SourceFileLoader("manesec","/var/lib/mkt/Bin/func_tools.py").load_module().uninstall(input_str)
# mkt reinstall <module>
def MKT_REINSTALL():
input_str = None
if (len(sys.argv) != 3):
input_str = None
else:
input_str = sys.argv[2]
from importlib.machinery import SourceFileLoader
SourceFileLoader("manesec","/var/lib/mkt/Bin/func_tools.py").load_module().reinstall(input_str)
# mkt upgrade routing
def MKT_UPGRADE():
input_str = None
if (len(sys.argv) != 3):
input_str = None
else:
input_str = sys.argv[2]
from importlib.machinery import SourceFileLoader
SourceFileLoader("manesec","/var/lib/mkt/Bin/func_tools.py").load_module().upgrade(input_str)
# mkt upgrade all
def MKT_UPGRADE_ALL():
from importlib.machinery import SourceFileLoader
SourceFileLoader("manesec","/var/lib/mkt/Bin/func_tools.py").load_module().upgradeAll()
# mkt cd <module>
def MKT_CD_TOOLD():
input_str = None
if (len(sys.argv) != 3):
input_str = None
else:
input_str = sys.argv[2]
from importlib.machinery import SourceFileLoader
search_bin = "/var/lib/mkt/Bin/func_cd.py"
SourceFileLoader("mkt_search",search_bin).load_module().findAndGoToToolsDir(input_str)
# ==========================================================================
# List
# ==========================================================================
# mkt list install module
def MKT_LIST():
def functionHelp():
print("[ERROR] Invalid parameter")
print(" Usage: mkt list <install,installable>")
sys.exit(1)
if (len(sys.argv) != 3):
functionHelp()
SelectFunction = {
"install" : MKT_LIST_INSTALLED,
"installable": MKT_LIST_INSTALLABLE,
}
SelectFunction.get(sys.argv[2],functionHelp)()
def MKT_LIST_INSTALLED():
print("[*] Installed Tools List:")
output_list = []
for path,subpaths,files in os.walk("/var/lib/mkt/Tools/Install/"):
for file in files:
if file[-4:] == ".mkt":
module_name = (path + "/"+ file)[:-4]
module_name = module_name.replace("/var/lib/mkt/Tools/Install/","")
output_list.append(module_name)
if (len(output_list) == 0):
print("[!] There are no tools installed.")
sys.exit(1)
output_list.sort()
for module_name in output_list:
print(" %s" % module_name )
def MKT_LIST_INSTALLABLE():
print("[*] Installable Tools List:")
output_list = []
for path,subpaths,files in os.walk("/var/lib/mkt/Tools/Source/"):
for file in files:
if file[-4:] == ".mkt":
module_name = (path + "/"+ file)[:-4]
module_name = module_name.replace("/var/lib/mkt/Tools/Source/","")
output_list.append(module_name)
if (len(output_list) == 0):
print("[!] There are no tools can be install.")
sys.exit(1)
output_list.sort()
for module_name in output_list:
print(" %s" % module_name )
# ==========================================================================
# Script
# ==========================================================================
# mkt script
def MKT_SCRIPT():
if (len(sys.argv) != 3):
from importlib.machinery import SourceFileLoader
search_bin = "/var/lib/mkt/Bin/func_script.py"
SourceFileLoader("search_whoosh",search_bin).load_module().useScript(None)
return
SelectFunction = {
"help" : MKT_SCRIPT_HELP,
"list" : MKT_SCRIPT_LIST,
"ls" : MKT_SCRIPT_LIST,
}
SelectFunction.get(sys.argv[2],MKT_SCRIPT_USE)()
def MKT_SCRIPT_HELP():
print("""Example for using mkt script command:
# List available script:
$ mkt script list
$ mkt script info
# Use the script:
$ mkt script Install/GoSpider.py
$ mkt script 3""")
def MKT_SCRIPT_USE():
input_str = sys.argv[2]
from importlib.machinery import SourceFileLoader
search_bin = "/var/lib/mkt/Bin/func_script.py"
SourceFileLoader("func_script",search_bin).load_module().useScript(input_str)
MKT_CLEARUP()
def MKT_SCRIPT_LIST():
from importlib.machinery import SourceFileLoader
search_bin = "/var/lib/mkt/Bin/func_script.py"
SourceFileLoader("func_script",search_bin).load_module().listScript()
# ==========================================================================
# Description
# ==========================================================================
# mkt desc tool xxxxx
# mkt desc db xxxxx
# mkt desc script xxxxx
def MKT_DESCRIPTION():
def functionHelp():
print("[ERROR] Invalid parameter")
print(" Usage: mkt desc <tool_install, tool_uninstall, db_install, db_uninstall, script> <module_name>")
sys.exit(0)
if ((len(sys.argv) < 4)):
functionHelp()
print(sys.argv[3])
SelectFunction = {
"tool_install": "/var/lib/mkt/Tools/Source/" + sys.argv[3] + ".mkt",
"tool_uninstall": "/var/lib/mkt/Tools/Install/" + sys.argv[3] + ".mkt",
"script" : "/var/lib/mkt/Script/" + sys.argv[3] + ".py",
}
if (sys.argv[2] not in SelectFunction.keys()):
functionHelp()
if os.path.exists(SelectFunction[sys.argv[2]]):
from importlib.machinery import SourceFileLoader
SourceFileLoader("mkt_desc",SelectFunction[sys.argv[2]]).load_module().description()
else:
print("[Error] File: %s not exist. " % (SelectFunction[sys.argv[2]]))
# ==========================================================================
# Res and DB
# ==========================================================================
def MKT_DB():
def functionHelp():
print("[ERROR] Invalid parameter")
print(" Usage: mkt db <list, installable, install <db_name>, uninstall <db_name>, reindex>")
sys.exit(0)
if not ((len(sys.argv) ==3) or len(sys.argv) ==4):
functionHelp()
SelectFunction = {
"install" : MKT_DB_INSTALL,
"uninstall" : MKT_DB_UNINSTALL,
"remove" : MKT_DB_UNINSTALL,
"rm" : MKT_DB_UNINSTALL,
"upgrade": MKT_DB_UPGRADE,
"list" : MKT_DB_LIST,
"ls" : MKT_DB_LIST,
"installable":MKT_DB_INSTALL_ABLE,
"reindex": MKT_DB_REINDEX
}
SelectFunction.get(sys.argv[2],functionHelp)()
def MKT_DB_REINDEX():
from importlib.machinery import SourceFileLoader
search_bin = "/var/lib/mkt/Bin/search_whoosh.py"
SourceFileLoader("search_whoosh",search_bin).load_module().ReIndex()
def MKT_DB_INSTALL():
import os
if (len(sys.argv) !=4):
print("[ERROR] Invalid parameter")
print(" Usage: mkt db install <db_name>")
sys.exit(1)
CheckUID()
input_str = sys.argv[3]
if (input_str.strip() == ""):
print("[ERROR] Input can not be null.")
sys.exit(0)
abs_file = Parser_DB_String(input_str)
relative_full_path = abs_file.replace("/var/lib/mkt/Res/Source/","")
relative_path = relative_full_path.replace(os.path.basename(abs_file),"")
Debug("abs_file =>" + abs_file)
Debug("relative_full_path =>" + relative_full_path)
Debug("relative_path =>" + relative_path)
if (relative_path.strip() == ""):
relative_path = "."
Debug("relative_path =>" + relative_path)
from pathlib import Path
path = Path( os.path.abspath( "/var/lib/mkt/Res/Install/" + relative_path ) )
Debug("Check and mkdir =>" + str(path))
path.mkdir(parents=True, exist_ok=True)
new_path = "/var/lib/mkt/Res/Install/" + relative_path + "/" + os.path.basename(abs_file)
new_path = os.path.abspath(new_path)
Debug("Move %s -> %s" % (abs_file,new_path))
shutil.move(abs_file,new_path)
from importlib.machinery import SourceFileLoader
SourceFileLoader("manesec",new_path).load_module().Install()
MKT_CLEARUP()
def MKT_DB_UNINSTALL():
if (len(sys.argv) !=4):
print("[ERROR] Invalid parameter")
print(" Usage: mkt db uninstall <db_name>")
sys.exit(0)
CheckUID()
input_str = sys.argv[3]
if (input_str.strip() == ""):
print("[ERROR] Input can not be null.")
sys.exit(1)
abs_file = Parser_DB_String_When_Installed(input_str)
relative_full_path = abs_file.replace("/var/lib/mkt/Res/Install/","")
relative_path = relative_full_path.replace(os.path.basename(abs_file),"")
Debug("abs_file =>" + abs_file)
Debug("relative_full_path =>" + relative_full_path)
Debug("relative_path =>" + relative_path)
if (relative_path.strip() == ""):
relative_path = "."
Debug("relative_path =>" + relative_path)
from pathlib import Path
path = Path( os.path.abspath( "/var/lib/mkt/Res/Source/" + relative_path ) )
Debug("Check and mkdir =>" + str(path))
path.mkdir(parents=True, exist_ok=True)
new_path = "/var/lib/mkt/Res/Source/" + relative_path + "/" + os.path.basename(abs_file)
new_path = os.path.abspath(new_path)
Debug("Move %s -> %s" % (abs_file,new_path))
shutil.move(abs_file,new_path)
from importlib.machinery import SourceFileLoader
SourceFileLoader("manesec",new_path).load_module().Uninstall()
MKT_CLEARUP()
def MKT_DB_LIST():
print("[*] List all installed db ...")
output_list = []
for path,_, files in os.walk("/var/lib/mkt/Res/Install"):
for file in files:
if file[-3:] == ".py":
abs_path = (path + "/"+ file)
module_name = abs_path[:-3]
module_name = module_name.replace("/var/lib/mkt/Res/Install/","")
output_list.append(module_name)
if (len(output_list) == 0):
print("[!] There are no search DB installed.")
sys.exit(1)
output_list.sort()
for module_name in output_list:
print(" %s" % module_name )
def MKT_DB_INSTALL_ABLE():
print("[*] List all installable db ...")
output_list = []
for path,_, files in os.walk("/var/lib/mkt/Res/Source"):
for file in files:
if file[-3:] == ".py":
abs_path = (path + "/"+ file)
module_name = abs_path[:-3]
module_name = module_name.replace("/var/lib/mkt/Res/Source/","")
output_list.append(module_name)
if (len(output_list) == 0):
print("[!] There are no search DB can be install.")
sys.exit(1)
output_list.sort()
for module_name in output_list:
print(" %s" % module_name )
def MKT_DB_UPGRADE():
if (len(sys.argv) != 4):
print("[ERROR] Invalid parameter")
print(" Usage: mkt db upgrade <name>")
sys.exit(1)
CheckUID()
if (sys.argv[3] == "all") :
MKT_DB_UPGRADE_ALL()
else:
MKT_DB_UPGRADE_TOOLS()
def MKT_DB_UPGRADE_ALL():
print("[*] Upgrade all the db ...")
nolist = True
for path,subpaths,files in os.walk("/var/lib/mkt/Res/Install/"):
for file in files:
if file[-3:] == ".py":
abs_path = (path + file)
module_name = abs_path[:-3]
module_name = module_name.replace("/var/lib/mkt/Res/Install/","")
print("Pre-running %s ..." %module_name )
nolist = False
from importlib.machinery import SourceFileLoader
SourceFileLoader("manesec",abs_path).load_module().Upgrade()
MKT_CLEARUP()
if (nolist):
print("[!] There are no db installed.")
def MKT_DB_UPGRADE_TOOLS():
input_str = sys.argv[3]
if (input_str.strip() == ""):
print("[ERROR] Input can not be null.")
sys.exit(1)
abs_paths = os.path.abspath("/var/lib/mkt/Res/Install/" + input_str + ".py")
if not (os.path.exists(abs_paths) and (abs_paths.startswith("/var/lib/mkt/Res/Install/"))):
print("[ERROR] No such db.")
sys.exit(1)
from importlib.machinery import SourceFileLoader
SourceFileLoader("manesec",abs_paths).load_module().Upgrade()
MKT_CLEARUP()
# ==========================================================================
# Search Module
# ==========================================================================
def MKT_DB_SEARCH():
from importlib.machinery import SourceFileLoader
search_bin = "/var/lib/mkt/Bin/func_search.py"
SourceFileLoader("mkt_search",search_bin).load_module().MKT_DB_SEARCH()
def MKT_FIND_SEARCH():
if (len(sys.argv) < 3):
sys.argv.append(None)
input_str = sys.argv[2:]
from importlib.machinery import SourceFileLoader
search_bin = "/var/lib/mkt/Bin/func_find.py"
SourceFileLoader("mkt_search",search_bin).load_module().findinFiles(input_str)
def MKT_FIND_AND_COPY():
if (len(sys.argv) < 3):
sys.argv.append(None)
if (len(sys.argv) < 4):
sys.argv.append(None)
input_str = sys.argv[2:4]
from importlib.machinery import SourceFileLoader
search_bin = "/var/lib/mkt/Bin/func_copy.py"
SourceFileLoader("mkt_search",search_bin).load_module().copyAndfindinFiles(input_str)
# ==========================================================================
# Main
# ==========================================================================
def CheckUID():
if os.getuid() != 0 :
print("[ERROR] Please run as root user.")
sys.exit(1)
def MKT_DOC_SERVER():
os.chdir("/var/lib/mkt/ResServer/")
os.system("python3 /var/lib/mkt/ResServer/main.py")
if __name__ == "__main__" :
if (len(sys.argv) == 1):
MKT_HELP()
sys.exit()
SelectFunction = {
"help" : MKT_HELP,
"list": MKT_LIST,
"script": MKT_SCRIPT,
"db":MKT_DB,
"reinstall": MKT_REINSTALL,
"install": MKT_INSTALL,
"remove": MKT_UNINSTALL,
"uninstall": MKT_UNINSTALL,
"update":MKT_UPGRADE,
"upgrade":MKT_UPGRADE,
"update-all":MKT_UPGRADE_ALL,
"upgrade-all":MKT_UPGRADE_ALL,
# doc server
"doc": MKT_DOC_SERVER,
"document-server": MKT_DOC_SERVER,
"doc-server": MKT_DOC_SERVER,
# description
"desc" : MKT_DESCRIPTION,
# go to tools dir
"cd": MKT_CD_TOOLD,
"locate": MKT_CD_TOOLD,
# Search function
"s":MKT_DB_SEARCH,
"search": MKT_DB_SEARCH,
# Find in file
"f" : MKT_FIND_SEARCH,
"find" :MKT_FIND_SEARCH,
# Find in file
"cp" :MKT_FIND_AND_COPY,
"copy" : MKT_FIND_AND_COPY
}
SelectFunction.get(sys.argv[1],MKT_HELP)()