|
| 1 | +using System; |
| 2 | +using System.Net; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Net.Sockets; |
| 6 | +using System.Collections; |
| 7 | +using System.Globalization; |
| 8 | + |
| 9 | +namespace SharpNBTScan |
| 10 | +{ |
| 11 | + class Program |
| 12 | + { |
| 13 | + private static byte[] nbtstat = new byte[] { |
| 14 | + 0xee, 0x33, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, |
| 15 | + 0x00, 0x00, 0x00, 0x00, 0x20, 0x43, 0x4b, 0x41, |
| 16 | + 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, |
| 17 | + 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, |
| 18 | + 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, |
| 19 | + 0x41, 0x41, 0x41, 0x41, 0x41, 0x00, 0x00, 0x21, |
| 20 | + 0x00, 0x01 |
| 21 | + }; |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// 16 进制转 byte[] 数组 |
| 25 | + /// </summary> |
| 26 | + private static byte[] Hex2Byte(String hexContent) |
| 27 | + { |
| 28 | + // 需要将 hex 转换成 byte 数组。 |
| 29 | + byte[] bytes = new byte[hexContent.Length / 2]; |
| 30 | + for (int i = 0; i < bytes.Length; i++) |
| 31 | + { |
| 32 | + // 每两个字符是一个 byte。 |
| 33 | + bytes[i] = byte.Parse(hexContent.Substring(i * 2, 2), NumberStyles.HexNumber); |
| 34 | + } |
| 35 | + return bytes; |
| 36 | + } |
| 37 | + |
| 38 | + private static string Conversion(String SourceString, int left, int right) |
| 39 | + { |
| 40 | + return Encoding.Default.GetString(Hex2Byte(SourceString.Substring(left, right))); |
| 41 | + } |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// 以固定长度拆分字符串 |
| 45 | + /// </summary> |
| 46 | + private static ArrayList SplitLength(string SourceString, int Length) |
| 47 | + { |
| 48 | + ArrayList list = new ArrayList(); |
| 49 | + for (int i = 0; i < SourceString.Trim().Length; i += Length) |
| 50 | + { |
| 51 | + if ((SourceString.Trim().Length - i) >= Length) |
| 52 | + list.Add(SourceString.Trim().Substring(i, Length)); |
| 53 | + else |
| 54 | + list.Add(SourceString.Trim().Substring(i, SourceString.Trim().Length - i)); |
| 55 | + } |
| 56 | + return list; |
| 57 | + } |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// 主功能函数 |
| 61 | + /// </summary> |
| 62 | + private static void DetectionNBTscan(String host) |
| 63 | + { |
| 64 | + String response = String.Empty; |
| 65 | + |
| 66 | + IPAddress ipAddress = IPAddress.Parse(host); |
| 67 | + IPEndPoint remoteEP = new IPEndPoint(ipAddress, 137); |
| 68 | + |
| 69 | + response = String.Format("\n[*] Detecting Remote Computer of {0}\n", host); |
| 70 | + try |
| 71 | + { |
| 72 | + byte[] response_v0 = new byte[1024]; |
| 73 | + using (var sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) |
| 74 | + { |
| 75 | + sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 3000); |
| 76 | + sock.Connect(remoteEP); |
| 77 | + |
| 78 | + sock.Send(nbtstat); |
| 79 | + sock.Receive(response_v0); |
| 80 | + } |
| 81 | + |
| 82 | + string NumberName = Convert.ToString(response_v0[56], 10); |
| 83 | + |
| 84 | + response += String.Format(" [+] Data length: {0}\n [+] Number of Names: {1}", Convert.ToString(response_v0[55], 10), NumberName); |
| 85 | + |
| 86 | + |
| 87 | + // 开始处理数据内容(这种解析方式属于取巧,不耐用):每个 Name 都是 18 个字节数组,如果转为 String 则为 36 个字符 |
| 88 | + string[] response_v1 = BitConverter.ToString(response_v0.Skip(57).ToArray()).Replace("-", "").Split(new String[] { "00000000" }, StringSplitOptions.RemoveEmptyEntries); |
| 89 | + ArrayList strList = SplitLength(response_v1[0], 36); |
| 90 | + foreach (string str in strList) |
| 91 | + { |
| 92 | + String Flags = str.Substring(str.Length - 6, 2); |
| 93 | + String NameFlags = str.Substring(str.Length - 4); |
| 94 | + |
| 95 | + if (Flags == "00" && NameFlags == "0400") |
| 96 | + { |
| 97 | + response += String.Format("\n [>] Name type: Unique name -> (Workstation/Redirector) -> Name: {0}<{1}>", Conversion(str, 0, 30), Flags); |
| 98 | + } |
| 99 | + else if (Flags == "00" && NameFlags == "8400") |
| 100 | + { |
| 101 | + response += String.Format("\n [>] Name type: Group name -> (Workstation/Redirector) -> Name: {0}<{1}>", Conversion(str, 0, 30), Flags); |
| 102 | + } |
| 103 | + else if (Flags == "1C" && NameFlags == "8400") |
| 104 | + { |
| 105 | + response += String.Format("\n [>] Name type: Group name -> (Domain Controllers) -> Name: {0}<{1}>", Conversion(str, 0, 30), Flags); |
| 106 | + } |
| 107 | + else if (Flags == "20" && NameFlags == "0400") |
| 108 | + { |
| 109 | + response += String.Format("\n [>] Name type: Unique name -> (Server service) -> Name: {0}<{1}>", Conversion(str, 0, 30), Flags); |
| 110 | + } |
| 111 | + else if (Flags == "1B" && NameFlags == "0400") |
| 112 | + { |
| 113 | + response += String.Format("\n [>] Name type: Unique name -> (Domain Master Browser) -> Name: {0}<{1}>", Conversion(str, 0, 30), Flags); |
| 114 | + } |
| 115 | + else if (Flags == "1E" && NameFlags == "8400") |
| 116 | + { |
| 117 | + response += String.Format("\n [>] Name type: Group name -> (Browser Election Service) -> Name: {0}<{1}>", Conversion(str, 0, 30), Flags); |
| 118 | + } |
| 119 | + else if (Flags == "1D" && NameFlags == "0400") |
| 120 | + { |
| 121 | + response += String.Format("\n [>] Name type: Unique name -> (Local Master Browser) -> Name: {0}<{1}>", Conversion(str, 0, 30), Flags); |
| 122 | + } |
| 123 | + else if (str.Substring(0, 4) == "0102" && NameFlags == "8400") |
| 124 | + { |
| 125 | + response += String.Format("\n [>] Name type: Unique name -> (Browser) -> Name: {0}<{1}>", Conversion(str, 4, 25), Flags); |
| 126 | + } |
| 127 | + else if (str.Length == 12) |
| 128 | + { |
| 129 | + String uintid = String.Empty; |
| 130 | + for (int i = 0; i < str.Length / 2; i++) |
| 131 | + { |
| 132 | + uintid += str.Substring(i * 2, 2) + "-"; |
| 133 | + } |
| 134 | + response += String.Format("\n [>] Uint ID(MAC Address): {0}", uintid.Substring(0, uintid.LastIndexOf('-'))); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + Console.WriteLine(response); |
| 139 | + } |
| 140 | + catch (Exception ex) |
| 141 | + { |
| 142 | + Console.WriteLine("[!] Error: {0}", ex.Message); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + static void Main(string[] args) |
| 147 | + { |
| 148 | + |
| 149 | + string host = args[0]; |
| 150 | + /* |
| 151 | + * 多线程(线程池)处理 |
| 152 | + */ |
| 153 | + DetectionNBTscan(host); |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments