Codebase list dnscat2 / af1e6903-ab20-4131-8fa2-eacf84b56729/main client / dnscat.c
af1e6903-ab20-4131-8fa2-eacf84b56729/main

Tree @af1e6903-ab20-4131-8fa2-eacf84b56729/main (Download .tar.gz)

dnscat.c @af1e6903-ab20-4131-8fa2-eacf84b56729/mainraw · history · blame

  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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
/* dnscat.c
 * Created March/2013
 * By Ron Bowes
 *
 * See LICENSE.md
 */
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#ifdef WIN32
#include "libs/my_getopt.h"
#else
#include <getopt.h>
#include <sys/socket.h>
#endif

#include "controller/controller.h"
#include "controller/session.h"
#include "libs/buffer.h"
#include "libs/ll.h"
#include "libs/log.h"
#include "libs/memory.h"
#include "libs/select_group.h"
#include "libs/udp.h"
#include "tunnel_drivers/driver_dns.h"
#include "tunnel_drivers/tunnel_driver.h"

/* Default options */
#define NAME    "dnscat2"
#define VERSION "v0.07"

/* Default options */
#define DEFAULT_DNS_HOST NULL
#define DEFAULT_DNS_PORT 53

/* Define these outside the function so they can be freed by the atexec() */
select_group_t *group         = NULL;
driver_dns_t   *tunnel_driver = NULL;
char           *system_dns    = NULL;

typedef struct
{
  char *process;
} exec_options_t;

typedef struct
{
  driver_type_t type;
  union
  {
    exec_options_t exec;
  } options;
} make_driver_t;

static make_driver_t *make_console()
{
  make_driver_t *make_driver = (make_driver_t*) safe_malloc(sizeof(make_driver_t));
  make_driver->type = DRIVER_TYPE_CONSOLE;

  return make_driver;
}

static make_driver_t *make_command()
{
  make_driver_t *make_driver = (make_driver_t*) safe_malloc(sizeof(make_driver_t));
  make_driver->type = DRIVER_TYPE_COMMAND;

  return make_driver;
}

static make_driver_t *make_ping()
{
  make_driver_t *make_driver = (make_driver_t*) safe_malloc(sizeof(make_driver_t));
  make_driver->type = DRIVER_TYPE_PING;

  return make_driver;
}

static make_driver_t *make_exec(char *process)
{
  make_driver_t *make_driver = (make_driver_t*) safe_malloc(sizeof(make_driver_t));
  make_driver->type = DRIVER_TYPE_EXEC;
  make_driver->options.exec.process = process;

  return make_driver;
}

static int create_drivers(ll_t *drivers)
{
  int num_created = 0;
  make_driver_t *this_driver;

  while((this_driver = ll_remove_first(drivers)))
  {
    num_created++;
    switch(this_driver->type)
    {
      case DRIVER_TYPE_CONSOLE:
        printf("Creating a console session!\n");
        controller_add_session(session_create_console(group, "console"));
        break;

      case DRIVER_TYPE_EXEC:
        printf("Creating a exec('%s') session!\n", this_driver->options.exec.process);
        controller_add_session(session_create_exec(group, this_driver->options.exec.process, this_driver->options.exec.process));
        break;

      case DRIVER_TYPE_COMMAND:
        printf("Creating a command session!\n");
        controller_add_session(session_create_command(group, "command"));
        break;

      case DRIVER_TYPE_PING:
        printf("Creating a ping session!\n");
        controller_add_session(session_create_ping(group, "ping"));
        break;
    }
    safe_free(this_driver);
  }

  /* Default to creating a command session. */
  if(num_created == 0)
  {
    num_created++;
    controller_add_session(session_create_command(group, "command"));
  }

  return num_created;
}

typedef struct
{
  char     *host;
  uint16_t  port;
  char     *server;
  char     *domain;
} dns_options_t;

typedef struct
{
  tunnel_driver_type_t type;
  union
  {
    dns_options_t dns_options;
  };
} make_tunnel_driver_t;

static void cleanup(void)
{
  LOG_WARNING("Terminating");

  controller_destroy();

  if(tunnel_driver)
    driver_dns_destroy(tunnel_driver);

  if(group)
    select_group_destroy(group);

  if(system_dns)
    safe_free(system_dns);

  print_memory();
}

void usage(char *name, char *message)
{
  fprintf(stderr,
"Usage: %s [args] [domain]\n"
"\n"
"General options:\n"
" --help -h               This page.\n"
" --version               Get the version.\n"
" --delay <ms>            Set the maximum delay between packets (default: 1000).\n"
"                         The minimum is technically 50 for technical reasons,\n"
"                         but transmitting too quickly might make performance\n"
"                         worse.\n"
" --steady                If set, always wait for the delay before sending.\n"
"                         the next message (by default, when a response is\n"
"                         received, the next message is immediately transmitted.\n"
" --max-retransmits <n>   Only re-transmit a message <n> times before giving up\n"
"                         and assuming the server is dead (default: 20).\n"
" --retransmit-forever    Set if you want the client to re-transmit forever\n"
"                         until a server turns up. This can be helpful, but also\n"
"                         makes the server potentially run forever.\n"
#ifndef NO_ENCRYPTION
" --secret                Set the shared secret; set the same one on the server\n"
"                         and the client to prevent man-in-the-middle attacks!\n"
" --no-encryption         Turn off encryption/authentication.\n"
#endif
"\n"
"Input options:\n"
" --console               Send/receive output to the console.\n"
" --exec -e <process>     Execute the given process and link it to the stream.\n"
/*" --listen -l <port>      Listen on the given port and link each connection to\n"
"                         a new stream\n"*/
" --command               Start an interactive 'command' session (default).\n"
" --ping                  Simply check if there's a dnscat2 server listening.\n"
"\n"
"Debug options:\n"
" -d                      Display more debug info (can be used multiple times).\n"
" -q                      Display less debug info (can be used multiple times).\n"
" --packet-trace          Display incoming/outgoing dnscat2 packets\n"
"\n"
"Driver options:\n"
" --dns <options>         Enable DNS mode with the given domain.\n"
"   domain=<domain>       The domain to make requests for.\n"
"   host=<hostname>       The host to listen on (default: 0.0.0.0).\n"
"   port=<port>           The port to listen on (default: 53).\n"
"   type=<type>           The type of DNS requests to use, can use\n"
"                         multiple comma-separated (options: TXT, MX,\n"
"                         CNAME, A, AAAA) (default: "DEFAULT_TYPES").\n"
"   server=<server>       The upstream server for making DNS requests\n"
"                         (default: autodetected = %s).\n"
#if 0
" --tcp <options>         Enable TCP mode.\n"
"   port=<port>           The port to listen on (default: 1234).\n"
"   host=<hostname>       The host to listen on (default: 0.0.0.0).\n"
#endif
"\n"
"Examples:\n"
" ./dnscat --dns domain=skullseclabs.org\n"
" ./dnscat --dns domain=skullseclabs.org,server=8.8.8.8,port=53\n"
" ./dnscat --dns domain=skullseclabs.org,port=5353\n"
" ./dnscat --dns domain=skullseclabs.org,port=53,type=A,CNAME\n"
#if 0
" --tcp port=1234\n"
" --tcp port=1234,host=127.0.0.1\n"
#endif
"\n"
"By default, a --dns driver on port 53 is enabled if a hostname is\n"
"passed on the commandline:\n"
"\n"
" ./dnscat skullseclabs.org\n"
"\n"
"ERROR: %s\n"
"\n"
, name, system_dns, message
);
  exit(0);
}

driver_dns_t *create_dns_driver_internal(select_group_t *group, char *domain, char *host, uint16_t port, char *type, char *server)
{
  if(!server && !domain)
  {
    printf("\n");
    printf("** WARNING!\n");
    printf("*\n");
    printf("* It looks like you're running dnscat2 with the system DNS server,\n");
    printf("* and no domain name!");
    printf("*\n");
    printf("* That's cool, I'm not going to stop you, but the odds are really,\n");
    printf("* really high that this won't work. You either need to provide a\n");
    printf("* domain to use DNS resolution (requires an authoritative server):\n");
    printf("*\n");
    printf("*     dnscat mydomain.com\n");
    printf("*\n");
    printf("* Or you have to provide a server to connect directly to:\n");
    printf("*\n");
    printf("*     dnscat --dns=server=1.2.3.4,port=53\n");
    printf("*\n");
    printf("* I'm going to let this keep running, but once again, this likely\n");
    printf("* isn't what you want!\n");
    printf("*\n");
    printf("** WARNING!\n");
    printf("\n");
  }

  if(!server)
    server = system_dns;

  if(!server)
  {
    LOG_FATAL("Couldn't determine the system DNS server! Please manually set");
    LOG_FATAL("the dns server with --dns server=8.8.8.8");
    LOG_FATAL("");
    LOG_FATAL("You can also fix this by creating a proper /etc/resolv.conf\n");
    exit(1);
  }

  printf("Creating DNS driver:\n");
  printf(" domain = %s\n", domain);
  printf(" host   = %s\n", host);
  printf(" port   = %u\n", port);
  printf(" type   = %s\n", type);
  printf(" server = %s\n", server);

  return driver_dns_create(group, domain, host, port, type, server);
}

driver_dns_t *create_dns_driver(select_group_t *group, char *options)
{
  char     *domain = NULL;
  char     *host = "0.0.0.0";
  uint16_t  port = 53;
  char     *type = DEFAULT_TYPES;
  char     *server = system_dns;

  char *token = NULL;

  for(token = strtok(options, ":,"); token && *token; token = strtok(NULL, ":,"))
  {
    char *name  = token;
    char *value = strchr(token, '=');

    if(value)
    {
      *value = '\0';
      value++;

      if(!strcmp(name, "domain"))
        domain = value;
      else if(!strcmp(name, "host"))
        host = value;
      else if(!strcmp(name, "port"))
        port = atoi(value);
      else if(!strcmp(name, "type"))
        type = value;
      else if(!strcmp(name, "server"))
        server = value;
      else
      {
        LOG_FATAL("Unknown --dns option: %s\n", name);
        exit(1);
      }
    }
    else
    {
      LOG_FATAL("ERROR parsing --dns: it has to be colon-separated name=value pairs!\n");
      exit(1);
    }
  }

  return create_dns_driver_internal(group, domain, host, port, type, server);
}

void create_tcp_driver(char *options)
{
  char *host = "0.0.0.0";
  uint16_t port = 1234;

  printf(" host   = %s\n", host);
  printf(" port   = %u\n", port);
}

int main(int argc, char *argv[])
{
  /* Define the options specific to the DNS protocol. */
  struct option long_options[] =
  {
    /* General options */
    {"help",    no_argument,       0, 0}, /* Help */
    {"h",       no_argument,       0, 0},
    {"version", no_argument,       0, 0}, /* Version */
#if 0
    {"name",    required_argument, 0, 0}, /* Name */
    {"n",       required_argument, 0, 0},
    {"download",required_argument, 0, 0}, /* Download */
    {"n",       required_argument, 0, 0},
    {"chunk",   required_argument, 0, 0}, /* Download chunk */
    {"isn",     required_argument, 0, 0}, /* Initial sequence number */
#endif

    {"delay",              required_argument, 0, 0}, /* Retransmit delay */
    {"steady",             no_argument,       0, 0}, /* Don't transmit immediately after getting a response. */
    {"max-retransmits",    required_argument, 0, 0}, /* Set the max retransmissions */
    {"retransmit-forever", no_argument,       0, 0}, /* Retransmit forever if needed */
#ifndef NO_ENCRYPTION
    {"secret",             required_argument, 0, 0}, /* Pre-shared secret */
    {"no-encryption",      no_argument,       0, 0}, /* Disable encryption */
#endif

    /* i/o options. */
    {"console", no_argument,       0, 0}, /* Enable console */
    {"exec",    required_argument, 0, 0}, /* Enable execute */
    {"e",       required_argument, 0, 0},
    {"command", no_argument,       0, 0}, /* Enable command (default) */
    {"ping",    no_argument,       0, 0}, /* Ping */

    /* Tunnel drivers */
    {"dns",     required_argument, 0, 0}, /* Enable DNS */
#if 0
    {"tcp",     optional_argument, 0, 0}, /* Enable TCP */
#endif

    /* Debug options */
    {"d",            no_argument, 0, 0}, /* More debug */
    {"q",            no_argument, 0, 0}, /* Less debug */
    {"packet-trace", no_argument, 0, 0}, /* Trace packets */

    /* Sentry */
    {0,              0,                 0, 0}  /* End */
  };

  int               c;
  int               option_index;
  const char       *option_name;

  NBBOOL            tunnel_driver_created = FALSE;
  ll_t             *drivers_to_create     = ll_create(NULL);
  uint32_t          drivers_created       = 0;

  log_level_t       min_log_level = LOG_LEVEL_WARNING;

  group = select_group_create();
  system_dns = dns_get_system();

  /* Seed with the current time; not great, but it'll suit our purposes. */
  srand((unsigned int)time(NULL));

  /* This is required for win32 support. */
  winsock_initialize();

  /* Set the default log level */
  log_set_min_console_level(min_log_level);

  /* Parse the command line options. */
  opterr = 0;
  while((c = getopt_long_only(argc, argv, "", long_options, &option_index)) != -1)
  {
    switch(c)
    {
      case 0:
        option_name = long_options[option_index].name;

        /* General options */
        if(!strcmp(option_name, "help") || !strcmp(option_name, "h"))
        {
          usage(argv[0], "--help requested");
        }
        if(!strcmp(option_name, "version"))
        {
          printf(NAME" "VERSION" (client)\n");
          exit(0);
        }
        else if(!strcmp(option_name, "isn"))
        {
          uint16_t isn = (uint16_t) (atoi(optarg) & 0xFFFF);
          debug_set_isn(isn);
        }
        else if(!strcmp(option_name, "delay"))
        {
          int delay = (int) atoi(optarg);
          session_set_delay(delay);
          LOG_INFO("Setting delay between packets to %dms", delay);
        }
        else if(!strcmp(option_name, "steady"))
        {
          session_set_transmit_immediately(FALSE);
        }
        else if(!strcmp(option_name, "max-retransmits"))
        {
          controller_set_max_retransmits(atoi(optarg));
        }
        else if(!strcmp(option_name, "retransmit-forever"))
        {
          controller_set_max_retransmits(-1);
        }
#ifndef NO_ENCRYPTION
        else if(!strcmp(option_name, "secret"))
        {
          session_set_preshared_secret(optarg);
        }
        else if(!strcmp(option_name, "no-encryption"))
        {
          session_set_encryption(FALSE);
        }
#endif

        /* i/o drivers */
        else if(!strcmp(option_name, "console"))
        {
          ll_add(drivers_to_create, ll_32(drivers_created++), make_console());

/*          session = session_create_console(group, "console");
          controller_add_session(session); */
        }
        else if(!strcmp(option_name, "exec") || !strcmp(option_name, "e"))
        {
          ll_add(drivers_to_create, ll_32(drivers_created++), make_exec(optarg));

/*          session = session_create_exec(group, optarg, optarg);
          controller_add_session(session); */
        }
        else if(!strcmp(option_name, "command"))
        {
          ll_add(drivers_to_create, ll_32(drivers_created++), make_command());

/*          session = session_create_command(group, "command");
          controller_add_session(session); */
        }
        else if(!strcmp(option_name, "ping"))
        {
          ll_add(drivers_to_create, ll_32(drivers_created++), make_ping());

/*          session = session_create_ping(group, "ping");
          controller_add_session(session); */
        }

        /* Tunnel driver options */
        else if(!strcmp(option_name, "dns"))
        {
          tunnel_driver_created = TRUE;
          tunnel_driver = create_dns_driver(group, optarg);
        }
        else if(!strcmp(option_name, "tcp"))
        {
          tunnel_driver_created = TRUE;
          create_tcp_driver(optarg);
        }

        /* Debug options */
        else if(!strcmp(option_name, "d"))
        {
          if(min_log_level > 0)
          {
            min_log_level--;
            log_set_min_console_level(min_log_level);
          }
        }
        else if(!strcmp(option_name, "q"))
        {
          min_log_level++;
          log_set_min_console_level(min_log_level);
        }
        else if(!strcmp(option_name, "packet-trace"))
        {
          session_enable_packet_trace();
        }
        else
        {
          usage(argv[0], "Unknown option");
        }
        break;

      case '?':
      default:
        usage(argv[0], "Unrecognized argument");
        break;
    }
  }

  create_drivers(drivers_to_create);
  ll_destroy(drivers_to_create);

  if(tunnel_driver_created && argv[optind])
  {
    printf("It looks like you used --dns and also passed a domain on the commandline.\n");
    printf("That's not allowed! Either use '--dns domain=xxx' or don't use a --dns\n");
    printf("argument!\n");
    exit(1);
  }

  /* If no output was set, use the domain, and use the last option as the
   * domain. */
  if(!tunnel_driver_created)
  {
    /* Make sure they gave a domain. */
    if(optind >= argc)
    {
      printf("Starting DNS driver without a domain! This will only work if you\n");
      printf("are directly connecting to the dnscat2 server.\n");
      printf("\n");
      printf("You'll need to use --dns server=<server> if you aren't.\n");
      tunnel_driver = create_dns_driver_internal(group, NULL, "0.0.0.0", 53, DEFAULT_TYPES, NULL);
    }
    else
    {
      tunnel_driver = create_dns_driver_internal(group, argv[optind], "0.0.0.0", 53, DEFAULT_TYPES, NULL);
    }
  }

  /* Be sure we clean up at exit. */
  atexit(cleanup);

  /* Start the driver! */
  driver_dns_go(tunnel_driver);

  return 0;
}