Skip to content

Instantly share code, notes, and snippets.

@ajinabraham
Last active February 2, 2024 15:15
Show Gist options
  • Save ajinabraham/0f5de3b0c7b7d3665e54740b9f536d81 to your computer and use it in GitHub Desktop.
Save ajinabraham/0f5de3b0c7b7d3665e54740b9f536d81 to your computer and use it in GitHub Desktop.
MOBSF REST API Python Requests Example
"""
MOBSF REST API Python Requests
"""
import json
import requests
from requests_toolbelt.multipart.encoder import MultipartEncoder
SERVER = "http://127.0.0.1:8000"
FILE = 'diva-beta.apk'
APIKEY = '<API_KEY>'
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"]}
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"]}
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)
json_resp(RESP)
pdf(RESP)
delete(RESP)
@klmmr
Copy link

klmmr commented Apr 11, 2020

Inspired by this, I created a simple Python wrapper for the MobSF REST API. It also contains a CLI so you can even use MobSF on the command line!

Maybe it is useful for someone... You can find it here: https://github.com/klmmr/mobsfpy

@m0wn1ka
Copy link

m0wn1ka commented Dec 13, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment