Skip to content

Commit

Permalink
Remove app, recent scans report, frida err handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ajinabraham committed Nov 25, 2023
1 parent 196b418 commit 743f4e5
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 32 deletions.
28 changes: 28 additions & 0 deletions mobsf/DynamicAnalyzer/views/ios/corellium_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,34 @@ def run_app(request, api=False):
# AJAX


@require_http_methods(['POST'])
def remove_app(request, api=False):
"""Remove an app from the device."""
data = {
'status': 'failed',
'message': 'Failed to uninstall the app'}
try:
instance_id = request.POST['instance_id']
bundle_id = request.POST['bundle_id']
failed = common_check(instance_id)
if failed:
return send_response(failed, api)
apikey = getattr(settings, 'CORELLIUM_API_KEY', '')
if not strict_package_check(bundle_id):
data['message'] = 'Invalid iOS Bundle id'
return send_response(data, api)
ca = CorelliumAgentAPI(apikey, instance_id)
if (ca.agent_ready()
and ca.remove_app(bundle_id) == OK):
data['status'] = OK
data['message'] = 'App uninstalled'
except Exception as exp:
logger.exception('Failed to uninstall the app')
data['message'] = str(exp)
return send_response(data, api)
# AJAX


@require_http_methods(['POST'])
def take_screenshot(request, api=False):
"""Take a Screenshot."""
Expand Down
11 changes: 11 additions & 0 deletions mobsf/DynamicAnalyzer/views/ios/frida_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ def spawn(self):
try:
_DEVICE = frida.get_remote_device()
_PID = _DEVICE.spawn([self.bundle_id])
except frida.NotSupportedError:
logger.exception('Not Supported Error')
return
except frida.ServerNotRunningError:
self.frida_ssh_forward()
if not _PID:
Expand All @@ -175,6 +178,9 @@ def spawn(self):
time.sleep(2)
except frida.TimedOutError:
logger.error('Timed out while waiting for device to appear')
except frida.NotSupportedError:
logger.exception('Not Supported Error')
return
except (frida.ProcessNotFoundError,
frida.TransportError,
frida.InvalidOperationError):
Expand Down Expand Up @@ -203,6 +209,9 @@ def session(self, pid, bundle_id):
_PID = _DEVICE.spawn([self.bundle_id])
# pid is the forntmost app
session = _DEVICE.attach(_PID)
except frida.NotSupportedError:
logger.exception('Not Supported Error')
return
except Exception:
logger.warning('Cannot attach to pid, spawning again')
self.spawn()
Expand All @@ -218,6 +227,8 @@ def session(self, pid, bundle_id):
sys.stdin.read()
script.unload()
session.detach()
except frida.NotSupportedError:
logger.exception('Not Supported Error')
except (frida.ProcessNotFoundError,
frida.TransportError,
frida.InvalidOperationError):
Expand Down
3 changes: 3 additions & 0 deletions mobsf/MobSF/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@
re_path(r'^ios/run_app/$',
instance.run_app,
name='run_app'),
re_path(r'^ios/remove_app/$',
instance.remove_app,
name='remove_app'),
re_path(r'^ios/take_screenshot/$',
instance.take_screenshot,
name='take_screenshot'),
Expand Down
4 changes: 1 addition & 3 deletions mobsf/MobSF/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -734,12 +734,10 @@ def base64_decode(value):
if is_base64(value) or value.startswith(commonb64s):
decoded = base64.b64decode(
value).decode('ISO-8859-1')
if set(decoded).difference(string.printable):
decoded = None
except Exception:
pass
if decoded:
return f'{value}\nBase64 Decoded: {decoded}'
return f'{value}\n\nBase64 Decoded: {decoded}'
return value


Expand Down
11 changes: 9 additions & 2 deletions mobsf/MobSF/views/home.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from mobsf.MobSF.forms import FormUtil, UploadFileForm
from mobsf.MobSF.utils import (
api_key,
get_md5,
is_dir_exists,
is_file_exists,
is_safe_path,
Expand Down Expand Up @@ -236,6 +237,7 @@ def recent_scans(request):
db_obj = RecentScansDB.objects.all().order_by('-TIMESTAMP').values()
android = StaticAnalyzerAndroid.objects.all()
ios = StaticAnalyzerIOS.objects.all()
updir = Path(settings.UPLD_DIR)
icon_mapping = {}
package_mapping = {}
for item in android:
Expand All @@ -249,8 +251,13 @@ def recent_scans(request):
else:
entry['PACKAGE'] = ''
entry['ICON_PATH'] = icon_mapping.get(entry['MD5'], '')
logcat = Path(settings.UPLD_DIR) / entry['MD5'] / 'logcat.txt'
entry['DYNAMIC_REPORT_EXISTS'] = logcat.exists()
if entry['FILE_NAME'].endswith('.ipa'):
entry['BUNDLE_HASH'] = get_md5(
entry['PACKAGE_NAME'].encode('utf-8'))
report_file = updir / entry['BUNDLE_HASH'] / 'mobsf_dump_file.txt'
else:
report_file = updir / entry['MD5'] / 'logcat.txt'
entry['DYNAMIC_REPORT_EXISTS'] = report_file.exists()
entries.append(entry)
context = {
'title': 'Recent Scans',
Expand Down
63 changes: 61 additions & 2 deletions mobsf/templates/dynamic_analysis/ios/dynamic_analysis.html
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,13 @@ <h4 class="modal-title">Create a Corellium iOS VM</h4>

var url = `{% url 'dynamic_analyzer_ios'%}?bundleid=${escapeHtml(bundle)}&instance_id=${$('#ios_dynamic').val()}`;
$('#in_device > tbody:' + order + '-child').append(
`<tr><td align="center"><img id="app_icon" src="data:image/png;base64,${escapeHtml(iconb64)}"/><br/><strong>${escapeHtml(name)}</strong></td><td>${escapeHtml(bundle)}</td><td>${escapeHtml(type)}</td><td><p><a class="btn btn-success disable" onclick="dynamic_loader()" href="${url}"><i class="fab fa-apple"></i> Start Dynamic Analysis</a> <a class="btn btn-info ${buttonState}" href="../../ios/view_report/${checksum}"><i class="fa fa-mobile"></i> View Report </a></p></td></tr>`);

`<tr><td align="center">
<img id="app_icon" src="data:image/png;base64,${escapeHtml(iconb64)}"/><br/>
<strong>${escapeHtml(name)}</strong></td><td>${escapeHtml(bundle)}</td>
<td>${escapeHtml(type)}</td>
<td><p><a class="btn btn-success disable" onclick="dynamic_loader()" href="${url}"><i class="fab fa-apple"></i> Start Dynamic Analysis</a>
<a class="btn btn-info ${buttonState}" href="../../ios/view_report/${checksum}"><i class="fa fa-mobile"></i> View Report </a>
<a class="btn btn-danger" id="${$('#ios_dynamic').val()}" onclick="remove_app(this, '${bundle}')"><i class="fas fa-trash-alt"></i> Uninstall</a></p></td></tr>`);
}
}
else {
Expand All @@ -415,6 +420,60 @@ <h4 class="modal-title">Create a Corellium iOS VM</h4>
}
});
}

// Remove app from device
function remove_app(item, bundle_id){
Swal.fire({
title: 'Are you sure?',
text: "This will remove the app from the device",
type: 'warning',
showCancelButton: true,
confirmButtonText: 'Yes',
cancelButtonText: 'No',
confirmButtonColor: '#d33',
cancelButtonColor: '#2da532',
}).then((result) => {
if (result.value) {

$.ajax({
url: '{% url 'remove_app' %}',
type : 'POST',
dataType: 'json',
data : {
csrfmiddlewaretoken: '{{ csrf_token }}',
instance_id: item.id,
bundle_id: bundle_id,
},
success : function(json) {
if (json.status==='ok'){
Swal.fire(
'Uninstalled!',
'Application is uninstalled',
'success'
).then(function () {
location.reload();
})
}
else {
Swal.fire(
'Uninstall Failed',
'Failed to uninstall the app: ' + json.message,
'error'
)
}
},
error : function(xhr,errmsg,err) {
Swal.fire(
'Uninstall Errored',
errmsg,
'error'
)
}
});
}
});
}

//Start VM
function start_vm(item){
Swal.fire({
Expand Down
Loading

0 comments on commit 743f4e5

Please sign in to comment.