-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkality.cpp
368 lines (322 loc) · 11.7 KB
/
kality.cpp
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
/*
* Kality - Kali Linux package management tool for Debian systems
*
* Author: Vandal (VandalByte)
* GitHub: https://github.com/VandalByte/kality
* License: GPL-3.0
*
* DISCLAIMER:
* This program is provided as-is. Use at your own risk.
*
* DESCRIPTION:
* Kality is a package management tool designed to integrate the Kali Linux
* repository into Debian-based systems. It allows installation of packages
* from the Kali repository that are not available in the Debian repository,
* while ensuring proper handling of keyrings and package preferences.
*
* Feel free to check out the official GitHub repository for any queries or issues.
*/
#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>
#include <unistd.h>
#include <fstream>
// ANSI COLOR CODES
namespace Color {
const std::string RESET = "\033[0m";
const std::string RED = "\033[31;1m";
const std::string GREEN = "\033[32;1m";
const std::string YELLOW = "\033[33;1m";
const std::string BLUE = "\033[34;1m";
const std::string CYAN = "\033[36;1m";
}
// LOGGING MACROS
#define LOG_INFO(msg) std::cout << Color::GREEN << "[INFO] " << Color::RESET << msg << Color::RESET << std::endl
#define LOG_ERROR(msg) std::cerr << Color::RED << "[ERROR] " << Color::RESET << msg << Color::RESET << std::endl
// FUNCTION PROTOTYPES
bool isRoot();
void help();
void update();
void purge();
void install(std::vector<std::string> pkgs);
void uninstall(std::vector<std::string> pkgs);
std::vector<std::string> getArgs(int argc, char* argv[]);
void setKeyring(bool set);
void getKeyring();
void removeKeyring();
bool isKeyringInstalled(const std::string& pkg);
void addFileContent(const std::string& filePath, const std::string& content = "");
// KEYRING URL
const std::string KEYRING_URL = "https://http.kali.org/kali/pool/main/k/kali-archive-keyring/kali-archive-keyring_2024.1_all.deb";
// MAIN RUNNER
int main(int argc, char* argv[]) {
// check if program is run as root user
if (!isRoot()) {
LOG_ERROR("Please run with sudo privileges.\n");
return -1;
}
// packages storage
std::vector<std::string> packages;
// check if any flag provided
if (argc > 1) {
std::string flag = argv[1]; // flag option
// displays help menu and exits
if (flag == "help" || flag == "h") {
help();
}
// removing kality from system
else if (flag == "purge" || flag == "p") {
// ask for confirmation
std::string confirm;
std::cout << "Are you sure you want to remove Kalify? (y/n): ";
std::getline(std::cin, confirm);
if (confirm == "yes" || confirm == "y") {
purge();
}
else {
std::cout << std::endl;
LOG_INFO("Purge operation cancelled!");
}
}
// updating installed packages
else if (flag == "update" || flag == "u") {
getKeyring(); // checking if keyring needs to be installed
setKeyring(true); // setting keyring in files
update();
setKeyring(false); // removing keyring in files
}
// installing packages
else if (flag == "install" || flag == "i") {
if (argc > 2) {
packages = getArgs(argc, argv); // getting package names
setKeyring(true);
install(packages);
setKeyring(false);
}
else {
LOG_ERROR("Please provide package(s) to install. For help use " + Color::YELLOW + "kality help" + Color::RESET);
return -1;
}
}
// uninstalling packages
else if (flag == "uninstall" || flag == "x") {
if (argc > 2) {
packages = getArgs(argc, argv);
setKeyring(true);
uninstall(packages);
setKeyring(false);
}
else {
LOG_ERROR("Please provide package(s) to uninstall. For help use " + Color::YELLOW + "kality help" + Color::RESET);
return -1;
}
}
// if no valid flag was found
else {
LOG_ERROR("Invalid argument(s). For help use " + Color::YELLOW + "kality help" + Color::RESET);
return -1;
}
}
// if no valid arg given
else {
LOG_ERROR("No flags found. For help use " + Color::YELLOW + "kality help" + Color::RESET);
return -1;
}
return 0;
}
// FUNCTIONS
// Function to sort the given arguments with required format
std::vector<std::string> getArgs(int argc, char* argv[]) {
std::vector<std::string> packages;
// adding package names to vector
for (int i = 2; argv[i] != nullptr; i++) {
std::string pkg = argv[i]; // pkg name
// converting pkg name to lowercase (to avoid future errors)
for (char& c : pkg) {
c = std::tolower(c);
}
// check duplication of the packages from args
bool isDuplicate = false;
for (const auto& existingPkg : packages) {
if (pkg == existingPkg) {
isDuplicate = true;
break;
}
}
if (!isDuplicate) {
packages.push_back(pkg);
}
}
return packages;
}
// Function to check whether the script excecuted with sudo privileges
bool isRoot() {
// if effective user ID is 0 (root)
return (geteuid() == 0);
}
// Function to display the help
void help() {
std::cout << " _ __ _ _ _ \n"
"| |/ /__ _| (_) |_ _ _ \n"
"| ' </ _` | | | _| || |\n"
"|_|\\_\\__,_|_|_|\\__|\\_, | By " + Color::CYAN + "Vandal\n" + Color::RESET + ""
" |__/ \n";
std::cout << "\nUsage: kality [options]\n\n";
std::cout << Color::GREEN + "OPTIONS:\n\n" + Color::RESET;
std::cout << " update (u)\t\t\tUpdate all installed packages\n";
std::cout << " install (i)\t\t\tInstall the provided packages if available in the repository\n";
std::cout << Color::YELLOW + " \t\t\t\te.g. kality install pkg1 pkg2 ... pkgN\t\n" + Color::RESET;
std::cout << " uninstall (x)\t\t\tUninstall the provided packages if found installed\n";
std::cout << Color::YELLOW + " \t\t\t\te.g. kality uninstall pkg1 pkg2 ... pkgN\t\n" + Color::RESET;
std::cout << " purge (p)\t\t\tRemoves kality from the system\n";
std::cout << " help (h)\t\t\tDisplays this help message and exits\n\n";
}
// Function to update the packages
void update() {
// the update command
int out = std::system("apt-get upgrade -y");
if (out != 0) {
LOG_ERROR("Failed to update packages.");
return;
}
LOG_INFO("All packages have been successfully updated!");
}
// Function to remove all kality changes
void purge() {
std::string srcFile = "/etc/apt/sources.list.d/kali.list";
std::string preFile = "/etc/apt/preferences.d/kali.pref";
// removing keyring pkg
removeKeyring();
// removing the kali.list file
if (std::remove(srcFile.c_str()) != 0) {
LOG_ERROR("Failed to remove kali.list file.");
}
// removing the kali.pref file
if (std::remove(preFile.c_str()) != 0) {
LOG_ERROR("Failed to remove kali.pref file.");
}
int out = std::system("apt-get update -y");
if (out != 0) {
LOG_ERROR("Failed to update packages.");
return;
}
LOG_INFO("Kality modified files have been removed!");
std::cout << Color::BLUE + "[NOTE]" + Color::RESET + " Now run the following to remove the bin file:\n\tsudo rm /usr/local/bin/kality\n";
}
// Function to install the packages
void install(std::vector<std::string> pkgs) {
std::string pkgList = "";
for (const auto& pkg : pkgs) {
pkgList += pkg + " ";
}
// the install command
std::string cmd = "apt-get install " + pkgList + "-y";
int out = std::system(cmd.c_str());
if (out != 0) {
LOG_ERROR("Failed to install packages.");
return;
}
LOG_INFO("All packages have been successfully installed!");
}
// Function to uninstall the packages
void uninstall(std::vector<std::string> pkgs) {
std::string pkgList = "";
for (const auto& pkg : pkgs) {
pkgList += pkg + " ";
}
// the install command
std::string cmdPurge = "apt-get remove --purge " + pkgList + "-y";
int out = std::system(cmdPurge.c_str());
if (out != 0) {
LOG_ERROR("Failed to uninstall packages.");
return;
}
std::string cmdAutoremove = "apt-get autoremove -y";
out = std::system(cmdAutoremove.c_str());
if (out != 0) {
LOG_ERROR("Failed to remove dependency packages.");
return;
}
LOG_INFO("All packages have been successfully uninstalled!");
}
// Function to remove the kali keyring
void removeKeyring() {
LOG_INFO("Removing the Kali keyring from the system...");
// std::string cmd = "dpkg --purge " + keyringPkg;
// purge command
std::string cmd = "dpkg --purge kali-archive-keyring";
int out = std::system(cmd.c_str());
if (out != 0) {
LOG_ERROR("Failed to remove Kali keyring.");
return;
}
LOG_INFO("Kali keyring removed successfully!");
}
// Function to set keyring file and preference file
void setKeyring(bool set) {
std::string keyContent = "deb https://http.kali.org/kali kali-rolling main non-free contrib";
std::string prefContent =
"Package: *\n"
"Pin: release a=kali-rolling\n"
"Pin-Priority: 50\n";
if (set) {
// adding kali apt configuration to file
addFileContent("/etc/apt/sources.list.d/kali.list", keyContent);
// adding priority preference to the file
// : must be in lower priority to avoid conflict between similar packages of main repo
addFileContent("/etc/apt/preferences.d/kali.pref", prefContent);
LOG_INFO("Kali keyring: SET");
}
else {
addFileContent("/etc/apt/sources.list.d/kali.list");
addFileContent("/etc/apt/preferences.d/kali.pref");
LOG_INFO("Kali keyring: RELEASED");
}
}
// Function to add the given content to the file specified (write)
void addFileContent(const std::string& filePath, const std::string& content) {
std::ofstream outfile(filePath);
if (!outfile.is_open()) {
LOG_ERROR("Can't open the file '" + Color::BLUE + filePath + Color::RESET + "'");
return;
}
outfile << content;
if (!outfile.good()) {
LOG_ERROR("Failed writing to the file '" + Color::BLUE + filePath + Color::RESET + "'");
}
outfile.close();
}
// Function to download and install Kali keyring
void getKeyring() {
// Check if the keyring package is already installed
if (isKeyringInstalled("kali-archive-keyring")) {
LOG_INFO("Kali keyring is already installed. Skipping...");
return;
}
// downloading the keyring
LOG_INFO("Downloading and installing Kali keyring...");
std::string wgetCmd = "wget " + KEYRING_URL;
if (system(wgetCmd.c_str()) != 0) {
LOG_ERROR("Failed to download the keyring.");
return;
}
// installing the keyring
std::string pkgFile = KEYRING_URL.substr(KEYRING_URL.find_last_of("/") + 1);
std::string dpkgCmd = "sudo dpkg -i " + pkgFile;
if (system(dpkgCmd.c_str()) != 0) {
LOG_ERROR("Failed to install the keyring.");
return;
}
// removing the keyring .deb file after installation
if (remove(pkgFile.c_str()) != 0) {
LOG_ERROR("Failed to remove the keyring file.");
return;
}
LOG_INFO("Kali keyring installed successfully.");
}
// Function to check if a keyring is installed
bool isKeyringInstalled(const std::string& pkg) {
std::string cmd = "dpkg-query -W -f='${Status}' " + pkg + " 2>/dev/null | grep -q 'install ok installed'";
return (system(cmd.c_str()) == 0);
}