-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathhelper.py
125 lines (111 loc) · 4.35 KB
/
helper.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
import dbmodule
from dbmodule import *
class Helper:
def __init__(self,args="",commnad=""):
self.args = args
self.commnad = commnad
#process the commnads
def process(self):
if self.commnad == "search":
if not self.args:
dbmodule.lastresults = dbmodule.searchAll()
self.printlastResult()
elif not self.__searchparams():
print i18n.t("help.help_search_error")
else:
dbmodule.lastresults = dbmodule.searchByCriterial(**self.__searchparams())
self.printlastResult()
elif self.commnad == "addfav" and self.args:
dbmodule.createFavorite(**self.__addfavparams())
elif self.commnad == "modfav" and self.args:
dbmodule.updateFavorite(**self.__modfavparams())
elif self.commnad == "delfav" and self.args:
dbmodule.deleteFavorite(**self.__delfavparams())
elif self.commnad == "showfav":
if not self.args:
dbmodule.lastresults = dbmodule.getFavorites()
self.printlastResult(True)
else:
dbmodule.lastresults = dbmodule.getFavorites(**self.__showfavparams())
self.printlastResult(True)
else:
print i18n.t("help.help_command_error")
# Display the last results
def printlastResult(self,fav=False):
if fav == True:
print("\033[1;32m*** {0:40} {1:40}\033[0m".format(*["Name","Ranking"]))
for key,value in dbmodule.lastresults.items():
print("\033[1;32m[+] {0:40} {1:35}\033[0m".format(*[value["name"],value["ranking"]]))
else:
print("\033[1;32m*** {0:40} {1:40}\033[0m".format(*["Name","Author"]))
for key,value in dbmodule.lastresults.items():
print("\033[1;32m[+] {0:40} {1:35}\033[0m".format(*[value["name"],value["author"]]))
# Display the documentation per script
def displayDoc(self):
try:
scriptFile = open(dbmodule.scriptsPath+self.args,'r')
self.__readLines(scriptFile)
except Exception, e:
try:
scriptFile = open(dbmodule.scriptsPath+self.args+".nse",'r')
self.__readLines(scriptFile)
except Exception, e:
print i18n.t("setup.del_fav_error")
finally:
pass
# used for the autocomplete
def resultitems(self):
i = 0
items = []
for k,v in dbmodule.lastresults.items():
items.insert(i,v["name"])
i = i + 1
return items
# private function to set params for search command
def __searchparams(self):
if self.args.find('name:') != -1 or self.args.find('category:') != -1 or self.args.find('author:') != -1:
return self.__setParams()
else:
return False
#private funtion to set params for addfav command
def __addfavparams(self):
if self.args.find('name:') != -1 or self.args.find('ranking:') != -1:
return self.__setParams()
#private funtion to set params for delfav command
def __delfavparams(self):
if self.args.find('name:') != -1:
return self.__setParams()
#private function to set params for modfav command
def __modfavparams(self):
if self.args.find('name:') != -1 or self.args.find('newname:') != -1 or self.args.find('newranking:') != -1:
return self.__setParams()
#private function to set paramas for showfav command
def __showfavparams(self):
if self.args.find('name:') != -1 or self.args.find('ranking:') != -1:
return self.__setParams()
# Set Params validations
def __setParams(self):
argsdic = {}
if len(self.args.split(":")) >= 4:
argsdic.update({
self.args.split(":")[0]:self.args.split(":")[1].split(" ")[0],
self.args.split(":")[1].split(" ")[1]:self.args.split(":")[2].split(" ")[0],
self.args.split(":")[2].split(" ")[1]:self.args.split(":")[3].split(" ")[0]})
elif len(self.args.split(":")) == 3:
argsdic.update({
self.args.split(":")[0]:self.args.split(":")[1].split(" ")[0],
self.args.split(":")[1].split(" ")[1]:self.args.split(":")[2].split(" ")[0]})
elif len(self.args.split(":")) == 2:
argsdic.update({self.args.split(":")[0]:self.args.split(":")[1].split(" ")[0]})
else:
print i18n.t("setup.bad_params")
return argsdic
#Private function to read lines
def __readLines(self,scriptFile):
lines = scriptFile.read().splitlines()
for line in lines:
if line.startswith("--@output") or line.startswith("-- @output"):
break
if not bool('local' in line):
print('\033[1;96m'+line+'\033[0m')
scriptFile.close()