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
| import re import os from pathlib import Path
class SSLPinningDetector: def __init__(self, app_path): self.app_path = app_path self.pinning_indicators = [] def detect_android_ssl_pinning(self): """检测Android应用中的SSL Pinning""" if self.app_path.endswith('.apk'): return self.detect_apk_ssl_pinning() if os.path.isdir(self.app_path): return self.detect_source_ssl_pinning() return False def detect_apk_ssl_pinning(self): """检测APK中的SSL Pinning""" import zipfile try: with zipfile.ZipFile(self.app_path, 'r') as apk: if 'classes.dex' in apk.namelist(): dex_data = apk.read('classes.dex') if self.scan_dex_for_pinning(dex_data): return True for file_name in apk.namelist(): if file_name.endswith('.xml') or file_name.endswith('.json'): file_data = apk.read(file_name) if self.scan_resources_for_pinning(file_data): return True except Exception as e: print(f"[-] Error analyzing APK: {e}") return False def scan_dex_for_pinning(self, dex_data): """扫描DEX文件中的SSL Pinning代码""" dex_str = dex_data.decode('utf-8', errors='ignore') pinning_strings = [ 'CertificatePinner', 'TrustKit', 'SSLContext', 'X509TrustManager', 'checkServerTrusted', 'pinning', 'certificate', 'sha256', 'public-key-pins' ] found_indicators = [] for string in pinning_strings: if string.lower() in dex_str.lower(): found_indicators.append(string) if found_indicators: self.pinning_indicators.extend(found_indicators) return True return False def scan_resources_for_pinning(self, resource_data): """扫描资源文件中的SSL Pinning配置""" resource_str = resource_data.decode('utf-8', errors='ignore') if 'network_security_config' in resource_str: if 'pin-set' in resource_str or 'certificates' in resource_str: self.pinning_indicators.append('network_security_config') return True if 'trustkit' in resource_str.lower(): self.pinning_indicators.append('trustkit_config') return True return False def detect_source_ssl_pinning(self): """检测源代码中的SSL Pinning""" for root, dirs, files in os.walk(self.app_path): for file in files: if file.endswith(('.java', '.kt', '.swift', '.m', '.h')): file_path = os.path.join(root, file) if self.scan_source_file(file_path): return True return False def scan_source_file(self, file_path): """扫描单个源文件""" try: with open(file_path, 'r', encoding='utf-8', errors='ignore') as f: content = f.read() pinning_patterns = [ r'CertificatePinner', r'TrustKit', r'SSLContext', r'X509TrustManager', r'checkServerTrusted', r'pinning', r'certificate.*pin', r'sha256.*pin' ] for pattern in pinning_patterns: if re.search(pattern, content, re.IGNORECASE): self.pinning_indicators.append(f"{file_path}: {pattern}") return True except Exception as e: print(f"[-] Error scanning file {file_path}: {e}") return False def get_pinning_indicators(self): """获取检测到的SSL Pinning指标""" return self.pinning_indicators
if __name__ == "__main__": detector = SSLPinningDetector("app.apk") has_pinning = detector.detect_android_ssl_pinning() if has_pinning: print("[+] SSL Pinning detected!") print("Indicators:") for indicator in detector.get_pinning_indicators(): print(f" - {indicator}") else: print("[-] No SSL Pinning detected")
|