forked from rafaelh/update-kali
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdateactions.py
219 lines (181 loc) · 6.78 KB
/
updateactions.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
#!/usr/bin/env python3
import os
import sys
import string
from datetime import datetime
def print_message(color, message):
"""Prints a formatted message to the console"""
if color == "green":
print(
"\033[1;32m[+] \033[0;37m"
+ datetime.now().strftime("%H:%M:%S")
+ " - "
+ message
)
elif color == "blue":
print(
"\033[1;34m[i] \033[0;37m"
+ datetime.now().strftime("%H:%M:%S")
+ " - "
+ message
)
elif color == "yellow":
print(
"\033[0;33m[<] \033[0;37m"
+ datetime.now().strftime("%H:%M:%S")
+ " - "
+ message,
end="",
)
elif color == "red":
print(
"\033[1;31m[-] \033[0;37m"
+ datetime.now().strftime("%H:%M:%S")
+ " - "
+ message
)
elif color == "error":
print(
"\033[1;31m[!] \033[0;37m"
+ datetime.now().strftime("%H:%M:%S")
+ " - "
+ message
)
else:
print(
"\033[0;41mInvalid Format\033[0;37m "
+ datetime.now().strftime("%H:%M:%S")
+ " "
+ message
)
def elevate_privileges():
"""Gets sudo privileges and returns the current date"""
status = os.system("sudo date; echo")
return status
def take_ownership(directory):
username = "kali"
cmdstring = "sudo chown " + username + ":" + username + " " + directory
os.system(cmdstring)
def update_packages():
"""Do a general update of the system packages"""
cmdseries = [
"sudo apt update",
"sudo apt full-upgrade -y",
"sudo apt autoremove -y",
]
for cmdstring in cmdseries:
os.system(cmdstring)
def install_package(package, apt_cache):
"""Installs a package using apt"""
if not apt_cache[package].is_installed:
print_message("green", "Installing " + package)
cmdstring = "sudo apt install -y " + package
os.system(cmdstring)
def remove_package(package, apt_cache):
"""Removes a package using apt"""
if apt_cache[package].is_installed:
print_message("red", "Removing " + package)
cmdstring = "sudo apt remove -y " + package
os.system(cmdstring)
def pip_package_install(pip_packages, installed_pip_packages):
"""Install python pip package"""
for package in pip_packages:
if not package in installed_pip_packages:
print_message("green", "Installing pip package " + package)
cmdstring = "python3 -m pip install --upgrade python3-" + package
os.system(cmdstring)
def update_pip_packages():
"""Update pip packages"""
cmdstring = "pip3 list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 pip3 install -U 2>/dev/null; break"
os.system(cmdstring)
def gem_package_install(gem_packages, installed_gem_packages):
"""Install Ruby gem package"""
for package in gem_packages:
if not package in installed_gem_packages:
print_message("green", "Installing gem package " + package)
cmdstring = "sudo gem install " + package
os.system(cmdstring)
def update_gems():
"""Update gem packages"""
cmdstring = "gem outdated >/dev/null; gem update"
os.system(cmdstring)
def install_golang_module(module, golang_install_directory):
"""Install the specified Golang module"""
modulename = module.split("/")[-1].lower()
if not os.path.exists(golang_install_directory + "/" + modulename):
print_message("green", "Installing go module " + modulename)
cmdseries = [
"go install " + module + "@latest",
"sudo ln -s "
+ golang_install_directory
+ "/"
+ modulename
+ "/bin/"
+ modulename
+ " /usr/local/bin/"
+ modulename,
]
os.environ["GOPATH"] = golang_install_directory + "/" + modulename
for cmdstring in cmdseries:
os.system(cmdstring)
def update_go_packages(golang_modules_to_install, golang_install_directory):
"""Rebuild all Go modules"""
print_message("green", "Rebuilding Go modules")
for modulename in golang_modules_to_install:
module = modulename.split("/")[-1].lower()
os.environ["GOPATH"] = golang_install_directory + "/" + module
cmdstring = "go install " + modulename + "@latest"
os.system(cmdstring)
def create_directory(directory):
"""Checks if the specified directory exists, and creates it if not"""
if not os.path.exists(directory):
print_message("green", "Creating directory: " + directory)
cmdstring = "mkdir " + directory
os.system(cmdstring)
def remove_directory(directory):
"""Checks if the specified directory exists, and deletes it if it does"""
directory = os.getenv("HOME") + "/" + directory
if os.path.exists(directory):
print_message("red", "Removing directory: " + directory)
cmdstring = "rmdir " + directory
os.system(cmdstring)
def sync_git_repo(gitrepo, repo_collection_dir):
"""Sync the specified git repository"""
repo_name = gitrepo.split("/")[-1].lower()
if os.path.exists(repo_collection_dir + "/" + repo_name):
print_message("yellow", "Syncing " + repo_name + ": ")
sys.stdout.flush()
cmdstring = "git -C " + repo_collection_dir + "/" + repo_name + " pull"
os.system(cmdstring)
else:
print_message("green", "Cloning " + repo_name)
cmdstring = "git clone " + gitrepo + " " + repo_collection_dir + "/" + repo_name
os.system(cmdstring)
def run_scripts():
"""Run each .sh or .py file in the scripts directory"""
script_directory = os.path.dirname(os.path.realpath(__file__)) + "/scripts"
if os.path.exists(script_directory):
scripts = sorted(os.listdir(script_directory))
for script in scripts:
if ".sh" or ".py" in script:
cmdstring = script_directory + "/" + script
os.system(cmdstring)
else:
print_message("error", "'scripts' directory is missing")
def add_line_to_zshrc(line):
"""
Appends a line to the .zshrc file located at /home/kali/.zshrc.
:param line: The line to be added to the .zshrc file.
"""
zshrc_path = "/home/kali/.zshrc"
try:
with open(zshrc_path, "r") as file:
lines = file.readlines()
if line not in [l.strip() for l in lines]:
with open(zshrc_path, "a") as file:
file.write("\n" + line + "\n")
print("Line added to .zshrc successfully.")
else:
print_message("yellow", "Line already exists in .zshrc.")
except Exception as e:
print_message("error", f"{e}")