home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / sectools / dsniff / record.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-17  |  3.7 KB  |  208 lines

  1. /*
  2.   record.c
  3.  
  4.   Copyright (c) 2000 Dug Song <dugsong@monkey.org>
  5.   
  6.   $Id: record.c,v 1.5 2000/05/17 16:02:40 dugsong Exp $
  7. */
  8.  
  9. #include "config.h"
  10. #include <sys/types.h>
  11. #include <rpc/rpc.h>
  12. #include <stdio.h>
  13. #include <time.h>
  14. #include <md5.h>
  15. #ifdef HAVE_DB_185_H
  16. #define DB_LIBRARY_COMPATIBILITY_API
  17. #include <db_185.h>
  18. #elif HAVE_DB_H
  19. #include <db.h>
  20. #endif
  21. #include <libnet.h>
  22. #include "options.h"
  23. #include "record.h"
  24.  
  25. struct rec {
  26.     time_t        time;
  27.     u_long        src;
  28.     u_long        dst;
  29.     u_int        proto;
  30.     u_short        sport;
  31.     u_short        dport;
  32.     struct netobj    name;
  33.     struct netobj    data;
  34. };
  35.     
  36. static DB *db;
  37.  
  38. static int
  39. xdr_rec(XDR *xdrs, struct rec *rec)
  40. {
  41.     if (xdr_u_long(xdrs, (u_long *)&rec->time) &&
  42.         xdr_u_long(xdrs, &rec->src) &&
  43.         xdr_u_long(xdrs, &rec->dst) &&
  44.         xdr_u_int(xdrs, &rec->proto) &&
  45.         xdr_u_short(xdrs, &rec->sport) &&
  46.         xdr_u_short(xdrs, &rec->dport) &&
  47.         xdr_netobj(xdrs, &rec->name) &&
  48.         xdr_netobj(xdrs, &rec->data)) {
  49.         return (1);
  50.     }
  51.     return (0);
  52. }
  53.  
  54. static void
  55. record_print(struct rec *rec)
  56. {
  57.     struct tm *tm;
  58.     char *srcp, *dstp, *protop, tstr[24], spstr[8], dpstr[8];
  59.     struct protoent *pr;
  60.     
  61.     tm = localtime(&rec->time);
  62.     strftime(tstr, sizeof(tstr), "%x %X", tm);
  63.     
  64.     srcp = libnet_host_lookup(rec->src, Opt_dns);
  65.     dstp = libnet_host_lookup(rec->dst, Opt_dns);
  66.  
  67.     if ((pr = getprotobynumber(rec->proto)) == NULL)
  68.         protop = "unknown";
  69.     else
  70.         protop = pr->p_name;
  71.     
  72.     snprintf(spstr, sizeof(spstr), "%d", rec->sport);
  73.     snprintf(dpstr, sizeof(dpstr), "%d", rec->dport);
  74.  
  75.     printf("-----------------\n");
  76.     printf("%s %s %s%s%s -> %s%s%s (%.*s)\n",
  77.            tstr, protop,
  78.            srcp, rec->sport ? "." : "", rec->sport ? spstr : "",
  79.            dstp, rec->dport ? "." : "", rec->dport ? dpstr : "",
  80.            (int) rec->name.n_len, rec->name.n_bytes);
  81.  
  82.     fwrite(rec->data.n_bytes, 1, rec->data.n_len, stdout);
  83.     printf("\n");
  84.     
  85.     fflush(stdout);
  86. }
  87.  
  88. static DBT *
  89. record_hash(struct rec *rec)
  90. {
  91.     static DBT key;
  92.     static u_char hash[16];
  93.     MD5_CTX ctx;
  94.  
  95.     /* Unique key: src/dst IPs, decode type, decode data. */
  96.     
  97.     MD5Init(&ctx);
  98.     MD5Update(&ctx, (u_char *) &rec->src, sizeof(rec->src));
  99.     MD5Update(&ctx, (u_char *) &rec->dst, sizeof(rec->dst));
  100.     MD5Update(&ctx, rec->name.n_bytes, rec->name.n_len);
  101.     MD5Update(&ctx, rec->data.n_bytes, rec->data.n_len);
  102.     MD5Final(hash, &ctx);
  103.  
  104.     key.data = hash;
  105.     key.size = sizeof(hash);
  106.     
  107.     return (&key);
  108. }
  109.  
  110. static int
  111. record_save(struct rec *rec)
  112. {
  113.     DBT *key, data;
  114.     XDR xdrs;
  115.     u_char buf[2048];
  116.     
  117.     xdrmem_create(&xdrs, buf, sizeof(buf), XDR_ENCODE);
  118.     
  119.     if (!xdr_rec(&xdrs, rec))
  120.         return (0);
  121.     
  122.     data.data = buf;
  123.     data.size = xdr_getpos(&xdrs);
  124.     
  125.     xdr_destroy(&xdrs);
  126.  
  127.     key = record_hash(rec);
  128.     
  129.     if (db->put(db, key, &data, R_NOOVERWRITE) == 0)
  130.         db->sync(db, 0);
  131.     
  132.     return (1);
  133. }
  134.  
  135. void
  136. record_dump(void)
  137. {
  138.     DBT key, data;
  139.     XDR xdrs;
  140.     struct rec rec;
  141.     
  142.     while (db->seq(db, &key, &data, R_NEXT) == 0) {    
  143.         memset(&rec, 0, sizeof(rec));
  144.         xdrmem_create(&xdrs, data.data, data.size, XDR_DECODE);
  145.         
  146.         if (xdr_rec(&xdrs, &rec)) {
  147.             record_print(&rec);
  148.         }
  149.         xdr_destroy(&xdrs);
  150.     }
  151. }
  152.  
  153. int
  154. record_init(char *file)
  155. {
  156.     int flags, mode;
  157.     
  158.     if (Opt_read) {
  159.         flags = O_RDONLY;
  160.         mode = 0;
  161.     }
  162.     else {
  163.         flags = O_RDWR|O_CREAT;
  164.         mode = S_IRUSR|S_IWUSR;
  165.     }
  166.     if ((db = dbopen(file, flags, mode, DB_BTREE, NULL)) == NULL)
  167.         return (0);
  168.  
  169.     return (1);
  170. }
  171.  
  172. int
  173. record(u_long src, u_long dst, int proto, u_short sport, u_short dport,
  174.        char *name, u_char *buf, int len)
  175. {
  176.     struct rec rec;
  177.  
  178.     rec.time = time(NULL);
  179.     
  180.     rec.src = src;
  181.     rec.dst = dst;
  182.     
  183.     rec.proto = proto;
  184.     
  185.     rec.sport = sport;
  186.     rec.dport = dport;
  187.     
  188.     rec.name.n_bytes = name;
  189.     rec.name.n_len = strlen(name);
  190.     
  191.     rec.data.n_bytes = buf;
  192.     rec.data.n_len = len;
  193.  
  194.     if (!Opt_read && !Opt_write)
  195.         record_print(&rec);
  196.  
  197.     record_save(&rec);
  198.  
  199.     return (1);
  200. }
  201.  
  202. void
  203. record_close(void)
  204. {
  205.     db->close(db);
  206. }
  207.  
  208.