-
-
Save gormih/41a840f035c723e06c5ee67354a1cf48 to your computer and use it in GitHub Desktop.
MOBSF REST API Python Requests Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
MOBSF REST API Python Requests | |
""" | |
import json | |
import requests | |
from requests_toolbelt.multipart.encoder import MultipartEncoder | |
SERVER = "http://127.0.0.1:8000" | |
FILE = 'SAP/diva-beta.apk' | |
APIKEY = '123' | |
def upload(): | |
"""Upload File""" | |
print("Uploading file") | |
multipart_data = MultipartEncoder(fields={'file': (FILE, open(FILE, 'rb'), 'application/octet-stream')}) | |
headers = {'Content-Type': multipart_data.content_type, 'Authorization': APIKEY} | |
response = requests.post(SERVER + '/api/v1/upload', data=multipart_data, headers=headers) | |
print(response.text) | |
return response.text | |
def scan(data): | |
"""Scan the file""" | |
print("Scanning file") | |
post_dict = json.loads(data) | |
headers = {'Authorization': APIKEY} | |
response = requests.post(SERVER + '/api/v1/scan', data=post_dict, headers=headers) | |
print(response.text) | |
def pdf(data): | |
"""Generate PDF Report""" | |
print("Generate PDF report") | |
headers = {'Authorization': APIKEY} | |
data = {"hash": json.loads(data)["hash"], "scan_type": json.loads(data)["scan_type"]} | |
response = requests.post(SERVER + '/api/v1/download_pdf', data=data, headers=headers, stream=True) | |
with open("report.pdf", 'wb') as flip: | |
for chunk in response.iter_content(chunk_size=1024): | |
if chunk: | |
flip.write(chunk) | |
print("Report saved as report.pdf") | |
def json_resp(data): | |
"""Generate JSON Report""" | |
print("Generate JSON report") | |
headers = {'Authorization': APIKEY} | |
data = {"hash": json.loads(data)["hash"], "scan_type": json.loads(data)["scan_type"]} | |
response = requests.post(SERVER + '/api/v1/report_json', data=data, headers=headers) | |
print(response.text) | |
def delete(data): | |
"""Delete Scan Result""" | |
print("Deleting Scan") | |
headers = {'Authorization': APIKEY} | |
data = {"hash": json.loads(data)["hash"]} | |
response = requests.post(SERVER + '/api/v1/delete_scan', data=data, headers=headers) | |
print(response.text) | |
RESP = upload() | |
scan(RESP) | |
pdf(RESP) | |
json_resp(RESP) | |
delete(RESP) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment