import requests import json # 设置Mobsf API的URL地址和密钥 base_url='http://127.0.0.1:8000/' api_url = base_url+'api/v1/' api_key = '' # 构建API请求的头信息 headers = {'Authorization': api_key} #curl --url "http://localhost:8000/api/v1/scans?page=1&page_size=10" -H "X-Mobsf-Api-Key:xxxxxxxxx" # 发送API请求,获取Mobsf前100项扫描结果 r = requests.get(api_url + 'scans?page=1&page_size=90', headers=headers) #scan_id = r.json()['content'][0]['FILE_NAME'] jslen=len(r.json()['content']) proxies={'http':'127.0.0.1:8080'} keywords="guess" for j in range(jslen): appmd5 = r.json()['content'][j]['MD5'] datas = {'hash': appmd5} url = base_url+"find/" # 参数拼凑,附件上传格式如picurl参数,其他表单参数值拼成tuple格式: proxies = { "http": "127.0.0.1:8080" } headers = { "Content-Type": "multipart/form-data; boundary=----WebKitFormBoundaryjvmMdroaobco6uyJ" } md5 =appmd5 datas = """------WebKitFormBoundaryjvmMdroaobco6uyJ\r\nContent-Disposition: form-data; name="md5"\r\n\r\n"""+md5+"""\r\n------WebKitFormBoundaryjvmMdroaobco6uyJ\r\nContent-Disposition: form-data; name="q"\r\n\r\n"""+keywords+"""\r\n------WebKitFormBoundaryjvmMdroaobco6uyJ\r\nContent-Disposition: form-data; name="code"\r\n\r\njava\r\n------WebKitFormBoundaryjvmMdroaobco6uyJ\r\nContent-Disposition: form-data; name="search_type"\r\n\r\ncontent\r\n------WebKitFormBoundaryjvmMdroaobco6uyJ\r\nContent-Disposition: form-data; name="api"\r\n\r\ntrue\r\n\r\n------WebKitFormBoundaryjvmMdroaobco6uyJ--""" res = requests.post(url, data=datas, headers=headers) if(res.status_code==500): continue js=json.loads(res.json()) if (len(js['matches'])>=1): print(r.json()['content'][j]['FILE_NAME']+" is matched keywords: "+keywords) # else: # print(r.json()['content'][j]['FILE_NAME']+" is not matched") for i in range(len(js['matches'])): print(" "+js['matches'][i])
自用,大家自行修改
import requests import json # 设置 MobSF API 的 URL 地址和密钥 base_url = 'http://127.0.0.1:8000/' api_url = base_url + 'api/v1/' api_key = '' headers = {'Authorization': api_key} # 发送 API 请求,获取 MobSF 前 90 项扫描结果 response = requests.get(api_url + 'scans?page=1&page_size=90', headers=headers) scan_results = response.json()['content'] # 搜索关键字 keywords = "HQ9" proxies = {'http': '127.0.0.1:8080'} multipart_boundary = '----WebKitFormBoundaryjvmMdroaobco6uyJ' # 遍历每个扫描结果 for scan in scan_results: appmd5 = scan['MD5'] url = base_url + "find/" # 构建 multipart/form-data 数据 multipart_data = f"""\ {multipart_boundary}\r Content-Disposition: form-data; name="md5"\r\n\r\n{appmd5}\r {multipart_boundary}\r Content-Disposition: form-data; name="q"\r\n\r\n{keywords}\r {multipart_boundary}\r Content-Disposition: form-data; name="code"\r\n\r\njava\r {multipart_boundary}\r Content-Disposition: form-data; name="search_type"\r\n\r\ncontent\r {multipart_boundary}\r Content-Disposition: form-data; name="api"\r\n\r\ntrue\r {multipart_boundary}--""" # 发送 POST 请求进行关键词搜索 try: with requests.post(url, data=multipart_data, headers={ 'Content-Type': f'multipart/form-data; boundary={multipart_boundary}' }, proxies=proxies) as res: # 检查是否有匹配的关键词 if res.status_code == 500: continue result_json = res.json() if 'matches' in result_json and len(result_json['matches']) >= 1: print(f"{scan['FILE_NAME']} is matched keywords: {keywords}") for match in result_json['matches']: print(f" {match}") except requests.RequestException as e: print(f"Error processing {scan['FILE_NAME']}: {e}")
主要优化:
简化响应处理:避免在循环中多次调用 r.json(),改为在开始时一次性解析。
上下文管理:使用 with 语句发送 POST 请求,确保请求完成后会自动关闭连接。
字符串格式化:简化 multipart/form-data 的拼接,通过 Python 的格式化方法来动态生成请求数据。
异常处理:增加了 try-except 块,捕获可能的请求异常,防止程序因网络问题崩溃。
这样可以提升代码的性能、可读性和安全性。
2024-11-14:
mobsf添加了鉴权系统,没有api接口,懒得模拟登录获取cookie,已更新
import requests import json # MobSF API URL&key base_url = 'http://127.0.0.1:8000/' api_url = base_url + 'api/v1/' #你的API key api_key = '' # Define headers for the API request # 你的cookie headers = { 'Authorization': api_key, 'Cookie': "sessionid=4yvpf33vmdv6azqb7swmkya8cm6nt3qb" } # Define proxy proxies = { "http": "http://127.0.0.1:8080" } # Set the keyword to search你的关键字 keyword = 'password' try: # Request to get the first 250 scan results r = requests.get(api_url + 'scans?page=1&page_size=250', headers=headers) r.raise_for_status() scan_results = r.json().get('content', []) for scan in scan_results: app_md5 = scan['MD5'] file_name = scan['FILE_NAME'] # Set data payload for searching data_payload = f"""------WebKitFormBoundaryjvmMdroaobco6uyJ\r\nContent-Disposition: form-data; name="md5"\r\n\r\n{app_md5}\r\n------WebKitFormBoundaryjvmMdroaobco6uyJ\r\nContent-Disposition: form-data; name="q"\r\n\r\n{keyword}\r\n------WebKitFormBoundaryjvmMdroaobco6uyJ\r\nContent-Disposition: form-data; name="code"\r\n\r\njava\r\n------WebKitFormBoundaryjvmMdroaobco6uyJ\r\nContent-Disposition: form-data; name="search_type"\r\n\r\ncontent\r\n------WebKitFormBoundaryjvmMdroaobco6uyJ\r\nContent-Disposition: form-data; name="api"\r\n\r\ntrue\r\n------WebKitFormBoundaryjvmMdroaobco6uyJ--""" # Define headers for search API search_headers = { "Content-Type": "multipart/form-data; boundary=----WebKitFormBoundaryjvmMdroaobco6uyJ", "Authorization": api_key, "Cookie": "sessionid=4yvpf33vmdv6azqb7swmkya8cm6nt3qb" } # Send POST request to search for keyword search_url = base_url + "find/" res = requests.post(search_url, data=data_payload, headers=search_headers) # Handle cases with 500 status code and other errors if res.status_code == 500: print(f"Server error for file: {file_name}") continue # Process response response_text = res.text # get raw response text try: # Decode the JSON-encoded string twice # First to convert the string literal into JSON format, then to parse as JSON response_json = json.loads(json.loads(response_text)) matches = response_json.get('matches', []) if matches: print(f"{file_name} is matched with keyword: {keyword}") for match in matches: print(" " + match) else: pass except json.JSONDecodeError: print(f"Failed to parse JSON response for file: {file_name} - Response: {response_text}") print("---------------------------------------------------") except requests.exceptions.RequestException as e: print("Error with API request:", e)
由chatgpt优化
本文作者为MCtech,转载请注明。