home *** CD-ROM | disk | FTP | other *** search
/ Chaos Computer Club 1997 February / cccd_beta_feb_97.iso / contrib / hacktool / sniffer / sniff.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-28  |  8.4 KB  |  601 lines

  1. /*   Ethernet Monitor Program   */
  2.  
  3. /* Must be compiled with HUGE memory model */
  4.  
  5. #include <stdio.h>
  6. #include <fcntl.h>
  7. #include <conio.h>
  8. #include <dos.h>
  9. #include <alloc.h>
  10. #include <io.h>
  11.  
  12. #define ACCESS_TYPE 2
  13. #define RELEASE_TYPE 3
  14. #define SET_RCV_MODE 20
  15. #define TERMINATE 5
  16.  
  17. #define MAXETHB 1522
  18. #define MINETHB 59
  19.  
  20. typedef struct {
  21.  
  22.  unsigned char t;
  23.  unsigned char p;
  24.  
  25.  } screenchar;
  26.  
  27. typedef struct {
  28.  
  29.  screenchar c[25][80];
  30.  
  31.  } screen;
  32.  
  33. screen far *video;
  34.  
  35. struct SREGS sregs;
  36. union REGS inregs, outregs;
  37.  
  38. unsigned int handle;
  39.  
  40. int capture;
  41.  
  42. extern unsigned char CHECK1[6], CHECK2[6];
  43.  
  44. extern int CHECKFLG;
  45.  
  46. extern void far RECEIVER();
  47.  
  48. extern void huge *FIRST, huge *IN, huge *HIGH, huge *LIMIT;
  49.  
  50. struct {
  51.  
  52.    unsigned char source[6];
  53.    unsigned char dest[6];
  54.    unsigned long packets;
  55.    unsigned long bytes;
  56.    int changed;
  57.  
  58.    } traffic[20];
  59.  
  60. int trafficnext = 0;
  61. int trafficold = 0;
  62.  
  63. unsigned long totpackets = 0;
  64. unsigned long totbytes = 0;
  65. int totchanged = 0;
  66.  
  67. unsigned long lostcount = 0;
  68. unsigned long runtcount = 0;
  69.  
  70.  
  71. int convhex(arg, addr)
  72.  
  73. char *arg;
  74. unsigned char *addr;
  75.  
  76. {
  77.  
  78. int i, v, ec;
  79.  
  80. unsigned char *pnt;
  81. char *ipnt;
  82.  
  83. pnt = addr;
  84. ipnt = arg;
  85. ec = 0;
  86.  
  87. for (i = 0; i < 6; i++) {
  88.  
  89.    if(*ipnt >= '0' && *ipnt <= '9') {v = *ipnt - '0';}
  90.  
  91.      else if (*ipnt >= 'a' && *ipnt <= 'f') {v = *ipnt - 'a' + 10;}
  92.  
  93.         else if(*ipnt >= 'A' && *ipnt <= 'F') {v = *ipnt - 'A' + 10;}
  94.  
  95.         else {ec = 1;}
  96.  
  97.    v = v*16;
  98.    ipnt++;
  99.  
  100.    if(*ipnt >= '0' && *ipnt <= '9') {v = v + *ipnt - '0';}
  101.  
  102.      else if (*ipnt >= 'a' && *ipnt <= 'f') {v = v + *ipnt - 'a' + 10;}
  103.  
  104.         else if(*ipnt >= 'A' && *ipnt <= 'F') {v = v + *ipnt - 'A' + 10;}
  105.  
  106.         else {ec = 1;}
  107.  
  108.    *pnt = v;
  109.  
  110.    ipnt++;
  111.    pnt++;
  112.  
  113.    }
  114.  
  115. if (*ipnt) ec = 1;
  116.  
  117. return (ec);
  118.  
  119. }
  120.  
  121. unsigned long fp2addr(void huge *point )
  122.  
  123. {
  124.  
  125. return ((((unsigned long) FP_SEG(point)) << 4) +
  126.          ((unsigned long) FP_OFF(point)));
  127.  
  128. }
  129.  
  130. void huge *addr2fp(unsigned long addr)
  131.  
  132. {
  133.  
  134. return MK_FP((unsigned int) ((addr >> 4) & 0xffff),
  135.               (unsigned int) (addr & 0xf));
  136.  
  137. }
  138.  
  139. printhead(pnt)
  140.  
  141. void huge *pnt;
  142.  
  143. {
  144.  
  145. unsigned char far *work;
  146.  
  147. int i, j;
  148.  
  149. work = (unsigned char far *) pnt;
  150.  
  151. printf(" Dest ");
  152.  
  153. for (i = 0; i < 6 ; i++) {
  154.  
  155.    j = *work;
  156.  
  157.    printf("%2.2x ", j);
  158.  
  159.    work++;
  160.  
  161.    }
  162.  
  163. printf("  Source ");
  164.  
  165. for (i = 0; i < 6; i++) {
  166.  
  167.    j = *work;
  168.  
  169.    printf("%2.2x ", j);
  170.  
  171.    work++;
  172.  
  173.    }
  174.  
  175. printf("  Type ");
  176.  
  177. for (i = 0; i < 2; i++) {
  178.  
  179.    j = *work;
  180.  
  181.    printf("%2.2x", j);
  182.  
  183.    work++;
  184.  
  185.    }
  186.  
  187. printf("\n");
  188.  
  189. }
  190.  
  191. void putulong(val, line, col)
  192.  
  193. unsigned long val;
  194. int line,col;
  195.  
  196. {
  197.  
  198.    int i,j,k;
  199.    unsigned long ival, jval;
  200.    screenchar far *place;
  201.  
  202.    place = &(video->c[line][col+9]);
  203.    ival = val;
  204.  
  205.    for (i = 9; i >= 0; i--) {
  206.  
  207.       jval = ival / 10L;
  208.       j = ival - jval * 10L;
  209.       place->t = j + '0';
  210.       if (jval == 0L) break;
  211.       ival=jval;
  212.       place--;
  213.       }
  214.  
  215. /*   for (i--; i >= 0; i--) video->c[line][col+i].t = ' '; */
  216.  
  217. }
  218.  
  219. void placetext(text, line, col)
  220.  
  221. char *text;
  222. int line,col;
  223.  
  224. {
  225.  
  226.    screenchar far *place;
  227.    char far *nextchar;
  228.  
  229.    place = &(video->c[line][col]);
  230.    nextchar = text;
  231.  
  232.    while (*nextchar != 0) {
  233.  
  234.       place->t = *nextchar;
  235.       place++;
  236.       nextchar++;
  237.  
  238.    }
  239.  
  240. }
  241.  
  242. void putethaddr(addr, line, col)
  243.  
  244. unsigned char addr[6];
  245.  
  246. int line,col;
  247.  
  248. {
  249.  
  250.    screenchar far *place;
  251.    int i;
  252.    unsigned char v;
  253.  
  254.    place = &(video->c[line][col]);
  255.  
  256.    for (i = 0; i < 6; i++) {
  257.  
  258.       v = (addr[i] >> 4) & 0xf;
  259.       place->t = (v > 9) ? v + 'A' - 10 : v + '0';
  260.       place++;
  261.       v = addr[i] & 0xf;
  262.       place->t = (v > 9) ? v + 'A' - 10 : v + '0';
  263.       if (i == 5) break;
  264.       place++;
  265.       place->t = ':';
  266.       place++;
  267.  
  268.    }
  269.  
  270. }
  271.  
  272.  
  273.  
  274. main(argc, argv)
  275.  
  276. int argc;
  277. char *argv[];
  278.  
  279. {
  280.  
  281. unsigned long heapleft, len, testfirst, testlimit;
  282.  
  283. unsigned int far *lenpt;
  284.  
  285. unsigned int lenr;
  286.  
  287. unsigned char huge *out, far *testout;
  288.  
  289. unsigned int segleft, firstseg;
  290.  
  291. unsigned char far *sender;
  292. unsigned char far *receiver;
  293.  
  294. int i, j;
  295.  
  296. if (argc > 1) {
  297.  
  298.    if ((capture = open(argv[1], O_WRONLY|O_CREAT|O_TRUNC|O_BINARY)) < 0) {
  299.       puts("Cannot open capture file");
  300.       exit(1);
  301.       }
  302.  
  303.    }
  304.  
  305. if (argc > 2) {
  306.  
  307.    if(convhex(argv[2], &CHECK1[0])) {
  308.       puts("Illegal address");
  309.       exit(1);
  310.       }
  311.    CHECKFLG = 1;
  312.    }
  313.  
  314. if (argc > 3) {
  315.  
  316.    if(convhex(argv[3], &CHECK2[0])) {
  317.       puts("Illegal address");
  318.       exit(1);
  319.       }
  320.    CHECKFLG = 2;
  321.    }
  322.  
  323. segleft = allocmem(0xa000, &firstseg);
  324.  
  325. if (allocmem(segleft, &firstseg) != -1) {
  326.    puts("Unable to allocate memory");
  327.    exit(1);
  328.    }
  329.  
  330. FIRST = MK_FP(firstseg, 0);
  331.  
  332. heapleft = (long) segleft * (long) 16;
  333.  
  334. video = (screen far *) MK_FP(0xb800, 0);
  335.  
  336. for (i = 0; i < 25; i++) {
  337.  
  338.    for (j = 0; j < 80; j++) {
  339.  
  340.       video->c[i][j].p = 7;
  341.       video->c[i][j].t = ' ';
  342.  
  343.       }
  344.  
  345.    }
  346.  
  347. placetext("Packets", 0, 0);
  348. placetext("Avg 10s", 0, 20);
  349. placetext(".", 0, 35);
  350. placetext("Bytes", 0, 40);
  351. placetext("Avg 10s", 0, 58);
  352. placetext(".", 0, 73);
  353. placetext("Source", 3, 6);
  354. placetext("Destination", 3, 25);
  355. placetext("Packets", 3, 46);
  356. placetext("Bytes", 3, 63);
  357. placetext("Runt Packets", 24, 23);
  358. placetext("from", 24, 48);
  359.  
  360. IN = FIRST;
  361. HIGH = FIRST;
  362. testfirst = fp2addr(FIRST);
  363. testlimit = testfirst + heapleft;
  364. LIMIT = addr2fp(testlimit);
  365.  
  366. segread(&sregs);
  367.  
  368. inregs.h.ah = ACCESS_TYPE;
  369. inregs.h.al = 1;
  370. inregs.x.bx = 0xffff;
  371. inregs.h.dl = 0;
  372. inregs.x.cx = 0;
  373. inregs.x.di = FP_OFF(&RECEIVER);
  374. sregs.es = FP_SEG(&RECEIVER);
  375.  
  376. int86x(0x60, &inregs, &outregs, &sregs);
  377.  
  378. handle = outregs.x.ax;
  379.  
  380. inregs.h.ah = SET_RCV_MODE;
  381. inregs.x.bx = handle;
  382. inregs.x.cx = 6;
  383.  
  384. int86(0x60, &inregs, &outregs);
  385.  
  386. out = FIRST;
  387.  
  388. loop:
  389.  
  390. if (out != IN) {
  391.  
  392.   if ((HIGH != FIRST) && (out == HIGH)) {
  393.  
  394.      out = FIRST;
  395.  
  396.      goto loop;
  397.  
  398.      }
  399.  
  400.   lenpt = (int far *) out;
  401.   lenr = *lenpt;
  402.   testout = out;
  403.   testout += lenr + 6;
  404.   lenpt = (int far *) testout;
  405.  
  406.   if ((*lenpt != lenr) || (lenr == 0)) {
  407.  
  408.      placetext("Lost count", 24, 0);
  409.      lostcount++;
  410.      putulong(lostcount, 24, 11);
  411.  
  412.      out = IN;
  413.  
  414.      goto loop;
  415.  
  416.      }
  417.  
  418.   if(lenr < MINETHB) {
  419.  
  420.      runtcount++;
  421.      putulong(runtcount, 24, 36);
  422.  
  423.      if(lenr >= 14) putethaddr(out + 12, 24, 53);
  424.  
  425.      }
  426.  
  427.   if(lenr < 14) {
  428.  
  429.      out += lenr + 8;
  430.  
  431.      goto loop;
  432.  
  433.      }
  434.  
  435.   totbytes += lenr;
  436.   totpackets++;
  437.   totchanged = 1;
  438.  
  439.   for (i = 0; i < trafficnext; i++) {
  440.  
  441.      sender = out + 17;
  442.      receiver = out + 11;
  443.  
  444.      for (j = 5; j >= 0; j--) {
  445.  
  446.         if(*sender != traffic[i].source[j] ||
  447.            *receiver != traffic[i].dest[j]) goto trynext;
  448.  
  449.         sender--;
  450.         receiver--;
  451.  
  452.         }
  453.  
  454.      traffic[i].packets++;
  455.      traffic[i].bytes += lenr;
  456.      traffic[i].changed = 1;
  457.  
  458.      goto conndone;
  459.  
  460.   trynext: ;
  461.  
  462.      }
  463.  
  464.   if (trafficnext < 20) {
  465.  
  466.      for (i = 0; i < 6; i++) {
  467.  
  468.         traffic[trafficnext].source[i] = *(out + 12 + i);
  469.         traffic[trafficnext].dest[i] = *(out + 6 + i);
  470.  
  471.         }
  472.  
  473.      traffic[trafficnext].packets = 1;
  474.      traffic[trafficnext].bytes = lenr;
  475.      traffic[trafficnext].changed = 1;
  476.  
  477.      trafficnext++;
  478.  
  479.      }
  480.  
  481. conndone:
  482.  
  483.   out += 6;
  484.  
  485.  
  486.  
  487.   out += lenr;
  488.  
  489.   out += 2;
  490.  
  491.   goto loop;
  492.  
  493.   }
  494.  
  495. if(totchanged != 0) {
  496.  
  497.    putulong(totpackets, 0, 8);
  498.    putulong(totbytes, 0, 46);
  499.    totchanged = 0;
  500.  
  501.    for (i = trafficold; i < trafficnext; i++) {
  502.  
  503.       putethaddr(&traffic[i].source[0], i + 4, 0);
  504.       putethaddr(&traffic[i].dest[0], i + 4, 22);
  505.  
  506.       }
  507.  
  508.    trafficold = trafficnext;
  509.  
  510.    for (i = 0; i < trafficnext; i++) {
  511.  
  512.       if(traffic[i].changed > 0) {
  513.  
  514.          putulong(traffic[i].packets, i + 4, 44);
  515.          putulong(traffic[i].bytes, i + 4, 59);
  516.  
  517.          traffic[i].changed = 0;
  518.  
  519.          }
  520.  
  521.       }
  522.  
  523.    goto loop;
  524.  
  525.    }
  526.  
  527. if(kbhit()) goto done;
  528. delay(20);
  529.  
  530. goto loop;
  531.  
  532. done:
  533.  
  534. inregs.h.ah = RELEASE_TYPE;
  535. inregs.x.bx = handle;
  536.  
  537. int86(0x60, &inregs, &outregs);
  538.  
  539. if(argc > 1) {
  540.  
  541.    if (fp2addr(HIGH) > fp2addr(IN)) {
  542.  
  543.       len = 0;
  544.       out = HIGH;
  545.  
  546. goback:
  547.  
  548.       lenpt = (int far *)addr2fp(fp2addr(HIGH) - len - 2);
  549.       lenr = *lenpt;
  550.  
  551.       if ((fp2addr(HIGH) - len - lenr - 8) > fp2addr(IN)) {
  552.  
  553.          out = addr2fp(fp2addr(HIGH) - len - lenr - 8);
  554.          len += lenr + 8;
  555.  
  556.          goto goback;
  557.  
  558.          }
  559.  
  560.       if (len > 0) {
  561.  
  562.          while (len > 1024) {
  563.  
  564.             write(capture, out, 1024);
  565.             len -= 1024;
  566.             out += 1024;
  567.  
  568.             }
  569.  
  570.          write(capture, out, len);
  571.  
  572.          }
  573.  
  574.       }
  575.  
  576.    if (fp2addr(IN) > fp2addr(FIRST)) {
  577.  
  578.       out = FIRST;
  579.       len = fp2addr(IN) - fp2addr(FIRST);
  580.  
  581.       while(len > 1024) {
  582.  
  583.          write(capture, out, 1024);
  584.          len -= 1024;
  585.          out += 1024;
  586.  
  587.          }
  588.  
  589.       write(capture, out, len);
  590.  
  591.       }
  592.  
  593.    close(capture);
  594.  
  595.    }
  596.  
  597. freemem(firstseg);
  598.  
  599. }
  600.  
  601.