File: detect_sniffer6.c

package info (click to toggle)
thc-ipv6 3.6-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 2,116 kB
  • sloc: ansic: 52,618; sh: 529; makefile: 77; perl: 34
file content (147 lines) | stat: -rw-r--r-- 4,651 bytes parent folder | download
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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <time.h>
#include <pcap.h>
#include <sys/timeb.h>
#include "thc-ipv6.h"

struct timespec ts, ts2;
int found = 0, oneonly = 0;
char doubles[256][16];

void help(char *prg) {
  printf("%s %s (c) 2019 by %s %s\n\n", prg, VERSION, AUTHOR, RESOURCE);
  printf("Syntax: %s interface [target6]\n\n", prg);
  printf("Tests if systems on the local LAN are sniffing.\n");
  printf("Works against Windows, Linux, OS/X and *BSD\n");
  printf("If no target is given, the link-local-all-nodes address is used, which\nhowever not always works.\n");
  exit(-1);
}

void alarming() {
  if (found == 0)
    printf("No packets received, no vulnerable system seems to be sniffing.\n");
  else
    printf("%d sniffing host%s detected.\n", found, found == 1 ? "" : "s");
  exit(0);
}

void check_packets(u_char *pingdata, const struct pcap_pkthdr *header, const unsigned char *data) {
  int len = header->caplen, ok = 0, i;
  unsigned char *ptr = (unsigned char *) data;

  if (do_hdr_size) {
    len -= do_hdr_size;
    ptr += do_hdr_size;
    if ((ptr[0] & 240) != 0x60)
      return;
  } else {
    len -= 14;
    ptr += 14;
  }

  if (len < 136)                // ignoring too short packet
    return;
  for (i = 0; i < 8 && ok == 0; i++)
    if (memcmp(pingdata, data + 106 + i, 8) == 0)
      ok = 1;

  if (ok) {
    if (found) {
      for (i = 0; i < found && ok == 1; i++)
        if (memcmp(doubles[i], thc_ipv62notation(ptr + 8), 16) == 0)
          ok = 0;
    }
    if (ok) {
      printf(" Sniffing host detected: %s\n", thc_ipv62notation(ptr + 8));
      memcpy(doubles[found], thc_ipv62notation(ptr + 8), 16);
      found++;
      if (oneonly)
        alarming();
    }
  }
}

int main(int argc, char *argv[]) {
  unsigned char *pkt1 = NULL, *pkt2 = NULL, buf[2096] = "thcping6", buf2[6];
  unsigned char *src6 = NULL, *dst6 = NULL;
  unsigned char dmac[7] = { 0x33, 0x33, 0xff, 0x01, 0x00, 0xfe, 0x00 };
  char string[255] = "icmp6 and dst ", *interface;
  int pkt1_len = 0, pkt2_len = 0, flags = 0, j;
  pcap_t *p;

  if (argc < 2 || argc > 3 || strncmp(argv[1], "-h", 2) == 0)
    help(argv[0]);

  setvbuf(stdout, NULL, _IONBF, 0);
  setvbuf(stderr, NULL, _IONBF, 0);

  if (getenv("THC_IPV6_PPPOE") != NULL || getenv("THC_IPV6_6IN4") != NULL) printf("WARNING: %s is not working with injection!\n", argv[0]);

  memset(buf, 0, sizeof(buf));
  for (j = 0; j < (128 / 8); j++)
    memcpy(buf + j * 8, "thcsniff", 8);
  memset(buf2 + 2, 0, 4);
  buf2[0] = NXT_INVALID;
  buf2[1] = 1;

  interface = argv[1];
  if (argc == 3) {
    if ((dst6 = thc_resolve6(argv[2])) == NULL) {
      fprintf(stderr, "Error: not a valid target: %s\n", argv[2]);
      exit(-1);
    }
    if (dst6[0] != 0xff)
      oneonly = 1;
  } else
    dst6 = thc_resolve6("ff02::1");
  if ((src6 = thc_get_own_ipv6(interface, dst6, PREFER_LINK)) == NULL) {
    fprintf(stderr, "Error: no IPv6 address found for interface %s!\n", interface);
    exit(-1);
  }

  if ((pkt1 = thc_create_ipv6_extended(interface, PREFER_LINK, &pkt1_len, src6, dst6, 255, 0, 0, 0, 0)) == NULL)
    return -1;
  if (thc_add_icmp6(pkt1, &pkt1_len, ICMP6_ECHOREQUEST, 0, flags, (unsigned char *) &buf, 128, 0) < 0)
    return -1;
  if (thc_generate_pkt(interface, NULL, dmac, pkt1, &pkt1_len) < 0) {
    fprintf(stderr, "Error: Can not generate packet, exiting ...\n");
    exit(-1);
  }

  if ((pkt2 = thc_create_ipv6_extended(interface, PREFER_LINK, &pkt2_len, src6, dst6, 255, 0, 0, 0, 0)) == NULL)
    return -1;
  if (thc_add_hdr_dst(pkt2, &pkt2_len, (unsigned char *) &buf2, sizeof(buf2)) < 0)
    return -1;
  if (thc_add_icmp6(pkt2, &pkt2_len, ICMP6_ECHOREQUEST, 0, flags, (unsigned char *) &buf, 128, 0) < 0)
    return -1;
  if (thc_generate_pkt(interface, NULL, dmac, pkt2, &pkt2_len) < 0) {
    fprintf(stderr, "Error: Can not generate packet, exiting ...\n");
    exit(-1);
  }

  strcat(string, thc_ipv62notation(src6));
  if ((p = thc_pcap_init(interface, string)) == NULL) {
    fprintf(stderr, "Error: could not capture on interface %s with string %s\n", interface, string);
    exit(-1);
  }
  signal(SIGALRM, alarming);
  alarm(3);
  printf("Sending sniffer detection packets to %s\n", thc_ipv62notation(dst6));
  thc_send_pkt(interface, pkt1, &pkt1_len);
  thc_send_pkt(interface, pkt2, &pkt2_len);
  thc_send_pkt(interface, pkt1, &pkt1_len);
  thc_send_pkt(interface, pkt2, &pkt2_len);
  while (1) {
    thc_pcap_check(p, (char *) check_packets, buf);
  }

  return 0;                     // not reached
}