-
Notifications
You must be signed in to change notification settings - Fork 27
/
kali.py
executable file
·326 lines (268 loc) · 10.7 KB
/
kali.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
#!/usr/bin/env python3
import os
import os.path
import sys
import subprocess
import requests
import signal
from bs4 import BeautifulSoup, SoupStrainer
import re
# global constants
REMOTE_URL='git://git.kali.org/packages/{PACKAGE}.git'
PACKAGE_FOLDER='dist/'
DESCRIPTION_EXTRACT_MAX_LENGTH = 100
# ##################################################################
# DATA & AUXILIARY FUNCTIONS
# ##################################################################
import data
import helpers
# ##################################################################
# HELPER FUNCTIONS
# ##################################################################
# Helper that handles Ctrl-D
def readInput(str):
print(str)
line = sys.stdin.readline()
if line:
line = line.replace("\r", "").replace("\n", "")
return line
else: # user pressed C-D, i.e. stdin has been
print("Quitting.")
sys.exit(1)
#register the Ctrl-C and others to have a clean exit
def handleInterrupts():
def signal_handler(signal, frame):
print("Quitting.")
sys.exit(1)
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGABRT, signal_handler)
signal.signal(signal.SIGFPE, signal_handler)
signal.signal(signal.SIGILL, signal_handler)
signal.signal(signal.SIGSEGV, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
# checks if a program is installed system-wide by running it in a subprocess and checking for error
def isInstalledWithSystemPM(program):
try:
# pipe output to /dev/null for silence
null = open("/dev/null", "w")
res = subprocess.Popen(["which", program], stdout=subprocess.PIPE, stderr=null)
null.close()
o = res.stdout.read().decode("utf8")
if o == "":
return False
return True
except OSError:
return False
# checks if a program is "installed" locally (in dist/)
def isInstalledWithGitLocally(package):
dirName = PACKAGE_FOLDER+package
return os.path.isdir(dirName)
# if git is not installed, exit 1
def isGitInstalled():
if not isInstalledWithSystemPM("git"):
print("git is required for this script to work. Please install it manually, e.g.:")
print(" $ sudo apt-get install git")
print(" or")
print(" $ sudo dnf install git")
print(" etc.")
print("Exiting, status 1.")
sys.exit(1)
# calls git clone, and wait for exit
def gitClone(repo, localDir):
try:
# pipe output to /dev/null for silence
null = open("/dev/null", "w")
subprocess.call(["git", "clone", repo, localDir])
null.close()
except OSError:
print("Could not clone...")
print("Exiting, status 1.")
sys.exit(1)
# main function that checks if we need to clone a package, if so, run the post-install scripts
def installIfNeeded(package):
dirName = PACKAGE_FOLDER+package
if isInstalledWithSystemPM(package):
print(package, "seems already installed. Skipping")
return
print("Testing if", package, "exists locally...")
if not isInstalledWithGitLocally(package) :
url = REMOTE_URL.replace("{PACKAGE}", package)
if package in data.specialGitURL:
url = data.specialGitURL[package]
print("Using special URL", url)
print("Not found, gonna clone in", dirName)
gitClone(url, dirName)
if package in data.postInstall:
print("Found post-install script(s)")
for s in data.postInstall[package]:
os.system("cd " + dirName + " && " + s)
# main function that tries to run a program (possibly cloning it before)
def run(package):
#if package is already installed on the system via package manager, just call it
if isInstalledWithSystemPM(package):
print(package, "seems already installed system-wide, calling it")
os.system(package)
#or, maybe clone the git, and run it
else:
installIfNeeded(package)
# if we know how to run it, call the command
if package in data.runCmds:
print("Running", package)
os.system(data.runCmds[package])
#if we don't, try to guess
else:
baseName = PACKAGE_FOLDER+package+"/"+package
if os.path.isfile(baseName+".sh"):
print("Found an executable:", baseName+".sh", "running it... (Ctrl-C to exit)")
os.system(baseName+".sh")
elif os.path.isfile(baseName+".py"):
print("Found an executable:", baseName+".py", "running it... (Ctrl-C to exit)")
os.system(baseName+".py")
elif os.path.isfile(baseName+".pl"):
print("Found an executable:", baseName+".pl", "running it... (Ctrl-C to exit)")
os.system(baseName+".pl")
#finally, we give up
else:
print("Please run the program in", PACKAGE_FOLDER, package)
# ##################################################################
# MAIN LOGIC
# ##################################################################
def printHeader():
print (''' _ _ __ __ ____ ____ _____ _____ __ ___
( )/ ) /__\ ( ) (_ _)___(_ _)( _ )( _ )( ) / __)
) ( /(__)\ )(__ _)(_(___) )( )(_)( )(_)( )(__ \__
(_)\_)(__)(__)(____)(____) (__) (_____)(_____)(____)(___/''')
def printTableHeader(longestPackageName):
print(" N°| Name"+ ' ' * (longestPackageName - 4)+" | Installed | Description")
print('---|'+'-' * (longestPackageName+3) + '|-----------|' + '-' * (DESCRIPTION_EXTRACT_MAX_LENGTH+1))
def printPackageLine(id, p, longestPackageName, highlightTerm):
#pad for 0-9
num = str(id)+") "
if id < 10 :
num = " "+num
#compute "installed" field
isInstalledStr = " "
if isInstalledWithSystemPM(p):
isInstalledStr = " YES, system"
elif isInstalledWithGitLocally(p):
isInstalledStr = " YES, git "
#compute description field
description = ""
if p in data.desc:
description = data.desc[p]
if len(description) > DESCRIPTION_EXTRACT_MAX_LENGTH:
description = description[0:DESCRIPTION_EXTRACT_MAX_LENGTH-3]+"..."
description = " " + description
#pad the name to the longest name
spaces = ' ' * (longestPackageName - len(p))
#if given, highlight the term
if highlightTerm is not None:
regex = re.compile(re.escape(highlightTerm), re.IGNORECASE)
p = regex.sub("\033[31m"+highlightTerm+"\033[m", p)
description = regex.sub("\033[31m"+highlightTerm+"\033[m", description)
if "[31m" not in p and "[31m" not in description:
description = description.replace("...", "\033[31m...\033[m")
print(num, p, spaces, isInstalledStr, description)
def printKaliMenu():
print('''
Please select a category:
1) Information Gathering 8) Exploitation Tools
2) Vulnerability Analysis 9) Forensics Tools
3) Wireless Attacks 10) Stress Testing
4) Web Applications 11) Password Attacks
5) Sniffing & Spoofing 12) Reverse Engineering
6) Maintaining Access 13) Hardware Hacking
7) Reporting Tools 14) Extra
''')
action = ""
while not action.isdigit() or int(action)<1 or int(action)>14 or not str(action) in data.packages:
action = readInput("Category: ")
printKaliSubMenu(str(action))
# prints a collection of packages
def printPackageCollection(package, highlightTerm):
#compute a map to find the package given the number
m = {}
longestStr = len(max(package, key=len))
print("")
i = 1
printTableHeader(longestStr)
for p in package:
m[i] = p
printPackageLine(i, p, longestStr, highlightTerm)
i += 1
print("")
no = ""
while not no.isdigit() or int(no)<1 or int(no)>=i:
no = readInput("Package No: ")
selectedPackage = m[int(no)]
printSelectedPackage(selectedPackage, highlightTerm)
# prints one of Kali's categories
def printKaliSubMenu(id):
ps = data.packages[id]
printPackageCollection(ps, None)
# prints the selected package, test if installed, and asks wheter to run it
def printSelectedPackage(p, highlightTerm):
print("-" * DESCRIPTION_EXTRACT_MAX_LENGTH)
print("| Package\033[1m", p, "\033[0m") #just to put in bold
print("-" * DESCRIPTION_EXTRACT_MAX_LENGTH)
if p in data.desc and data.desc[p] != "":
print("| Description:")
d = data.desc[p]
if highlightTerm is not None:
regex = re.compile(re.escape(highlightTerm), re.IGNORECASE)
d = regex.sub("\033[31m"+highlightTerm+"\033[m", d)
#print with line wrap
ds = d.split("\n")
for part in ds:
while len(part) > DESCRIPTION_EXTRACT_MAX_LENGTH:
end = part[0:DESCRIPTION_EXTRACT_MAX_LENGTH].rfind(' ')
print("| "+part[0:end])
part=part[end+1:]
print("| "+part)
else:
print("| (no description yet)")
print("-" * DESCRIPTION_EXTRACT_MAX_LENGTH)
if isInstalledWithSystemPM(p) or isInstalledWithGitLocally(p):
print("This package is already installed (and will not be downloaded).")
else:
print("This package is \033[31mnot\033[m installed, and will be downloaded if you try to run it.")
ans = ""
while ans != "y" and ans != "n" :
ans = readInput('Would you like to download/run it ? [Y/n] ').lower()
if ans == "y":
print("")
run(p)
else:
printKaliMenu()
def search(term):
print("")
print("Searching for \033[31m", term, "\033[m")
matches = []
for cat in data.packages:
if not cat.isdigit():
for p in data.packages[cat]:
if term.lower() in p.lower() or (p in data.desc and term.lower() in data.desc[p].lower()):
matches.append(p)
if len(matches) == 0:
print("No packages matching.")
else:
matches = list(set(matches)) #make results uniques
printPackageCollection(matches, term)
# ##################################################################
# ENTRY POINT
# ##################################################################
# entry point
handleInterrupts()
isGitInstalled()
printHeader()
# if no args given, print interactive menu, otherwise, directly search
if len(sys.argv) == 1:
print("")
print("PROTIP: use "+sys.argv[0]+" TERM to directly search for TERM")
printKaliMenu()
else:
search(sys.argv[1])
# use this to test if all packages are still hosted correctly
# helpers.testAllURLs()
#l = helpers.fetchPackageLinks()
#helpers.fetchPackageDescription(l)