PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/nmap_ftp.cc

https://gitlab.com/g10h4ck/nmap-gsoc2015
C++ | 383 lines | 224 code | 28 blank | 131 comment | 88 complexity | b5df5914c9a1a37481e7278e4c6512b3 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, Apache-2.0, LGPL-2.0, LGPL-2.1, MIT
  1. /***************************************************************************
  2. * nmap_ftp.cc -- Nmap's FTP routines used for FTP bounce scan (-b)
  3. * *
  4. ***********************IMPORTANT NMAP LICENSE TERMS************************
  5. * *
  6. * The Nmap Security Scanner is (C) 1996-2015 Insecure.Com LLC. Nmap is *
  7. * also a registered trademark of Insecure.Com LLC. This program is free *
  8. * software; you may redistribute and/or modify it under the terms of the *
  9. * GNU General Public License as published by the Free Software *
  10. * Foundation; Version 2 ("GPL"), BUT ONLY WITH ALL OF THE CLARIFICATIONS *
  11. * AND EXCEPTIONS DESCRIBED HEREIN. This guarantees your right to use, *
  12. * modify, and redistribute this software under certain conditions. If *
  13. * you wish to embed Nmap technology into proprietary software, we sell *
  14. * alternative licenses (contact sales@nmap.com). Dozens of software *
  15. * vendors already license Nmap technology such as host discovery, port *
  16. * scanning, OS detection, version detection, and the Nmap Scripting *
  17. * Engine. *
  18. * *
  19. * Note that the GPL places important restrictions on "derivative works", *
  20. * yet it does not provide a detailed definition of that term. To avoid *
  21. * misunderstandings, we interpret that term as broadly as copyright law *
  22. * allows. For example, we consider an application to constitute a *
  23. * derivative work for the purpose of this license if it does any of the *
  24. * following with any software or content covered by this license *
  25. * ("Covered Software"): *
  26. * *
  27. * o Integrates source code from Covered Software. *
  28. * *
  29. * o Reads or includes copyrighted data files, such as Nmap's nmap-os-db *
  30. * or nmap-service-probes. *
  31. * *
  32. * o Is designed specifically to execute Covered Software and parse the *
  33. * results (as opposed to typical shell or execution-menu apps, which will *
  34. * execute anything you tell them to). *
  35. * *
  36. * o Includes Covered Software in a proprietary executable installer. The *
  37. * installers produced by InstallShield are an example of this. Including *
  38. * Nmap with other software in compressed or archival form does not *
  39. * trigger this provision, provided appropriate open source decompression *
  40. * or de-archiving software is widely available for no charge. For the *
  41. * purposes of this license, an installer is considered to include Covered *
  42. * Software even if it actually retrieves a copy of Covered Software from *
  43. * another source during runtime (such as by downloading it from the *
  44. * Internet). *
  45. * *
  46. * o Links (statically or dynamically) to a library which does any of the *
  47. * above. *
  48. * *
  49. * o Executes a helper program, module, or script to do any of the above. *
  50. * *
  51. * This list is not exclusive, but is meant to clarify our interpretation *
  52. * of derived works with some common examples. Other people may interpret *
  53. * the plain GPL differently, so we consider this a special exception to *
  54. * the GPL that we apply to Covered Software. Works which meet any of *
  55. * these conditions must conform to all of the terms of this license, *
  56. * particularly including the GPL Section 3 requirements of providing *
  57. * source code and allowing free redistribution of the work as a whole. *
  58. * *
  59. * As another special exception to the GPL terms, Insecure.Com LLC grants *
  60. * permission to link the code of this program with any version of the *
  61. * OpenSSL library which is distributed under a license identical to that *
  62. * listed in the included docs/licenses/OpenSSL.txt file, and distribute *
  63. * linked combinations including the two. *
  64. * *
  65. * Any redistribution of Covered Software, including any derived works, *
  66. * must obey and carry forward all of the terms of this license, including *
  67. * obeying all GPL rules and restrictions. For example, source code of *
  68. * the whole work must be provided and free redistribution must be *
  69. * allowed. All GPL references to "this License", are to be treated as *
  70. * including the terms and conditions of this license text as well. *
  71. * *
  72. * Because this license imposes special exceptions to the GPL, Covered *
  73. * Work may not be combined (even as part of a larger work) with plain GPL *
  74. * software. The terms, conditions, and exceptions of this license must *
  75. * be included as well. This license is incompatible with some other open *
  76. * source licenses as well. In some cases we can relicense portions of *
  77. * Nmap or grant special permissions to use it in other open source *
  78. * software. Please contact fyodor@nmap.org with any such requests. *
  79. * Similarly, we don't incorporate incompatible open source software into *
  80. * Covered Software without special permission from the copyright holders. *
  81. * *
  82. * If you have any questions about the licensing restrictions on using *
  83. * Nmap in other works, are happy to help. As mentioned above, we also *
  84. * offer alternative license to integrate Nmap into proprietary *
  85. * applications and appliances. These contracts have been sold to dozens *
  86. * of software vendors, and generally include a perpetual license as well *
  87. * as providing for priority support and updates. They also fund the *
  88. * continued development of Nmap. Please email sales@nmap.com for further *
  89. * information. *
  90. * *
  91. * If you have received a written license agreement or contract for *
  92. * Covered Software stating terms other than these, you may choose to use *
  93. * and redistribute Covered Software under those terms instead of these. *
  94. * *
  95. * Source is provided to this software because we believe users have a *
  96. * right to know exactly what a program is going to do before they run it. *
  97. * This also allows you to audit the software for security holes. *
  98. * *
  99. * Source code also allows you to port Nmap to new platforms, fix bugs, *
  100. * and add new features. You are highly encouraged to send your changes *
  101. * to the dev@nmap.org mailing list for possible incorporation into the *
  102. * main distribution. By sending these changes to Fyodor or one of the *
  103. * Insecure.Org development mailing lists, or checking them into the Nmap *
  104. * source code repository, it is understood (unless you specify otherwise) *
  105. * that you are offering the Nmap Project (Insecure.Com LLC) the *
  106. * unlimited, non-exclusive right to reuse, modify, and relicense the *
  107. * code. Nmap will always be available Open Source, but this is important *
  108. * because the inability to relicense code has caused devastating problems *
  109. * for other Free Software projects (such as KDE and NASM). We also *
  110. * occasionally relicense the code to third parties as discussed above. *
  111. * If you wish to specify special license conditions of your *
  112. * contributions, just say so when you send them. *
  113. * *
  114. * This program is distributed in the hope that it will be useful, but *
  115. * WITHOUT ANY WARRANTY; without even the implied warranty of *
  116. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Nmap *
  117. * license file for more details (it's in a COPYING file included with *
  118. * Nmap, and also available from https://svn.nmap.org/nmap/COPYING) *
  119. * *
  120. ***************************************************************************/
  121. /* $Id$ */
  122. #include "nmap_ftp.h"
  123. #include "output.h"
  124. #include "NmapOps.h"
  125. #include "nmap_error.h"
  126. #include "tcpip.h"
  127. #include "Target.h"
  128. extern NmapOps o;
  129. struct ftpinfo get_default_ftpinfo(void) {
  130. #if (defined(IN_ADDR_DEEPSTRUCT) || defined(SOLARIS))
  131. /* Note that struct in_addr in solaris is 3 levels deep just to store an
  132. * unsigned int! */
  133. struct ftpinfo ftp = { FTPUSER, FTPPASS, "", { { { 0 } } } , 21, 0};
  134. #else
  135. struct ftpinfo ftp = { FTPUSER, FTPPASS, "", { 0 }, 21, 0};
  136. #endif
  137. return ftp;
  138. }
  139. /* parse a URL stype ftp string of the form user:pass@server:portno */
  140. int parse_bounce_argument(struct ftpinfo *ftp, char *url) {
  141. char *p = url, *q, *s;
  142. if ((q = strrchr(url, '@'))) { /* we have user and/or pass */
  143. *q++ = '\0';
  144. if ((s = strchr(p, ':'))) { /* we have user AND pass */
  145. *s++ = '\0';
  146. strncpy(ftp->pass, s, 255);
  147. } else { /* we ONLY have user */
  148. log_write(LOG_STDOUT, "Assuming %s is a username, and using the default password: %s\n",
  149. p, ftp->pass);
  150. }
  151. strncpy(ftp->user, p, 63);
  152. } else {
  153. q = url;
  154. }
  155. /* q points to beginning of server name */
  156. if ((s = strchr(q, ':'))) { /* we have portno */
  157. *s++ = '\0';
  158. ftp->port = atoi(s);
  159. }
  160. strncpy(ftp->server_name, q, MAXHOSTNAMELEN);
  161. ftp->user[63] = ftp->pass[255] = ftp->server_name[MAXHOSTNAMELEN] = 0;
  162. return 1;
  163. }
  164. int ftp_anon_connect(struct ftpinfo *ftp) {
  165. int sd;
  166. struct sockaddr_in sock;
  167. int res;
  168. char recvbuf[2048];
  169. char command[512];
  170. if (o.verbose || o.debugging)
  171. log_write(LOG_STDOUT, "Attempting connection to ftp://%s:%s@%s:%i\n",
  172. ftp->user, ftp->pass, ftp->server_name, ftp->port);
  173. if ((sd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
  174. gh_perror("Couldn't create %s socket", __func__);
  175. return 0;
  176. }
  177. socket_bindtodevice(sd, o.device);
  178. sock.sin_family = AF_INET;
  179. sock.sin_addr.s_addr = ftp->server.s_addr;
  180. sock.sin_port = htons(ftp->port);
  181. res = connect(sd, (struct sockaddr *) &sock, sizeof(struct sockaddr_in));
  182. if (res < 0)
  183. fatal("Your FTP bounce proxy server won't talk to us!");
  184. if (o.verbose || o.debugging)
  185. log_write(LOG_STDOUT, "Connected:");
  186. while ((res = recvtime(sd, recvbuf, sizeof(recvbuf) - 1, 7, NULL)) > 0) {
  187. if (o.debugging || o.verbose) {
  188. recvbuf[res] = '\0';
  189. log_write(LOG_STDOUT, "%s", recvbuf);
  190. }
  191. }
  192. if (res < 0)
  193. pfatal("recv problem from FTP bounce server");
  194. Snprintf(command, 511, "USER %s\r\n", ftp->user);
  195. send(sd, command, strlen(command), 0);
  196. res = recvtime(sd, recvbuf, sizeof(recvbuf) - 1, 12, NULL);
  197. if (res <= 0)
  198. pfatal("recv problem from FTP bounce server");
  199. recvbuf[res] = '\0';
  200. if (o.debugging)
  201. log_write(LOG_STDOUT, "sent username, received: %s", recvbuf);
  202. if (recvbuf[0] == '5')
  203. fatal("Your FTP bounce server doesn't like the username \"%s\"", ftp->user);
  204. Snprintf(command, 511, "PASS %s\r\n", ftp->pass);
  205. send(sd, command, strlen(command), 0);
  206. res = recvtime(sd, recvbuf, sizeof(recvbuf) - 1, 12, NULL);
  207. if (res < 0)
  208. pfatal("recv problem from FTP bounce server");
  209. if (!res) {
  210. error("Timeout from bounce server ...");
  211. } else {
  212. recvbuf[res] = '\0';
  213. if (o.debugging)
  214. log_write(LOG_STDOUT, "sent password, received: %s", recvbuf);
  215. if (recvbuf[0] == '5')
  216. fatal("Your FTP bounce server refused login combo (%s/%s)", ftp->user, ftp->pass);
  217. }
  218. while ((res = recvtime(sd, recvbuf, sizeof(recvbuf) - 1, 2, NULL)) > 0) {
  219. if (o.debugging) {
  220. recvbuf[res] = '\0';
  221. log_write(LOG_STDOUT, "%s", recvbuf);
  222. }
  223. }
  224. if (res < 0)
  225. pfatal("recv problem from FTP bounce server");
  226. if (o.verbose)
  227. log_write(LOG_STDOUT, "Login credentials accepted by FTP server!\n");
  228. ftp->sd = sd;
  229. return sd;
  230. }
  231. /* FTP bounce attack scan. This function is rather lame and should be
  232. rewritten. But I don't think it is used much anyway. If I'm going to
  233. allow FTP bounce scan, I should really allow SOCKS proxy scan. */
  234. void bounce_scan(Target *target, u16 *portarray, int numports,
  235. struct ftpinfo *ftp) {
  236. o.current_scantype = BOUNCE_SCAN;
  237. time_t starttime;
  238. int res , sd = ftp->sd, i = 0;
  239. const char *t = (const char *)target->v4hostip();
  240. int retriesleft = FTP_RETRIES;
  241. char recvbuf[2048];
  242. char targetstr[20];
  243. char command[512];
  244. char hostname[1200];
  245. unsigned short portno, p1, p2;
  246. int timedout;
  247. if (numports == 0)
  248. return; /* nothing to scan for */
  249. Snprintf(targetstr, 20, "%d,%d,%d,%d,", UC(t[0]), UC(t[1]), UC(t[2]), UC(t[3]));
  250. starttime = time(NULL);
  251. if (o.verbose || o.debugging) {
  252. struct tm *tm = localtime(&starttime);
  253. assert(tm);
  254. log_write(LOG_STDOUT, "Initiating TCP FTP bounce scan against %s at %02d:%02d\n", target->NameIP(hostname, sizeof(hostname)), tm->tm_hour, tm->tm_min );
  255. }
  256. for (i = 0; i < numports; i++) {
  257. /* Check for timeout */
  258. if (target->timedOut(NULL))
  259. return;
  260. portno = htons(portarray[i]);
  261. p1 = ((unsigned char *) &portno)[0];
  262. p2 = ((unsigned char *) &portno)[1];
  263. Snprintf(command, 512, "PORT %s%i,%i\r\n", targetstr, p1, p2);
  264. if (o.debugging)
  265. log_write(LOG_STDOUT, "Attempting command: %s", command);
  266. if (send(sd, command, strlen(command), 0) < 0 ) {
  267. gh_perror("send in %s", __func__);
  268. if (retriesleft) {
  269. if (o.verbose || o.debugging)
  270. log_write(LOG_STDOUT, "Our FTP proxy server hung up on us! retrying\n");
  271. retriesleft--;
  272. close(sd);
  273. ftp->sd = ftp_anon_connect(ftp);
  274. if (ftp->sd < 0)
  275. return;
  276. sd = ftp->sd;
  277. i--;
  278. } else {
  279. error("Our socket descriptor is dead and we are out of retries. Giving up.");
  280. close(sd);
  281. ftp->sd = -1;
  282. return;
  283. }
  284. } else { /* Our send is good */
  285. res = recvtime(sd, recvbuf, 2048, 15, NULL);
  286. if (res <= 0) {
  287. perror("recv problem from FTP bounce server");
  288. } else { /* our recv is good */
  289. recvbuf[res] = '\0';
  290. if (o.debugging)
  291. log_write(LOG_STDOUT, "result of port query on port %i: %s",
  292. portarray[i], recvbuf);
  293. if (recvbuf[0] == '5') {
  294. if (portarray[i] > 1023) {
  295. fatal("Your FTP bounce server sucks, it won't let us feed bogus ports!");
  296. } else {
  297. error("Your FTP bounce server doesn't allow privileged ports, skipping them.");
  298. while (i < numports && portarray[i] < 1024) i++;
  299. if (!portarray[i]) {
  300. fatal("And you didn't want to scan any unprivileged ports. Giving up.");
  301. }
  302. }
  303. } else { /* Not an error message */
  304. if (send(sd, "LIST\r\n", 6, 0) > 0 ) {
  305. res = recvtime(sd, recvbuf, 2048, 12, &timedout);
  306. if (res < 0) {
  307. perror("recv problem from FTP bounce server");
  308. } else if (res == 0) {
  309. if (timedout)
  310. target->ports.setPortState(portarray[i], IPPROTO_TCP, PORT_FILTERED);
  311. else target->ports.setPortState(portarray[i], IPPROTO_TCP, PORT_CLOSED);
  312. } else {
  313. recvbuf[res] = '\0';
  314. if (o.debugging)
  315. log_write(LOG_STDOUT, "result of LIST: %s", recvbuf);
  316. if (!strncmp(recvbuf, "500", 3)) {
  317. /* oh dear, we are not aligned properly */
  318. if (o.verbose || o.debugging)
  319. error("FTP command misalignment detected ... correcting.");
  320. res = recvtime(sd, recvbuf, 2048, 10, NULL);
  321. }
  322. if (recvbuf[0] == '1' || recvbuf[0] == '2') {
  323. target->ports.setPortState(portarray[i], IPPROTO_TCP, PORT_OPEN);
  324. if (recvbuf[0] == '1') {
  325. res = recvtime(sd, recvbuf, 2048, 5, NULL);
  326. if (res < 0)
  327. perror("recv problem from FTP bounce server");
  328. else {
  329. recvbuf[res] = '\0';
  330. if (res > 0) {
  331. if (o.debugging)
  332. log_write(LOG_STDOUT, "nxt line: %s", recvbuf);
  333. if (recvbuf[0] == '4' && recvbuf[1] == '2' && recvbuf[2] == '6') {
  334. target->ports.forgetPort(portarray[i], IPPROTO_TCP);
  335. if (o.debugging || o.verbose)
  336. log_write(LOG_STDOUT, "Changed my mind about port %i\n", portarray[i]);
  337. }
  338. }
  339. }
  340. }
  341. } else {
  342. /* This means the port is closed ... */
  343. target->ports.setPortState(portarray[i], IPPROTO_TCP, PORT_CLOSED);
  344. }
  345. }
  346. }
  347. }
  348. }
  349. }
  350. }
  351. if (o.debugging || o.verbose)
  352. log_write(LOG_STDOUT, "Scanned %d ports in %ld seconds via the Bounce scan.\n",
  353. numports, (long) time(NULL) - starttime);
  354. return;
  355. }