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
| import platform import winreg import re import subprocess import os
service_port_mapping = { 21: "FTP/TFTP/VSFTPD (File Transfer Protocol)", 22: "SSH (Secure Shell)", 23: "Telnet (Remote Connection)", 25: "SMTP (Email)", 53: "DNS (Domain Name System)", 67: "DHCP (Dynamic Host Configuration Protocol)", 68: "DHCP (Dynamic Host Configuration Protocol)", 110: "POP3 (Email)", 123: "NTP", 135: "RPC(Remote Procedure Call)", 137: "NetBIOS Name Service(NBNS)", 138: "NetBIOS Datagram Service", 139: "Samba (File Sharing)", 143: "IMAP (Email)", 161: "SNMP (Simple Network Management Protocol)", 389: "LDAP (Lightweight Directory Access Protocol)", 445: "SMB (Server Message Block)", 512: "Linux Rexec (Remote Execution Service)", 513: "Linux Rexec (Remote Execution Service)", 514: "Linux Rexec (Remote Execution Service)", 873: "Rsync (Remote File Synchronization)", 1080: "Socket", 1098: "JAVARMI", 1352: "Lotus Domino (Email)", 1433: "MSSQL (Microsoft SQL Server)", 1521: "Oracle Database", 2049: "NFS (Network File System)", 2181: "Zookeeper", 2222: "DA", 2375: "Docker Remote API", 2601: "zebra", 3306: "MySQL Database", 3128: "squid", 3389: "RDP (Remote Desktop Protocol)", 4100: "SysBase", 4440: "rundeck", 4848: "GlassFish Console", 5000: "Sybase/DB2 Database", 5422: "PostgreSQL Database", 5432: "PostgreSQL Database", 5632: "PCAnywhere Service", 5900: "VNC (Virtual Network Computing)", 5984: "CouchDB", 6082: "Varnish", 6379: "Redis Database", 7001: "WebLogic", 7002: "WebLogic", 80: "HTTP", 443: "HTTPS", 8000: "Jdwp", 8069: "Zabbix", 8161: "ActiveMQ", 8080: "Jboss/Tomcat/Resin/GlassFish/Jenkins/Jetty", 8089: "Jboss/Tomcat/Resin/GlassFish/Jenkins/Jetty", 8083: "InfluxDB", 8086: "InfluxDB", 8980: "OpenNMS", 9000: "FastCGI", 9080: "Websphere Console", 9090: "Websphere Console", 9200: "Elasticsearch", 9300: "Elasticsearch", 11211: "Memcached", 27017: "MongoDB", 27018: "MongoDB", 50000: "SAP", 50010: "Hadoop", 50030: "Hadoop", 50070: "Hadoop",
}
def get_startup_items(hive, subkey): try: key = winreg.OpenKey(hive, subkey, 0, winreg.KEY_READ)
startup_items = {} index = 0
while True: try: name, value, _ = winreg.EnumValue(key, index) startup_items[name] = value index += 1 except OSError: break
return startup_items
except Exception as e: return {}
startup_items_current_user_run = get_startup_items(winreg.HKEY_CURRENT_USER, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Run")
startup_items_current_user_runonce = get_startup_items(winreg.HKEY_CURRENT_USER, r"SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce")
startup_items_local_machine_run = get_startup_items(winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Run")
startup_items_local_machine_runonce = get_startup_items(winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce")
def print_startup_items(startup_items, title): if startup_items: print("[+] {}: ".format(title)) for name, value in startup_items.items(): print(name) else: pass
def list_registry_subkeys(hive, subkey): try: with winreg.OpenKey(hive, subkey) as key: subkeys_count, _, _ = winreg.QueryInfoKey(key) subkeys = [winreg.EnumKey(key, i) for i in range(subkeys_count)] return subkeys except Exception as e: return []
def get_registry_value(hive, subkey, value_name): try: with winreg.OpenKey(hive, subkey) as key: value, _ = winreg.QueryValueEx(key, value_name) return value except Exception as e: return None
def getInstallAppInfo(): print("========================= 安装应用 =========================") hive1 = winreg.HKEY_LOCAL_MACHINE subkey = r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" subkeys1 = list_registry_subkeys(hive1, subkey)
hive2 = winreg.HKEY_CURRENT_USER subkeys2 = list_registry_subkeys(hive2, subkey) if subkeys1: print("[+] HKLM:")
for subkey_name in subkeys1: subkey_path = r"{}\{}".format(subkey, subkey_name) display_name = get_registry_value(hive1, subkey_path, "DisplayName")
if display_name: print("{}".format(display_name)) else: pass
if subkeys2: print("[+] HKCU:")
for subkey_name in subkeys2: subkey_path = r"{}\{}".format(subkey, subkey_name) display_name = get_registry_value(hive2, subkey_path, "DisplayName")
if display_name: print("{}".format(display_name)) else: pass
def getAutoRunInfo(): print("========================= 自启动项 =========================") print_startup_items(startup_items_current_user_run, "HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run") print_startup_items(startup_items_current_user_runonce, "HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce") print_startup_items(startup_items_local_machine_run, "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run") print_startup_items(startup_items_local_machine_runonce, "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce")
def getPortInfo(): print("========================= 开放端口 =========================") services_TCP_found = [] services_TCP_unfound = [] services_UDP_found = [] services_UDP_unfound = [] try: result = subprocess.Popen(["netstat", "-ano"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) out, err = result.communicate() netstat_output = out.decode('gbk')
for line in netstat_output.splitlines(): match = re.match(r'\s*TCP\s+(\S+)\s+(\S+)\s+(\S+)\s+(\d+)', line) if match: local_address, _, state, pid = match.groups()
if state in ["LISTENING", "ESTABLISHED"]: ip_port_match = re.match(r'(\S+):(\d+)', local_address) if ip_port_match: ip, port = ip_port_match.groups() service = service_port_mapping.get(int(port), "Unknown Service") if service != "Unknown Service": services_TCP_found.append({"port": port, "service": service})
else: services_TCP_unfound.append({"port": port, "service": service})
services_TCP_found = [dict(t) for t in {tuple(d.items()) for d in services_TCP_found}] services_TCP_found = sorted(services_TCP_found, key=lambda x: int(x['port']))
services_TCP_unfound = [dict(t) for t in {tuple(d.items()) for d in services_TCP_unfound}] services_TCP_unfound = sorted(services_TCP_unfound, key=lambda x: int(x['port']))
print("[+] 共匹配到 {} 个已知TCP端口服务".format(len(services_TCP_found))) print("[+] 共匹配到 {} 个未知TCP端口服务".format(len(services_TCP_unfound)))
print("[+] 识别出的TCP端口服务:") if services_TCP_found: for service_info in services_TCP_found: print("TCP\t\t{}\t\t{}".format(service_info['port'] ,service_info['service']))
print("[+] 未识别出的TCP端口服务:") if services_TCP_unfound: for service_info in services_TCP_unfound: print("TCP\t\t{}\t\t{}".format(service_info['port'], service_info['service']))
if not any(re.match(r'\s*TCP\s+(\S+)\s+(\S+)\s+(\S+)\s+(\d+)', line) for line in netstat_output.splitlines()): print("[-] 未找到相关TCP端口服务!")
for line in netstat_output.splitlines(): UDPpattern = re.compile(r'UDP\s+(?P<ip_address>[\w\.\[\]:]+):(?P<port>\d+)\s+\*:\*\s+(?P<pid>\d+)') UDPmatches = UDPpattern.findall(line)
for UDPmatch in UDPmatches: service = service_port_mapping.get(int(UDPmatch[1]), "Unknown Service") if service != "Unknown Service": services_UDP_found.append({"port": UDPmatch[1], "service": service}) else: services_UDP_unfound.append({"port": UDPmatch[1], "service": service})
services_UDP_found = [dict(t) for t in {tuple(d.items()) for d in services_UDP_found}] services_UDP_found = sorted(services_UDP_found, key=lambda x: int(x['port']))
services_UDP_unfound = [dict(t) for t in {tuple(d.items()) for d in services_UDP_unfound}] services_UDP_unfound = sorted(services_UDP_unfound, key=lambda x: int(x['port']))
print("[+] 共匹配到 {} 个已知UDP端口服务".format(len(services_UDP_found))) print("[+] 共匹配到 {} 个未知UDP端口服务".format(len(services_UDP_unfound)))
print("[+] 识别出的UDP端口服务:") if services_UDP_found: for service_info in services_UDP_found: print("UDP\t\t{}\t\t{}".format(service_info['port'], service_info['service']))
print("[+] 未识别出的UDP端口服务:") if services_UDP_unfound: for service_info in services_UDP_unfound: print("UDP\t\t{}\t\t{}".format(service_info['port'], service_info['service']))
except subprocess.CalledProcessError as e: print("[-] 错误: {}".format(e))
def getServiceInfo(): try: print('========================= 运行服务 =========================') result = subprocess.Popen('net start | find /v "已经启动以下 Windows 服务" | find /v "命令成功完成"', stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) out, err = result.communicate() netstat_output = out.decode('gbk') netstat_output = "\n".join(line for line in netstat_output.splitlines() if line.strip()) cleaned_text = "\n".join(line.lstrip() for line in netstat_output.splitlines()) print(cleaned_text)
except subprocess.CalledProcessError as e: print("[-] 错误: {}".format(e))
def getSystemInfo(): print("========================= 系统信息 =========================") cmd_command1 = r'systeminfo | findstr /i /c:"主机名" /c:"OS 名称" /c:"OS 版本" | find /v "BIOS 版本"' process = subprocess.Popen(cmd_command1, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout1, stderr1 = process.communicate() stdout1 = stdout1.decode('gbk') stdout1 = "\n".join(line for line in stdout1.splitlines() if line.strip())
print(stdout1) if stderr1: print("[-] error:", stderr1.decode('gbk'))
cmd_command2 = r'cmd /c ipconfig | findstr /i "IPv4 地址 IP Address" | findstr /i /v "IPv6"'
process = subprocess.Popen(cmd_command2, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
stdout2, stderr2 = process.communicate() if stderr2: print("[-] error:", stderr2.decode('gbk'))
ipv4_pattern1 = re.compile(r'IPv4 地址[^:]+:\s*([\d.]+)', re.IGNORECASE) ipv4_pattern2 = re.compile(r'IP Address\. .+? : (\d+\.\d+\.\d+\.\d+)', re.IGNORECASE)
match1 = ipv4_pattern1.search(stdout2.decode('gbk', errors='replace')) match2 = ipv4_pattern2.search(stdout2.decode('gbk', errors='replace'))
if match1: ipv4_address = match1.group(1) print("IPv4地址:\t ", ipv4_address) elif match2: ipv4_address = match2.group(1) print("IPv4地址:\t ", ipv4_address)
else: print("[-] 未找到IPv4地址")
cmd_command3 = 'systeminfo' process = subprocess.Popen(cmd_command3, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) stdout3, stderr3 = process.communicate() if stderr3: print("[-] error:", stderr3.decode('gbk'))
kb_pattern = re.compile(r'KB\d{7}') q_pattern = re.compile(r'Q\d{6}')
matches_kb = kb_pattern.findall(stdout3.decode('gbk')) matches_q = q_pattern.findall(stdout3.decode('gbk'))
if matches_kb: print("已安装" + str(len(matches_kb)) + "个补丁:\t", matches_kb) if matches_q: print("已安装" + str(len(matches_q)) + "个补丁:\t", matches_q)
return_code = process.returncode if return_code != 0: print("[-] Return Code:", return_code)
def getScheduleTaskInfo(): directory = r'C:\Windows\System32\Tasks' print("========================= 计划任务 =========================")
try: file_list = os.listdir(directory) if file_list: files_array = [] for filename in file_list: file_path = os.path.join(directory, filename) if os.path.isfile(file_path): files_array.append(filename) if files_array: for file in files_array: print("{}".format(file)) else: print("[-] 未找到计划任务!")
except FileNotFoundError: print("[-] 未找到计划任务!")
def getOSRelease(): print('[+] 开始对本机进行信息收集,请稍等片刻......') print('[+] 检测到当前主机系统为',platform.system() +" "+platform.release())
if __name__ == "__main__": getOSRelease() getSystemInfo() getAutoRunInfo() getPortInfo() getInstallAppInfo() getServiceInfo() getScheduleTaskInfo()
|