-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkality.cpp
411 lines (359 loc) · 12.9 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
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
/*
* 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>
#include <regex>
using namespace std;
// ANSI COLOR CODES
namespace Color {
const string RESET = "\033[0m";
const string RED = "\033[31;1m";
const string GREEN = "\033[32;1m";
const string YELLOW = "\033[33;1m";
const string BLUE = "\033[34;1m";
const string CYAN = "\033[36;1m";
}
// LOGGING MACROS
#define LOG_INFO(msg) cout << Color::GREEN << "[INFO] " << Color::RESET << msg << Color::RESET << endl
#define LOG_ERROR(msg) cerr << Color::RED << "[ERROR] " << Color::RESET << msg << Color::RESET << endl
// FUNCTION PROTOTYPES
bool is_root();
void help();
void update();
void purge();
void install(vector<string> pkgs);
void uninstall(vector<string> pkgs);
vector<string> get_args(int argc, char* argv[]);
void set_keyring(bool set);
void get_keyring();
void rm_keyring();
bool is_keyring_installed(const string& pkg);
void add_file_content(const string& filePath, const string& content = "");
string find_deb_url(const string &html_content, const string &base_url);
// KEYRING URL
const string KEYRING_BASE_URL = "https://http.kali.org/kali/pool/main/k/kali-archive-keyring/";
// MAIN RUNNER
int main(int argc, char* argv[]) {
// check if program is run as root user
if (!is_root()) {
LOG_ERROR("Please run with sudo privileges.\n");
return -1;
}
// packages storage
vector<string> packages;
// check if any flag provided
if (argc > 1) {
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
string confirm;
cout << "Are you sure you want to remove Kalify? (y/n): ";
getline(cin, confirm);
if (confirm == "yes" || confirm == "y") {
purge();
}
else {
cout << endl;
LOG_INFO("Purge operation cancelled!");
}
}
// updating installed packages
else if (flag == "update" || flag == "u") {
get_keyring(); // checking if keyring needs to be installed
set_keyring(true); // setting keyring in files
update();
set_keyring(false); // removing keyring in files
}
// installing packages
else if (flag == "install" || flag == "i") {
if (argc > 2) {
packages = get_args(argc, argv); // getting package names
set_keyring(true);
install(packages);
set_keyring(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 = get_args(argc, argv);
set_keyring(true);
uninstall(packages);
set_keyring(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 find the .deb file
string find_deb_url(const string &html_content, const string &base_url) {
regex deb_regex("href=\"(.*?\\.deb)\"");
smatch matches;
if (regex_search(html_content, matches, deb_regex)) {
return base_url + matches.str(1);
} else {
return ""; // return null string if not found (highly unlikely) TODO: might need a check
}
}
// Function to sort the given arguments with required format
vector<string> get_args(int argc, char* argv[]) {
vector<string> packages;
// adding package names to vector
for (int i = 2; argv[i] != nullptr; i++) {
string pkg = argv[i]; // pkg name
// converting pkg name to lowercase (to avoid future errors)
for (char& c : pkg) {
c = 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 is_root() {
// if effective user ID is 0 (root)
return (geteuid() == 0);
}
// Function to display the help
void help() {
cout << " _ __ _ _ _ \n"
" | |/ /__ _| (_) |_ _ _ \n"
" | ' </ _` | | | _| || |\n"
" |_|\\_\\__,_|_|_|\\__|\\_, | By " + Color::CYAN + "Vandal\n" + Color::RESET + ""
" |__/ \n";
cout << "\n Usage: kality [options]\n\n";
cout << Color::GREEN + " OPTIONS:\n\n" + Color::RESET;
cout << " update (u)\t\t\tUpdate all installed packages\n";
cout << " install (i)\t\t\tInstall the provided packages if available in the repository\n";
cout << Color::YELLOW + " \t\t\t\te.g. kality install pkg1 pkg2 ... pkgN\t\n" + Color::RESET;
cout << " uninstall (x)\t\t\tUninstall the provided packages if found installed\n";
cout << Color::YELLOW + " \t\t\t\te.g. kality uninstall pkg1 pkg2 ... pkgN\t\n" + Color::RESET;
cout << " purge (p)\t\t\tRemoves kality from the system\n";
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 = 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() {
string srcFile = "/etc/apt/sources.list.d/kali.list";
string preFile = "/etc/apt/preferences.d/kali.pref";
// removing keyring pkg
rm_keyring();
// removing the kali.list file
if (remove(srcFile.c_str()) != 0) {
LOG_ERROR("Failed to remove kali.list file.");
}
// removing the kali.pref file
if (remove(preFile.c_str()) != 0) {
LOG_ERROR("Failed to remove kali.pref file.");
}
int out = system("apt-get update -y");
if (out != 0) {
LOG_ERROR("Failed to update packages.");
return;
}
LOG_INFO("Kality modified files have been removed!");
cout << Color::BLUE + "[NOTE]" + Color::RESET + " Now run the following to remove the bin file:\n";
cout << Color::YELLOW + " sudo rm /usr/local/bin/kality\n" + Color::RESET;
}
// Function to install the packages
void install(vector<string> pkgs) {
string pkgList = "";
for (const auto& pkg : pkgs) {
pkgList += pkg + " ";
}
// the install command
string cmd = "apt-get install " + pkgList + "-y";
int out = 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(vector<string> pkgs) {
string pkgList = "";
for (const auto& pkg : pkgs) {
pkgList += pkg + " ";
}
// the install command
string cmdPurge = "apt-get remove --purge " + pkgList + "-y";
int out = system(cmdPurge.c_str());
if (out != 0) {
LOG_ERROR("Failed to uninstall packages.");
return;
}
string cmdAutoremove = "apt-get autoremove -y";
out = 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 rm_keyring() {
LOG_INFO("Removing the Kali keyring from the system...");
// string cmd = "dpkg --purge " + keyringPkg;
// purge command
string cmd = "dpkg --purge kali-archive-keyring";
int out = 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 set_keyring(bool set) {
string keyContent = "deb https://http.kali.org/kali kali-rolling main non-free contrib";
string prefContent =
"Package: *\n"
"Pin: release a=kali-rolling\n"
"Pin-Priority: 50\n";
if (set) {
// adding kali apt configuration to file
add_file_content("/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
add_file_content("/etc/apt/preferences.d/kali.pref", prefContent);
LOG_INFO("Kali keyring: SET");
}
else {
add_file_content("/etc/apt/sources.list.d/kali.list");
add_file_content("/etc/apt/preferences.d/kali.pref");
LOG_INFO("Kali keyring: RELEASED");
}
}
// Function to add the given content to the file specified (write)
void add_file_content(const string& filePath, const string& content) {
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 get_keyring() {
// Check if the keyring package is already installed
if (is_keyring_installed("kali-archive-keyring")) {
LOG_INFO("Kali keyring is already installed. Skipping...");
return;
}
// Latest keyring check from the web page
LOG_INFO("Finding the latest Kali keyring...");
string temp_filename = "temp_keyring_page.html";
string KEYRING_URL;
string curl_cmd = "curl -s " + KEYRING_BASE_URL + " -o " + temp_filename;
int res = system(curl_cmd.c_str());
// Check if curl command was successful
if (res == 0) {
ifstream fin(temp_filename);
if (!fin) {
LOG_ERROR("Failed to open temp keyring page for parsing.");
return;
}
// Read the content of the downloaded web page into a string
string html_content((istreambuf_iterator<char>(fin)), istreambuf_iterator<char>());
fin.close();
// Find the .deb file URL by concatenating the base URL with the .deb file path
KEYRING_URL = find_deb_url(html_content, KEYRING_BASE_URL); // deb file URL
remove(temp_filename.c_str());
} else {
LOG_ERROR("Failed to download the webpage using curl.");
return;
}
// Downloading the keyring
LOG_INFO("Downloading and installing Kali keyring...");
string wgetCmd = "wget " + KEYRING_URL;
if (system(wgetCmd.c_str()) != 0) {
LOG_ERROR("Failed to download the keyring.");
return;
}
// Installing the keyring
string pkgFile = KEYRING_URL.substr(KEYRING_URL.find_last_of("/") + 1);
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 is_keyring_installed(const string& pkg) {
string cmd = "dpkg-query -W -f='${Status}' " + pkg + " 2>/dev/null | grep -q 'install ok installed'";
return (system(cmd.c_str()) == 0);
}