-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecureShell.cpp
More file actions
404 lines (310 loc) · 12 KB
/
Copy pathSecureShell.cpp
File metadata and controls
404 lines (310 loc) · 12 KB
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
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <direct.h> //For directory operations: _chdir, _getcwd, _mkdir, _rmdir
#include <windows.h> //For console colors and system timing
using namespace std;
//System limits
#define BLOOM_SIZE 1024
#define HASH_TABLE_SIZE 31
#define MAX_TRIE_CHILD 26
#define MAX_PATH_LEN 260
// Global state for the "Chroot Jail" security feature
char initialWorkingDirectory[MAX_PATH_LEN];
// 1. BLOOM FILTER (The Malware Guard)
// Purpose: Probabilistic check to block "dangerous" commands before execution.
// Alignment: GA-3 (Problem Analysis) - Real-world security utilization.
class SecurityGuard{
bool bitArray[BLOOM_SIZE];
public:
SecurityGuard(){
for (int i = 0; i < BLOOM_SIZE; i++) bitArray[i] = false;
}
// Using DJB2 hash for speed and distribution
int hash1(const char* s){
unsigned long h = 5381;
while (*s) h = ((h << 5) + h) + *s++;
return h % BLOOM_SIZE;
}
// Using SDBM hash to minimize false positive collisions
int hash2(const char* s){
unsigned long h = 0;
while (*s) h = (*s++) + (h << 6) + (h << 16) - h;
return h % BLOOM_SIZE; }
void blacklist(const char* cmd){
bitArray[hash1(cmd)] = true;
bitArray[hash2(cmd)] = true; }
bool isMalicious(const char* cmd){
return bitArray[hash1(cmd)] && bitArray[hash2(cmd)];
}
};
// 2. TRIE (The Autocomplete Indexer)
// Purpose: Fast prefix-based lookup for command suggestions.
// Alignment: Master implementation of linked data structures (CLO-1).
struct TrieNode{
TrieNode* children[MAX_TRIE_CHILD];
bool isEndOfWord;
TrieNode(){
isEndOfWord = false;
for (int i = 0; i < MAX_TRIE_CHILD; i++) children[i] = NULL;
}
};
class CommandIndexer{
TrieNode* root;
void findRecursive(TrieNode* node, char* prefix, int depth){
if (node->isEndOfWord) {
prefix[depth] = '\0';
cout << " -> " << prefix << endl;
}
for (int i = 0; i < 26; i++) {
if (node->children[i]) {
prefix[depth] = (char)(i + 'a');
findRecursive(node->children[i], prefix, depth + 1);
}
}
}
public:
CommandIndexer() { root = new TrieNode(); }
void insert(const char* cmd){
TrieNode* curr = root;
for (int i = 0; cmd[i]; i++) {
int idx = tolower(cmd[i]) - 'a';
if (idx < 0 || idx >= 26) continue;
if (!curr->children[idx]) curr->children[idx] = new TrieNode();
curr = curr->children[idx];
}
curr->isEndOfWord = true;
}
void suggest(const char* partial){
TrieNode* curr = root;
for (int i = 0; partial[i]; i++) {
int idx = tolower(partial[i]) - 'a';
if (idx < 0 || idx >= 26 || !curr->children[idx]) return;
curr = curr->children[idx];
}
char buffer[50];
strcpy(buffer, partial);
findRecursive(curr, buffer, strlen(partial));
}
};
// 3. HASH TABLE WITH CHAINING (Command Registry)
// Purpose: O(1) average lookup.We used chaining to satisfy Lab CLO-1.
// Alignment: Advanced Data Structures (CLO-1/2).
struct HashNode{
char name[32];
void (*func)(const char*);
HashNode* next;
HashNode(const char* n, void (*f)(const char*)){
strcpy(name, n);
func = f;
next = NULL;
}
};
class CommandRegistry{
HashNode* table[HASH_TABLE_SIZE];
int computeHash(const char* key){
int h = 0;
for (int i = 0; key[i]; i++) h = (h * 37) + key[i];
return (h < 0 ? -h : h) % HASH_TABLE_SIZE;
}
public:
CommandRegistry(){
for (int i = 0; i < HASH_TABLE_SIZE; i++) table[i] = NULL;
}
void bind(const char* name, void (*f)(const char*)){
int idx = computeHash(name);
HashNode* newNode = new HashNode(name, f);
newNode->next = table[idx];
table[idx] = newNode;
}
void run(const char* name, const char* args){
int idx = computeHash(name);
HashNode* curr = table[idx];
while (curr) {
if (strcmp(curr->name, name) == 0) {
curr->func(args);
return;
}
curr = curr->next;
}
cout << "\033[31mCommand '" << name << "' not found. Type 'help'.\033[0m" << endl;
}
};
// 4. TREAP (Frequency-Priority History)
// Purpose: Keeps history sorted alphabetically while prioritizing most used items.
// Alignment: Balanced Search Trees / Priority Queues (CLO-1/2).
struct TreapNode {
char cmdLine[128];
int frequency; // Heap priority (based on usage)
int randomRank; // Tie-breaker to maintain balance
TreapNode *left, *right;
TreapNode(const char* line) {
strcpy(cmdLine, line);
frequency = 1;
randomRank = rand() % 1000;
left = right = NULL;
}
};
//Treap for history with rotations
class HistoryAudit {
TreapNode* root;
TreapNode* rotateRight(TreapNode* y) { //left rotation
TreapNode *x = y->left, *T2 = x->right;
x->right = y;
y->left = T2;
return x;
}
TreapNode* rotateLeft(TreapNode* x) { //right rotation
TreapNode *y = x->right, *T2 = y->left;
y->left = x;
x->right = T2;
return y;
}
TreapNode* insert(TreapNode* node, const char* line) {
if (!node) return new TreapNode(line);
if (strcmp(line, node->cmdLine) == 0) {
node->frequency++; // Increase priority of frequently used command
return node;
}
if (strcmp(line, node->cmdLine) < 0) {
node->left = insert(node->left, line);
if (node->left->frequency > node->frequency) node = rotateRight(node);
} else {
node->right = insert(node->right, line);
if (node->right->frequency > node->frequency) node = rotateLeft(node);
}
return node;
}
void display(TreapNode* node) {
if (!node) return;
display(node->left);
cout << " [" << node->frequency << "x] " << node->cmdLine << endl;
display(node->right);
}
public:
HistoryAudit() { root = NULL; }
void add(const char* line) { root = insert(root, line); }
void show() { display(root); }
};
//GLOBAL INSTANCES
CommandRegistry shellReg;
CommandIndexer shellTrie;
SecurityGuard shellShield;
HistoryAudit shellLogs;
// COMMAND LOGIC
void cmd_help(const char*){
cout << "\n\033[33m--- COMMAND LIST ---\033[0m\n";
cout << "Basic: ls, pwd, cd, mkdir, rmdir, touch, rm, clear, exit\n";
cout << "System: whoami, date, echo <text>, history\n";
cout << "Security: encrypt <file>, decrypt <file>\n";
}
void cmd_pwd(const char*){ //cuurent directroy
char buffer[MAX_PATH_LEN];
_getcwd(buffer, MAX_PATH_LEN);
cout << buffer << endl;
}
void cmd_cd(const char* args){ //change directory
if (strlen(args) == 0) return;
// SECURITY: Chroot Jail implementation
if (strcmp(args, "..") == 0) {
char current[MAX_PATH_LEN];
_getcwd(current, MAX_PATH_LEN);
if (strcmp(current, initialWorkingDirectory) == 0) {
cout << "\033[31mSecurity: You cannot navigate above the project root.\033[0m" << endl;
return;
}
}
if (_chdir(args) != 0) cout << "Error: Path not found.\n";
}
void cmd_whoami(const char*) { cout << "Attique Alvi AirUniversity_Student" << endl; }
void cmd_date(const char*) { time_t n = time(0); cout << ctime(&n); }
void cmd_echo(const char* args) { cout << args << endl; } //displays message
void cmd_ls(const char*) { system("dir /b"); } //list
void cmd_mkdir(const char* args) { if(_mkdir(args) != 0) cout << "Failed to create directory.\n"; } //make directory
void cmd_rmdir(const char* args) { if(_rmdir(args) != 0) cout << "Failed to remove directory.\n"; } //remove director
void cmd_touch(const char* args) { ofstream f(args); f.close(); cout << "File " << args << " touched.\n"; } //make folder
void cmd_rm(const char* args) { if(remove(args) != 0) cout << "Failed to remove file.\n"; } //remove file
void cmd_clear(const char*) { system("cls"); } //clears screen
void cmd_exit(const char*) { exit(0); } //exit terminal
void xorTransform(const char* filename){ // for encryption and decryption
if (strlen(filename) == 0) return;
char key = 'S'; // Simple encryption key
fstream file(filename, ios::in | ios::out | ios::binary);
if (!file) { cout << "File operation failed.\n"; return; }
char c;
while (file.get(c)){
file.seekp((int)file.tellg() - 1);
c ^= key;
file.put(c);
file.seekg(file.tellp());
}
file.close();
cout << "Security transformation applied to: " << filename << endl;
}
// MAIN
int main(){
srand(time(0));
_getcwd(initialWorkingDirectory, MAX_PATH_LEN);
// Registering 14 commands
shellReg.bind("help", cmd_help);
shellReg.bind("pwd", cmd_pwd);
shellReg.bind("cd", cmd_cd);
shellReg.bind("ls", cmd_ls);
shellReg.bind("mkdir", cmd_mkdir);
shellReg.bind("rmdir", cmd_rmdir);
shellReg.bind("touch", cmd_touch);
shellReg.bind("rm", cmd_rm);
shellReg.bind("whoami", cmd_whoami);
shellReg.bind("date", cmd_date);
shellReg.bind("echo", cmd_echo);
shellReg.bind("history", cmd_help); // Wrapper for history
shellReg.bind("clear", cmd_clear);
shellReg.bind("exit", cmd_exit);
shellReg.bind("encrypt", xorTransform);
shellReg.bind("decrypt", xorTransform);
// Indexing for Autocomplete
const char* list[] = {"help","pwd","cd","ls","mkdir","rmdir","touch","rm","whoami","date","echo","history","clear","exit","encrypt","decrypt"};
for(int i=0; i<16; i++) shellTrie.insert(list[i]);
// Blacklisting dangerous commands in Bloom Filter
shellShield.blacklist("format");
shellShield.blacklist("deltree");
system("cls");
cout << "\033[1;36m====================================================\033[0m" << endl;
cout << "\033[1;36m SECURE ALGORITHMIC TERMINAL \033[0m" << endl;
cout << "\033[1;36m====================================================\033[0m" << endl;
char inputBuffer[256];
while (true){
char path[MAX_PATH_LEN];
_getcwd(path, MAX_PATH_LEN);
cout << "\033[1;32mateeq@minibash\033[0m:\033[1;34m" << path << "\033[0m$ ";
if (!cin.getline(inputBuffer, 256)) {
cout << "\n\033[31mEOF detected (Ctrl + Z). Exiting terminal safely.\033[0m\n";
break;
}
if (strlen(inputBuffer) == 0) continue;
// Malware Check (Bloom Filter)
if (shellShield.isMalicious(inputBuffer)) {
cout << "\033[31m[BLOOM FILTER] Execution blocked: Potential threat.\033[0m" << endl;
continue;
}
// Suggestions (Trie)
shellTrie.suggest(inputBuffer);
// Parsing
char cmd[64] = "", args[192] = "";
int i = 0;
while(inputBuffer[i] != ' ' && inputBuffer[i] != '\0'){
cmd[i] = inputBuffer[i];
i++;
}
cmd[i] = '\0';
if(inputBuffer[i] == ' ') strcpy(args, inputBuffer + i + 1);
// History Update (Treap)
shellLogs.add(cmd);
// Execution (Hash Table Chaining)
if (strcmp(cmd, "history") == 0) shellLogs.show();
else shellReg.run(cmd, args);
}
return 0;
}