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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
/*
* record.c
*
* Copyright (c) 2000 Dug Song <dugsong@monkey.org>
*
* $Id: record.c,v 1.10 2001/03/15 08:33:04 dugsong Exp $
*/
#include "config.h"
#define _GNU_SOURCE
#include <sys/types.h>
#include <netinet/in.h>
#include <rpc/rpc.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>
#include <md5.h>
#ifdef WITH_BERKELEY_DB
#ifdef HAVE_DB_185_H
#define DB_LIBRARY_COMPATIBILITY_API
#include <db_185.h>
#elif HAVE_DB_H
#include <db.h>
#endif
#endif
#include <libnet.h>
#include "options.h"
#include "record.h"
#include "decode.h"
#include "crc32.h"
struct rec {
time_t time;
in_addr_t src;
in_addr_t dst;
u_int proto;
u_short sport;
u_short dport;
struct netobj name;
struct netobj data;
};
extern struct _dc_meta dc_meta;
uint32_t dup_table[16 * 1024];
size_t dup_idx;
static bool
dupdb_hit(uint32_t crc) {
if (memmem(dup_table, sizeof dup_table, &crc, sizeof crc) == NULL)
return false; // FALSE
return true;
}
static void
dupdb_add(uint32_t crc) {
dup_table[dup_idx++] = crc;
dup_idx = dup_idx % (sizeof dup_table / sizeof *dup_table);
}
static bool
dupdb_is_new(uint32_t crc) {
if (dupdb_hit(crc))
return false; // exists already
dupdb_add(crc);
return true;
}
#ifdef WITH_BERKELEY_DB
static DB *db;
static int
xdr_rec(XDR *xdrs, struct rec *rec)
{
if (xdr_u_long(xdrs, (u_long *)&rec->time) &&
xdr_u_long(xdrs, (u_long *)&rec->src) &&
xdr_u_long(xdrs, (u_long *)&rec->dst) &&
xdr_u_int(xdrs, &rec->proto) &&
xdr_u_short(xdrs, &rec->sport) &&
xdr_u_short(xdrs, &rec->dport) &&
xdr_netobj(xdrs, &rec->name) &&
xdr_netobj(xdrs, &rec->data)) {
return (1);
}
return (0);
}
#endif
static void
record_print(struct rec *rec)
{
struct tm *tm;
char *srcp, *dstp, *protop, tstr[64 + 24], spstr[8], dpstr[8];
struct protoent *pr;
char srcbuf[128], dstbuf[128];
tm = localtime(&rec->time);
if (Opt_color)
strftime(tstr, sizeof(tstr), CW CF"%x "CDW CF"%X", tm);
else
strftime(tstr, sizeof(tstr), "%x %X", tm);
if ((!Opt_color) || (Opt_dns)) {
srcp = libnet_addr2name4(rec->src, Opt_dns);
dstp = libnet_addr2name4(rec->dst, Opt_dns);
} else {
srcp = srcbuf;
dstp = dstbuf;
color_ip(srcbuf, sizeof srcbuf, rec->src);
color_ip(dstbuf, sizeof dstbuf, rec->dst);
}
if ((pr = getprotobynumber(rec->proto)) == NULL)
protop = "unknown";
else
protop = pr->p_name;
snprintf(spstr, sizeof(spstr), "%d", rec->sport);
snprintf(dpstr, sizeof(dpstr), "%d", rec->dport);
if (Opt_color) {
char *c_proto = CDB;
if (rec->proto == IPPROTO_TCP)
c_proto = CDG;
else if (rec->proto == IPPROTO_UDP)
c_proto = CDC;
printf(CW">>>>> %s %s%s"CN" %s%s%s "CF"->"CN" %s%s%s (%.*s)",
tstr, c_proto, protop,
srcp, rec->sport ? ":" : "", rec->sport ? spstr : "",
dstp, rec->dport ? ":" : "", rec->dport ? dpstr : "",
(int) rec->name.n_len, rec->name.n_bytes);
} else {
printf(">>>>> %s %s %s%s%s -> %s%s%s (%.*s)",
tstr, protop,
srcp, rec->sport ? ":" : "", rec->sport ? spstr : "",
dstp, rec->dport ? ":" : "", rec->dport ? dpstr : "",
(int) rec->name.n_len, rec->name.n_bytes);
}
if (dc_meta.is_hot) {
if (Opt_color)
printf(" "CR"[HOTHOT]\n"CN);
else
printf(" [HOTHOT]\n");
} else
printf("\n");
fwrite(rec->data.n_bytes, 1, rec->data.n_len, stdout);
printf("\n");
fflush(stdout);
}
#ifdef WITH_BERKELEY_DB
static DBT *
record_hash(struct rec *rec)
{
static DBT key;
static u_char hash[16];
MD5_CTX ctx;
/* Unique key: src/dst IPs, decode type, decode data. */
MD5Init(&ctx);
MD5Update(&ctx, (u_char *) &rec->src, sizeof(rec->src));
MD5Update(&ctx, (u_char *) &rec->dst, sizeof(rec->dst));
MD5Update(&ctx, rec->name.n_bytes, rec->name.n_len);
MD5Update(&ctx, rec->data.n_bytes, rec->data.n_len);
MD5Final(hash, &ctx);
key.data = hash;
key.size = sizeof(hash);
return (&key);
}
#endif
static int
record_save(struct rec *rec)
{
#ifdef WITH_BERKELEY_DB
DBT *key, data;
XDR xdrs;
u_char buf[2048];
xdrmem_create(&xdrs, buf, sizeof(buf), XDR_ENCODE);
if (!xdr_rec(&xdrs, rec))
return (0);
data.data = buf;
data.size = xdr_getpos(&xdrs);
xdr_destroy(&xdrs);
key = record_hash(rec);
if (db->put(db, key, &data, R_NOOVERWRITE) == 0)
db->sync(db, 0);
#endif
return (1);
}
void
record_dump(void)
{
#ifdef WITH_BERKELEY_DB
DBT key, data;
XDR xdrs;
struct rec rec;
while (db->seq(db, &key, &data, R_NEXT) == 0) {
memset(&rec, 0, sizeof(rec));
xdrmem_create(&xdrs, data.data, data.size, XDR_DECODE);
if (xdr_rec(&xdrs, &rec)) {
record_print(&rec);
}
xdr_destroy(&xdrs);
}
#endif
}
int
record_init(char *file)
{
#ifdef WITH_BERKELEY_DB
int flags, mode;
if (Opt_read) {
flags = O_RDONLY;
mode = 0;
}
else {
flags = O_RDWR|O_CREAT;
mode = S_IRUSR|S_IWUSR;
}
if ((db = dbopen(file, flags, mode, DB_BTREE, NULL)) == NULL)
return (0);
#endif
return (1);
}
int
record(in_addr_t src, in_addr_t dst, int proto, u_short sport, u_short dport,
char *name, u_char *buf, int len)
{
struct rec rec;
uint32_t crc;
if (!Opt_show_dups) {
// Check for duplicates.
crc = dc_meta.crc;
// Add entire buffer unless decoding function already added 'uniq' items.
if (crc == CRC32_INITIAL_STATE)
crc = crc32_update(buf, len, crc);
crc = crc32_update(&src, sizeof src, crc);
crc = crc32_update(&dst, sizeof dst, crc);
crc = crc32_update(&dport, sizeof dport, crc);
if (!dupdb_is_new(crc))
return 1;
}
rec.time = time(NULL);
rec.src = src;
rec.dst = dst;
rec.proto = proto;
rec.sport = sport;
rec.dport = dport;
rec.name.n_bytes = name;
rec.name.n_len = strlen(name);
rec.data.n_bytes = buf;
rec.data.n_len = len;
if (!Opt_read && !Opt_write)
record_print(&rec);
record_save(&rec);
return (1);
}
void
record_close(void)
{
#ifdef WITH_BERKELEY_DB
db->close(db);
#endif
}
|