10_urlsnarf_escape.patch 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. diff -Nur dsniff-2.4-old/urlsnarf.c dsniff-2.4/urlsnarf.c
  2. --- dsniff-2.4-old/urlsnarf.c 2009-09-15 00:11:20.480202765 +0300
  3. +++ dsniff-2.4/urlsnarf.c 2009-09-15 00:11:20.581452467 +0300
  4. @@ -84,6 +84,43 @@
  5. return (tstr);
  6. }
  7. +static char *
  8. +escape_log_entry(char *string)
  9. +{
  10. + char *out;
  11. + unsigned char *c, *o;
  12. + size_t len;
  13. +
  14. + if (!string)
  15. + return NULL;
  16. +
  17. + /* Determine needed length */
  18. + for (c = string, len = 0; *c; c++) {
  19. + if ((*c < 32) || (*c >= 128))
  20. + len += 4;
  21. + else if ((*c == '"') || (*c =='\\'))
  22. + len += 2;
  23. + else
  24. + len++;
  25. + }
  26. + out = malloc(len+1);
  27. + if (!out)
  28. + return NULL;
  29. + for (c = string, o = out; *c; c++, o++) {
  30. + if ((*c < 32) || (*c >= 128)) {
  31. + snprintf(o, 5, "\\x%02x", *c);
  32. + o += 3;
  33. + } else if ((*c == '"') || ((*c =='\\'))) {
  34. + *(o++) = '\\';
  35. + *o = *c;
  36. + } else {
  37. + *o = *c;
  38. + }
  39. + }
  40. + out[len]='\0';
  41. + return out;
  42. +}
  43. +
  44. static int
  45. process_http_request(struct tuple4 *addr, u_char *data, int len)
  46. {
  47. @@ -142,18 +179,26 @@
  48. buf_tok(NULL, NULL, i);
  49. }
  50. }
  51. - if (user == NULL)
  52. - user = "-";
  53. - if (vhost == NULL)
  54. - vhost = libnet_addr2name4(addr->daddr, Opt_dns);
  55. - if (referer == NULL)
  56. - referer = "-";
  57. - if (agent == NULL)
  58. - agent = "-";
  59. -
  60. + user = escape_log_entry(user);
  61. + vhost = escape_log_entry(vhost);
  62. + uri = escape_log_entry(uri);
  63. + referer = escape_log_entry(referer);
  64. + agent = escape_log_entry(agent);
  65. +
  66. printf("%s - %s [%s] \"%s http://%s%s\" - - \"%s\" \"%s\"\n",
  67. libnet_addr2name4(addr->saddr, Opt_dns),
  68. - user, timestamp(), req, vhost, uri, referer, agent);
  69. + (user?user:"-"),
  70. + timestamp(), req,
  71. + (vhost?vhost:libnet_addr2name4(addr->daddr, Opt_dns)),
  72. + uri,
  73. + (referer?referer:"-"),
  74. + (agent?agent:"-"));
  75. +
  76. + free(user);
  77. + free(vhost);
  78. + free(uri);
  79. + free(referer);
  80. + free(agent);
  81. }
  82. fflush(stdout);
  83. diff -Nur dsniff-2.4-old/urlsnarf.c.orig dsniff-2.4/urlsnarf.c.orig
  84. --- dsniff-2.4-old/urlsnarf.c.orig 1970-01-01 02:00:00.000000000 +0200
  85. +++ dsniff-2.4/urlsnarf.c.orig 2009-09-15 00:11:20.450202713 +0300
  86. @@ -0,0 +1,275 @@
  87. +/*
  88. + * urlsnarf.c
  89. + *
  90. + * Sniff the network for HTTP request URLs, output in CLF format.
  91. + *
  92. + * Copyright (c) 1999 Dug Song <dugsong@monkey.org>
  93. + *
  94. + * $Id: urlsnarf.c,v 1.35 2001/03/15 09:26:13 dugsong Exp $
  95. + */
  96. +
  97. +#include "config.h"
  98. +
  99. +#include <sys/types.h>
  100. +#include <sys/socket.h>
  101. +#include <netinet/in.h>
  102. +#include <arpa/inet.h>
  103. +
  104. +#include <stdio.h>
  105. +#include <stdlib.h>
  106. +#include <unistd.h>
  107. +#include <string.h>
  108. +#include <regex.h>
  109. +#include <time.h>
  110. +#include <err.h>
  111. +#include <libnet.h>
  112. +#include <nids.h>
  113. +#include <pcap.h>
  114. +
  115. +#include "pcaputil.h"
  116. +#include "buf.h"
  117. +#include "base64.h"
  118. +#include "version.h"
  119. +
  120. +#define DEFAULT_PCAP_FILTER "tcp port 80 or port 8080 or port 3128"
  121. +
  122. +u_short Opt_dns = 1;
  123. +int Opt_invert = 0;
  124. +regex_t *pregex = NULL;
  125. +
  126. +static void
  127. +usage(void)
  128. +{
  129. + fprintf(stderr, "Version: " VERSION "\n"
  130. + "Usage: urlsnarf [-n] [-i interface | -p pcapfile] [[-v] pattern [expression]]\n");
  131. + exit(1);
  132. +}
  133. +
  134. +static int
  135. +regex_match(char *string)
  136. +{
  137. + return (pregex == NULL ||
  138. + ((regexec(pregex, string, 0, NULL, 0) == 0) ^ Opt_invert));
  139. +}
  140. +
  141. +static char *
  142. +timestamp(void)
  143. +{
  144. + static char tstr[32], sign;
  145. + struct tm *t, gmt;
  146. + time_t tt = time(NULL);
  147. + int days, hours, tz, len;
  148. +
  149. + gmt = *gmtime(&tt);
  150. + t = localtime(&tt);
  151. +
  152. + days = t->tm_yday - gmt.tm_yday;
  153. + hours = ((days < -1 ? 24 : 1 < days ? -24 : days * 24) +
  154. + t->tm_hour - gmt.tm_hour);
  155. + tz = hours * 60 + t->tm_min - gmt.tm_min;
  156. +
  157. + len = strftime(tstr, sizeof(tstr), "%d/%b/%Y:%X", t);
  158. + if (len < 0 || len > sizeof(tstr) - 5)
  159. + return (NULL);
  160. +
  161. + if (tz < 0) {
  162. + sign = '-';
  163. + tz = -tz;
  164. + }
  165. + else sign = '+';
  166. +
  167. + snprintf(tstr + len, sizeof(tstr) - len, " %c%.2d%.2d",
  168. + sign, tz / 60, tz % 60);
  169. +
  170. + return (tstr);
  171. +}
  172. +
  173. +static int
  174. +process_http_request(struct tuple4 *addr, u_char *data, int len)
  175. +{
  176. + struct buf *msg, buf;
  177. + char *p, *req, *uri, *user, *vhost, *referer, *agent;
  178. + int i;
  179. +
  180. + buf_init(&buf, data, len);
  181. +
  182. + while ((i = buf_index(&buf, "\r\n\r\n", 4)) >= 0) {
  183. + msg = buf_tok(&buf, NULL, i);
  184. + msg->base[msg->end] = '\0';
  185. + buf_skip(&buf, 4);
  186. +
  187. + if (!regex_match(buf_ptr(msg)))
  188. + continue;
  189. +
  190. + if ((req = strtok(buf_ptr(msg), "\r\n")) == NULL)
  191. + continue;
  192. +
  193. + if (strncmp(req, "GET ", 4) != 0 &&
  194. + strncmp(req, "POST ", 5) != 0 &&
  195. + strncmp(req, "CONNECT ", 8) != 0)
  196. + continue;
  197. +
  198. + if ((uri = strchr(req, ' ')) == NULL)
  199. + continue;
  200. +
  201. + *uri++ = '\0';
  202. + if (strncmp(uri, "http://", 7) == 0) {
  203. + for (uri += 7; *uri != '/'; uri++)
  204. + ;
  205. + }
  206. + user = vhost = referer = agent = NULL;
  207. +
  208. + while ((p = strtok(NULL, "\r\n")) != NULL) {
  209. + if (strncasecmp(p, "Authorization: Basic ", 21) == 0) {
  210. + p += 21;
  211. + i = base64_pton(p, p, strlen(p));
  212. + p[i] = '\0';
  213. + user = p;
  214. + if ((p = strchr(p, ':')) != NULL)
  215. + *p = '\0';
  216. + }
  217. + else if (strncasecmp(p, "Host: ", 6) == 0) {
  218. + vhost = p + 6;
  219. + }
  220. + else if (strncasecmp(p, "Referer: ", 9) == 0) {
  221. + referer = p + 9;
  222. + }
  223. + else if (strncasecmp(p, "User-Agent: ", 12) == 0) {
  224. + agent = p + 12;
  225. + }
  226. + else if (strncasecmp(p, "Content-length: ", 16) == 0) {
  227. + i = atoi(p + 16);
  228. + buf_tok(NULL, NULL, i);
  229. + }
  230. + }
  231. + if (user == NULL)
  232. + user = "-";
  233. + if (vhost == NULL)
  234. + vhost = libnet_addr2name4(addr->daddr, Opt_dns);
  235. + if (referer == NULL)
  236. + referer = "-";
  237. + if (agent == NULL)
  238. + agent = "-";
  239. +
  240. + printf("%s - %s [%s] \"%s http://%s%s\" - - \"%s\" \"%s\"\n",
  241. + libnet_addr2name4(addr->saddr, Opt_dns),
  242. + user, timestamp(), req, vhost, uri, referer, agent);
  243. + }
  244. + fflush(stdout);
  245. +
  246. + return (len - buf_len(&buf));
  247. +}
  248. +
  249. +static void
  250. +sniff_http_client(struct tcp_stream *ts, void **yoda)
  251. +{
  252. + int i;
  253. +
  254. + switch (ts->nids_state) {
  255. +
  256. + case NIDS_JUST_EST:
  257. + ts->server.collect = 1;
  258. +
  259. + case NIDS_DATA:
  260. + if (ts->server.count_new != 0) {
  261. + i = process_http_request(&ts->addr, ts->server.data,
  262. + ts->server.count -
  263. + ts->server.offset);
  264. + nids_discard(ts, i);
  265. + }
  266. + break;
  267. +
  268. + default:
  269. + if (ts->server.count != 0) {
  270. + process_http_request(&ts->addr, ts->server.data,
  271. + ts->server.count -
  272. + ts->server.offset);
  273. + }
  274. + break;
  275. + }
  276. +}
  277. +
  278. +static void
  279. +null_syslog(int type, int errnum, struct ip *iph, void *data)
  280. +{
  281. +}
  282. +
  283. +int
  284. +main(int argc, char *argv[])
  285. +{
  286. + extern char *optarg;
  287. + extern int optind;
  288. + int c;
  289. + struct nids_chksum_ctl chksum_ctl;
  290. +
  291. + while ((c = getopt(argc, argv, "i:p:nvh?V")) != -1) {
  292. + switch (c) {
  293. + case 'i':
  294. + nids_params.device = optarg;
  295. + break;
  296. + case 'p':
  297. + nids_params.filename = optarg;
  298. + break;
  299. + case 'n':
  300. + Opt_dns = 0;
  301. + break;
  302. + case 'v':
  303. + Opt_invert = 1;
  304. + break;
  305. + default:
  306. + usage();
  307. + }
  308. + }
  309. + argc -= optind;
  310. + argv += optind;
  311. +
  312. + if (argc > 0 && strlen(argv[0])) {
  313. + if ((pregex = (regex_t *) malloc(sizeof(*pregex))) == NULL)
  314. + err(1, "malloc");
  315. + if (regcomp(pregex, argv[0], REG_EXTENDED|REG_NOSUB) != 0)
  316. + errx(1, "invalid regular expression");
  317. + }
  318. + if (argc > 1) {
  319. + nids_params.pcap_filter = copy_argv(argv + 1);
  320. + }
  321. + else nids_params.pcap_filter = DEFAULT_PCAP_FILTER;
  322. +
  323. + nids_params.scan_num_hosts = 0;
  324. + nids_params.syslog = null_syslog;
  325. +
  326. + if (!nids_init())
  327. + errx(1, "%s", nids_errbuf);
  328. +
  329. + nids_register_tcp(sniff_http_client);
  330. +
  331. + if (nids_params.pcap_filter != NULL) {
  332. + if (nids_params.filename == NULL) {
  333. + warnx("listening on %s [%s]", nids_params.device,
  334. + nids_params.pcap_filter);
  335. + }
  336. + else {
  337. + warnx("using %s [%s]", nids_params.filename,
  338. + nids_params.pcap_filter);
  339. + }
  340. + }
  341. + else {
  342. + if (nids_params.filename == NULL) {
  343. + warnx("listening on %s", nids_params.device);
  344. + }
  345. + else {
  346. + warnx("using %s", nids_params.filename);
  347. + }
  348. + }
  349. +
  350. + chksum_ctl.netaddr = 0;
  351. + chksum_ctl.mask = 0;
  352. + chksum_ctl.action = NIDS_DONT_CHKSUM;
  353. +
  354. + nids_register_chksum_ctl(&chksum_ctl, 1);
  355. +
  356. + nids_run();
  357. +
  358. + /* NOTREACHED */
  359. +
  360. + exit(0);
  361. +}