Codebase list dnscat2 / 9b31863 client / controller / session.c
9b31863

Tree @9b31863 (Download .tar.gz)

session.c @9b31863raw · 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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
/* session.c
 * By Ron Bowes
 *
 * See LICENSE.md
 */

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifndef WIN32
#include <unistd.h>
#include <sys/time.h>
#endif

#include "controller/packet.h"
#include "libs/buffer.h"
#include "libs/log.h"
#include "libs/memory.h"
#include "libs/select_group.h"

#ifndef NO_ENCRYPTION
#include "libs/crypto/encryptor.h"
#endif

#include "session.h"

/* Allow the user to override the initial sequence number. */
static uint32_t isn = 0xFFFFFFFF;

/* Enable/disable packet tracing. */
static NBBOOL packet_trace;

/* The amount of delay between packets. */
static int packet_delay = 1000;

/* Transmit instantly when data is received. */
static NBBOOL transmit_instantly_on_data = TRUE;

#ifndef NO_ENCRYPTION
/* Should we set up encryption? */
static NBBOOL do_encryption = TRUE;

/* Pre-shared secret (used for authentication) */
static char *preshared_secret = NULL;
#endif

/* Define a handler function pointer. */
typedef NBBOOL(packet_handler)(session_t *session, packet_t *packet);

#ifndef NO_ENCRYPTION
static NBBOOL should_we_encrypt(session_t *session)
{
  return do_encryption && (session->state != SESSION_STATE_BEFORE_INIT);
}
#endif

static uint64_t time_ms()
{
#ifdef WIN32
  FILETIME ft;
  GetSystemTimeAsFileTime(&ft);

  return ((uint64_t)ft.dwLowDateTime + ((uint64_t)(ft.dwHighDateTime) << 32LL)) / 10000;
#else
  struct timeval tv;
  gettimeofday(&tv, NULL);
  return (tv.tv_sec) * 1000 + (tv.tv_usec) / 1000 ;
#endif
}

/* Decide whether or not we should transmit data yet. */
static NBBOOL can_i_transmit_yet(session_t *session)
{
  if(time_ms() - session->last_transmit > packet_delay)
    return TRUE;
  return FALSE;
}

static void you_can_transmit_now(session_t *session)
{
  session->last_transmit = 0;
}

/* Polls the driver for data and puts it in our own buffer. This is necessary
 * because the session needs to ACK data and such. */
static void poll_driver_for_data(session_t *session)
{
  size_t length = -1;

  /* Read all the data we can. */
  uint8_t *data = driver_get_outgoing(session->driver, &length, -1);

  /* If a driver returns NULL, it means it's done - once the driver is
   * done and all our data is sent, go into 'shutdown' mode. */
  if(!data)
  {
    if(buffer_get_remaining_bytes(session->outgoing_buffer) == 0)
      session_kill(session);
  }
  else
  {
    if(length)
      buffer_add_bytes(session->outgoing_buffer, data, length);

    safe_free(data);
  }
}

uint8_t *session_get_outgoing(session_t *session, size_t *packet_length, size_t max_length)
{
  packet_t *packet       = NULL;
  uint8_t  *packet_bytes = NULL;
  uint8_t  *data         = NULL;
  size_t    data_length  = -1;

  /* Suck in any data we can from the driver. */
  poll_driver_for_data(session);

  /* Don't transmit too quickly without receiving anything. */
  if(!can_i_transmit_yet(session))
    return NULL;

#ifndef NO_ENCRYPTION
  /* If we're in encryption mode, we have to save 8 bytes for the encrypted_packet header. */
  if(should_we_encrypt(session))
  {
    max_length -= 8;

    if(max_length <= 0)
    {
      LOG_FATAL("There isn't enough room in this protocol to encrypt packets!");
      exit(1);
    }
  }
#endif

  /* It's pretty ugly, but I don't see any other way, since ping requires
   * special packets we have to handle it separately. */
  if(session->is_ping)
  {
    /* Read data without consuming it (ie, leave it in the buffer till it's ACKed) */
    data = buffer_read_remaining_bytes(session->outgoing_buffer, &data_length, max_length - packet_get_ping_size(), FALSE);
    packet = packet_create_ping(session->id, (char*)data);
    safe_free(data);

    LOG_INFO("In PING, sending a PING packet (%zd bytes of data...)", data_length);
  }
  else
  {
    switch(session->state)
    {
#ifndef NO_ENCRYPTION
      case SESSION_STATE_BEFORE_INIT:
        packet = packet_create_enc(session->id, 0);
        packet_enc_set_init(packet, session->encryptor->my_public_key);
        break;

      case SESSION_STATE_BEFORE_AUTH:
        packet = packet_create_enc(session->id, 0);
        packet_enc_set_auth(packet, session->encryptor->my_authenticator);
        break;
#endif

      case SESSION_STATE_NEW:
        packet = packet_create_syn(session->id, session->my_seq, (options_t)0);

        if(session->is_command)
          packet_syn_set_is_command(packet);

        if(session->name)
          packet_syn_set_name(packet, session->name);

        break;

      case SESSION_STATE_ESTABLISHED:
        /* Re-negotiate encryption if we have to */
#ifndef NO_ENCRYPTION
        if(should_we_encrypt(session))
        {
          if(encryptor_should_we_renegotiate(session->encryptor))
          {
            if(session->new_encryptor)
            {
              LOG_FATAL("The server didn't respond to our re-negotiation request! Waiting...");
              return FALSE;
            }

            LOG_WARNING("Wow, this session is old! Time to re-negotiate encryption keys!");

            /* Create a new encryptor. */
            session->new_encryptor = encryptor_create(preshared_secret);

            packet = packet_create_enc(session->id, 0);
            packet_enc_set_init(packet, session->new_encryptor->my_public_key);

            break;
          }
        }
#endif
        /* Read data without consuming it (ie, leave it in the buffer till it's ACKed) */
        data = buffer_read_remaining_bytes(session->outgoing_buffer, &data_length, max_length - packet_get_msg_size(session->options), FALSE);
        LOG_INFO("In SESSION_STATE_ESTABLISHED, sending a MSG packet (SEQ = 0x%04x, ACK = 0x%04x, %zd bytes of data...)", session->my_seq, session->their_seq, data_length);

        if(data_length == 0 && session->is_shutdown)
          packet = packet_create_fin(session->id, "Stream closed");
        else
          packet = packet_create_msg(session->id, session->my_seq, session->their_seq, data, data_length);

        safe_free(data);

        break;

      default:
        LOG_FATAL("Wound up in an unknown state: 0x%x", session->state);
        exit(1);
    }
  }

  if(packet)
  {
    if(packet_trace)
    {
      printf("OUTGOING: ");
      packet_print(packet, session->options);
      printf("\n");
    }

    packet_bytes = packet_to_bytes(packet, packet_length, session->options);
    packet_destroy(packet);

#ifndef NO_ENCRYPTION
    if(should_we_encrypt(session))
    {
      buffer_t *packet_buffer = buffer_create(BO_BIG_ENDIAN);

      buffer_add_bytes(packet_buffer, packet_bytes, *packet_length);
      safe_free(packet_bytes);

      encryptor_encrypt_buffer(session->encryptor, packet_buffer);
      encryptor_sign_buffer(session->encryptor, packet_buffer);

      packet_bytes = buffer_create_string_and_destroy(packet_buffer, packet_length);
    }
#endif

    session->last_transmit = time_ms();
    session->missed_transmissions++;
  }

  return packet_bytes;
}

#ifndef NO_ENCRYPTION
static NBBOOL _handle_enc_before_init(session_t *session, packet_t *packet)
{
  if(packet->body.enc.subtype != PACKET_ENC_SUBTYPE_INIT)
  {
    LOG_FATAL("Received an unexpected encryption packet for this state: 0x%04x!", packet->body.enc.subtype);
    exit(1);
  }

  if(!encryptor_set_their_public_key(session->encryptor, packet->body.enc.public_key))
  {
    LOG_FATAL("Failed to calculate a shared secret!");
    exit(1);
  }

  if(LOG_LEVEL_INFO >= log_get_min_console_level())
    encryptor_print(session->encryptor);

  if(preshared_secret)
  {
    session->state = SESSION_STATE_BEFORE_AUTH;
  }
  else
  {
    session->state = SESSION_STATE_NEW;

    printf("\n");
    printf("Encrypted session established! For added security, please verify the server also displays this string:\n");
    printf("\n");
    encryptor_print_sas(session->encryptor);
    printf("\n");
  }

  return TRUE;
}

static NBBOOL _handle_enc_renegotiate(session_t *session, packet_t *packet)
{
  if(packet->body.enc.subtype != PACKET_ENC_SUBTYPE_INIT)
  {
    LOG_FATAL("Received an unexpected encryption packet for this state: 0x%04x!", packet->body.enc.subtype);
    exit(1);
  }

  if(!session->encryptor)
  {
    LOG_FATAL("Received an unexpected renegotiation from the server!");
    exit(1);
  }

  if(!encryptor_set_their_public_key(session->new_encryptor, packet->body.enc.public_key))
  {
    LOG_FATAL("Failed to calculate a shared secret for renegotiation!");
    exit(1);
  }

  LOG_WARNING("Server responded to re-negotiation request! Switching to new keys!");

  /* Kill the old encryptor and replace it with the new one. */
  encryptor_destroy(session->encryptor);
  session->encryptor = session->new_encryptor;
  session->new_encryptor = NULL;

  if(LOG_LEVEL_INFO >= log_get_min_console_level())
    encryptor_print(session->encryptor);

  return TRUE;
}

static NBBOOL _handle_enc_before_auth(session_t *session, packet_t *packet)
{
  if(packet->body.enc.subtype != PACKET_ENC_SUBTYPE_AUTH)
  {
    LOG_FATAL("Received an unexpected encryption packet for this state: 0x%04x!", packet->body.enc.subtype);
    exit(1);
  }

  /* Check their authenticator. */
  if(memcmp(packet->body.enc.authenticator, session->encryptor->their_authenticator, 32))
  {
    LOG_FATAL("Their authenticator was wrong! That likely means something weird is happening on the newtork...");
    exit(1);
  }

  printf("\n");
  printf("** Peer verified with pre-shared secret!\n");
  printf("\n");

  session->state = SESSION_STATE_NEW;

  return TRUE;
}
#endif

static NBBOOL _handle_syn_new(session_t *session, packet_t *packet)
{
  session->their_seq = packet->body.syn.seq;
  session->options   = (options_t) packet->body.syn.options;

  /* We can send a response right away */
  you_can_transmit_now(session);

  /* We can reset the transmission counter. */
  session->missed_transmissions = 0;

  /* Update the state. */
  session->state                = SESSION_STATE_ESTABLISHED;

  printf("Session established!\n");

  return TRUE;
}

static NBBOOL _handle_msg_established(session_t *session, packet_t *packet)
{
  NBBOOL send_right_away = FALSE;

  LOG_INFO("In SESSION_STATE_ESTABLISHED, received a MSG");

  /* Validate the SEQ */
  if(packet->body.msg.seq == session->their_seq)
  {
    /* Verify the ACK is sane */
    uint16_t bytes_acked = packet->body.msg.ack - session->my_seq;

    /* If there's still bytes waiting in the buffer.. */
    if(bytes_acked <= buffer_get_remaining_bytes(session->outgoing_buffer))
    {
      /* Since we got a valid response back, the connection isn't dying. */
      session->missed_transmissions = 0;

      /* Reset the retransmit counter since we got some valid data. */
      if(bytes_acked > 0)
      {
        /* Only reset the counter if we want to re-transmit
         * right away. */
        if(transmit_instantly_on_data)
        {
          you_can_transmit_now(session);
          session->missed_transmissions = 0;
          send_right_away = TRUE;
        }
      }

      /* Increment their sequence number */
      session->their_seq = (session->their_seq + packet->body.msg.data_length) & 0xFFFF;

      /* Remove the acknowledged data from the buffer */
      buffer_consume(session->outgoing_buffer, bytes_acked);

      /* Increment my sequence number */
      if(bytes_acked != 0)
      {
        session->my_seq = (session->my_seq + bytes_acked) & 0xFFFF;
      }

      /* Print the data, if we received any, and then immediately receive more. */
      if(packet->body.msg.data_length > 0)
      {
        driver_data_received(session->driver, packet->body.msg.data, packet->body.msg.data_length);
        you_can_transmit_now(session);
      }
    }
    else
    {
      LOG_WARNING("Bad ACK received (%d bytes acked; %d bytes in the buffer)", bytes_acked, buffer_get_remaining_bytes(session->outgoing_buffer));
    }
  }
  else
  {
    LOG_WARNING("Bad SEQ received (Expected %d, received %d)", session->their_seq, packet->body.msg.seq);
  }

  return send_right_away;
}

static NBBOOL _handle_fin(session_t *session, packet_t *packet)
{
  LOG_FATAL("Received FIN: (reason: '%s') - closing session", packet->body.fin.reason);
  you_can_transmit_now(session);
  session->missed_transmissions = 0;
  session_kill(session);

  return TRUE;
}

static NBBOOL _handle_error(session_t *session, packet_t *packet)
{
  LOG_FATAL("Received a %s packet in state %s!", packet_type_to_string(packet->packet_type), session_state_to_string(session->state));
  exit(1);
}

static NBBOOL _handle_warning(session_t *session, packet_t *packet)
{
  LOG_WARNING("Received a %s packet in state %s; ignoring!", packet_type_to_string(packet->packet_type), session_state_to_string(session->state));

  return FALSE;
}

NBBOOL session_data_incoming(session_t *session, uint8_t *data, size_t length)
{
  uint8_t *packet_bytes;

  /* Parse the packet to get the session id */
  packet_t *packet;

  /* Set to TRUE if data was properly ACKed and we should send more right away. */
  NBBOOL send_right_away = FALSE;

  /* Suck in any data we can from the driver. */
  poll_driver_for_data(session);

  /* Make a copy of the data so we can mess around with it. */
  packet_bytes = safe_malloc(length);
  memcpy(packet_bytes, data, length);

  /* I no longer want to risk messing with data by accident, so get rid of it. */
  data = NULL;

#ifndef NO_ENCRYPTION
  if(should_we_encrypt(session))
  {
    buffer_t *packet_buffer = buffer_create_with_data(BO_BIG_ENDIAN, packet_bytes, length);
    safe_free(packet_bytes);

    if(!encryptor_check_signature(session->encryptor, packet_buffer))
    {
      LOG_WARNING("Server's signature was wrong! Ignoring!");
      return FALSE;
    }

    /* Decrypt the buffer. */
    encryptor_decrypt_buffer(session->encryptor, packet_buffer, NULL);

    /* Switch to the decrypted data. */
    packet_bytes = buffer_create_string_and_destroy(packet_buffer, &length);
  }
#endif

  /* Parse the packet. */
  packet = packet_parse(packet_bytes, length, session->options);

  /* Free the memory we allocated. */
  safe_free(packet_bytes);

  /* Print packet data if we're supposed to. */
  if(packet_trace)
  {
    printf("INCOMING: ");
    packet_print(packet, session->options);
    printf("\n");
  }

  if(session->is_ping && packet->packet_type == PACKET_TYPE_PING)
  {
    /* This only returns if the receive is bad. */
    driver_data_received(session->driver, (uint8_t*)packet->body.ping.data, strlen(packet->body.ping.data));
  }
  else
  {
    /* Handlers for the various states / messages */
    packet_handler *handlers[PACKET_TYPE_COUNT_NOT_PING][SESSION_STATE_COUNT];

#ifndef NO_ENCRYPTION
    handlers[PACKET_TYPE_SYN][SESSION_STATE_BEFORE_INIT]    = _handle_error;
    handlers[PACKET_TYPE_SYN][SESSION_STATE_BEFORE_AUTH]    = _handle_error;
#endif
    handlers[PACKET_TYPE_SYN][SESSION_STATE_NEW]            = _handle_syn_new;
    handlers[PACKET_TYPE_SYN][SESSION_STATE_ESTABLISHED]    = _handle_warning;

#ifndef NO_ENCRYPTION
    handlers[PACKET_TYPE_MSG][SESSION_STATE_BEFORE_INIT]    = _handle_error;
    handlers[PACKET_TYPE_MSG][SESSION_STATE_BEFORE_AUTH]    = _handle_error;
#endif
    handlers[PACKET_TYPE_MSG][SESSION_STATE_NEW]            = _handle_warning;
    handlers[PACKET_TYPE_MSG][SESSION_STATE_ESTABLISHED]    = _handle_msg_established;

#ifndef NO_ENCRYPTION
    handlers[PACKET_TYPE_FIN][SESSION_STATE_BEFORE_INIT]    = _handle_fin;
    handlers[PACKET_TYPE_FIN][SESSION_STATE_BEFORE_AUTH]    = _handle_fin;
#endif
    handlers[PACKET_TYPE_FIN][SESSION_STATE_NEW]            = _handle_fin;
    handlers[PACKET_TYPE_FIN][SESSION_STATE_ESTABLISHED]    = _handle_fin;

#ifndef NO_ENCRYPTION
    handlers[PACKET_TYPE_ENC][SESSION_STATE_BEFORE_INIT]   = _handle_enc_before_init;
    handlers[PACKET_TYPE_ENC][SESSION_STATE_BEFORE_AUTH]   = _handle_enc_before_auth;
    handlers[PACKET_TYPE_ENC][SESSION_STATE_NEW]           = _handle_error;
    handlers[PACKET_TYPE_ENC][SESSION_STATE_ESTABLISHED]   = _handle_enc_renegotiate;
#endif

    /* Be extra cautious. */
    if(packet->packet_type < 0 || packet->packet_type >= PACKET_TYPE_COUNT_NOT_PING)
    {
      LOG_FATAL("Received an illegal packet:");
      packet_print(packet, session->options);
      printf("\n");
      exit(1);
    }

    if(session->state < 0 || session->state >= SESSION_STATE_COUNT)
    {
      LOG_FATAL("We ended up in an illegal state: 0x%x", session->state);
    }

    send_right_away = handlers[packet->packet_type][session->state](session, packet);
  }

  packet_destroy(packet);

  return send_right_away;
}

void session_kill(session_t *session)
{
  if(session->is_shutdown)
  {
    LOG_WARNING("Tried to kill a session that's already dead: %d", session->id);
    return;
  }

  session->is_shutdown = TRUE;
  driver_close(session->driver);
}

void session_destroy(session_t *session)
{
  if(!session->is_shutdown)
    session_kill(session);

  if(session->name)
    safe_free(session->name);

  if(session->driver)
    driver_destroy(session->driver);

  if(session->outgoing_buffer)
    buffer_destroy(session->outgoing_buffer);

#ifndef NO_ENCRYPTION
  if(session->encryptor)
    encryptor_destroy(session->encryptor);
#endif

  safe_free(session);
}

static char *get_name(char *base)
{
  /* This code isn't beautiful, but it does the job and it's only local
   * stuff anyway (no chance of being exploited). */
  char hostname[64];
  buffer_t *buffer = NULL;

  /* Read the hostname */
  gethostname(hostname, 64);
  hostname[63] = '\0';

  buffer = buffer_create(BO_BIG_ENDIAN);
  buffer_add_string(buffer, base);
  buffer_add_string(buffer, " (");
  buffer_add_string(buffer, hostname);
  buffer_add_string(buffer, ")");
  buffer_add_int8(buffer, 0);

  return (char*)buffer_create_string_and_destroy(buffer, NULL);
}

static session_t *session_create(char *name)
{
  session_t *session     = (session_t*)safe_malloc(sizeof(session_t));

  session->id            = rand() % 0xFFFF;

  /* Check if it's a 16-bit value (I set it to a bigger value to set a random isn) */
  if(isn == (isn & 0xFFFF))
    session->my_seq      = (uint16_t) isn; /* Use the hardcoded one. */
  else
    session->my_seq      = rand() % 0xFFFF; /* Random isn */

#ifndef NO_ENCRYPTION
  if(do_encryption)
    session->state       = SESSION_STATE_BEFORE_INIT;
  else
    session->state       = SESSION_STATE_NEW;
#else
  session->state         = SESSION_STATE_NEW;
#endif
  session->their_seq     = 0;
  session->is_shutdown   = FALSE;

  session->last_transmit = 0;
  session->missed_transmissions = 0;
  session->outgoing_buffer = buffer_create(BO_BIG_ENDIAN);

#ifndef NO_ENCRYPTION
  session->encryptor = encryptor_create(preshared_secret);

  if(!session->encryptor)
  {
    LOG_FATAL("Failed to generate a keypair!");
    exit(1);
  }
#endif
  session->name = NULL;
  if(name)
  {
    session->name = get_name(name);
    LOG_INFO("Setting session->name to %s", session->name);
  }

  return session;
}

session_t *session_create_console(select_group_t *group, char *name)
{
  session_t *session = session_create(name);

  session->driver = driver_create(DRIVER_TYPE_CONSOLE, driver_console_create(group));

  return session;
}

session_t *session_create_exec(select_group_t *group, char *name, char *process)
{
  session_t *session = session_create(name);

  session->driver = driver_create(DRIVER_TYPE_EXEC, driver_exec_create(group, process));

  return session;
}

session_t *session_create_command(select_group_t *group, char *name)
{
  session_t *session = session_create(name);

  session->driver = driver_create(DRIVER_TYPE_COMMAND, driver_command_create(group));
  session->is_command = TRUE;

  return session;
}

session_t *session_create_ping(select_group_t *group, char *name)
{
  session_t *session = session_create(name);

  session->driver = driver_create(DRIVER_TYPE_PING, driver_ping_create(group));
  session->is_ping = TRUE;

  return session;
}

void debug_set_isn(uint16_t value)
{
  isn = value;

  LOG_WARNING("WARNING: Setting a custom ISN can be dangerous!");
}

void session_enable_packet_trace()
{
  packet_trace = TRUE;
}

void session_set_delay(int delay_ms)
{
  packet_delay = delay_ms;
}

void session_set_transmit_immediately(NBBOOL transmit_immediately)
{
  transmit_instantly_on_data = transmit_immediately;
}

#ifndef NO_ENCRYPTION
void session_set_preshared_secret(char *new_preshared_secret)
{
  preshared_secret = new_preshared_secret;
}
void session_set_encryption(NBBOOL new_encryption)
{
  do_encryption = FALSE;
}
#endif

NBBOOL session_is_shutdown(session_t *session)
{
  return session->is_shutdown;
}

char *session_state_to_string(session_state_t state)
{
  switch(state)
  {
#ifndef NO_ENCRYPTION
    case SESSION_STATE_BEFORE_INIT:
      return "BEFORE_INIT";
    case SESSION_STATE_BEFORE_AUTH:
      return "BEFORE_AUTH";
#endif
    case SESSION_STATE_NEW:
      return "NEW";
    case SESSION_STATE_ESTABLISHED:
      return "ESTABLISHED";
    case SESSION_STATE_COUNT:
      return "Unknown!";
  }

  return "Unknown";
};