# Buggy code
def binary_analysis(src, tools_dir, app_dir, executable_name):
    """Binary Analysis of IPA."""
    try:
        binary_analysis_dict = {}
        logger.info("Starting Binary Analysis")
        dirs = os.listdir(src)
        dot_app_dir = ""
        for dir_ in dirs:
            if dir_.endswith(".app"):
                dot_app_dir = dir_
                break
        # Bin Dir - Dir/Payload/x.app/
        bin_dir = os.path.join(src, dot_app_dir)
        if executable_name is None:
            bin_name = dot_app_dir.replace(".app", "")
        else:
            bin_name = executable_name
        # Bin Path - Dir/Payload/x.app/x
        bin_path = os.path.join(bin_dir, bin_name)
        binary_analysis_dict["libs"] = []
        binary_analysis_dict["bin_res"] = []
        binary_analysis_dict["strings"] = []
        if not is_file_exists(bin_path):
            logger.warning("MobSF Cannot find binary in %s", bin_path)
            logger.warning("Skipping Otool, Classdump and Strings")
        else:
            bin_info = get_bin_info(bin_path)
            otool_dict = otool_analysis(tools_dir, bin_name, bin_path, bin_dir)
            bin_type = detect_bin_type(otool_dict["libs"])
            cls_dump = class_dump(tools_dir, bin_path, app_dir, bin_type)
            if not cls_dump:
                cls_dump = {}
            strings_in_ipa = strings_on_ipa(bin_path)
            otool_dict["anal"] = list(filter(None, otool_dict["anal"] + [cls_dump]))
            binary_analysis_dict["libs"] = otool_dict["libs"]
            binary_analysis_dict["bin_res"] = otool_dict["anal"]
            binary_analysis_dict["strings"] = strings_in_ipa
            binary_analysis_dict["macho"] = bin_info
            binary_analysis_dict["bin_type"] = bin_type

        return binary_analysis_dict
    except Exception:
        logger.exception("iOS Binary Analysis")


# Traceback (KeyError)
[WARNING] 23/Aug/2019 07:28:40 - Could not find VirtualBox path
[INFO] 23/Aug/2019 07:28:40 - MobSF Basic Environment Check
[INFO] 23/Aug/2019 07:28:40 - Checking for Update.
[INFO] 23/Aug/2019 07:28:41 - No updates available.
[INFO] 23/Aug/2019 07:28:55 - MIME Type: application/octet-stream FILE: test_uuu.ipa
[INFO] 23/Aug/2019 07:28:56 - Performing Static Analysis of iOS IPA
[INFO] 23/Aug/2019 07:28:56 - iOS Static Analysis Started
[INFO] 23/Aug/2019 07:28:56 - iOS Binary (IPA) Analysis Started
[INFO] 23/Aug/2019 07:28:56 - Generating Hashes
[INFO] 23/Aug/2019 07:28:56 - Extracting IPA
[INFO] 23/Aug/2019 07:28:56 - Unzipping
[INFO] 23/Aug/2019 07:28:57 - Get Files, BIN Plist -> XML, and Normalize
[INFO] 23/Aug/2019 07:28:57 - iOS Info.plist Analysis Started
[INFO] 23/Aug/2019 07:28:57 - Finding Info.plist in iOS Binary
[INFO] 23/Aug/2019 07:28:57 - Checking Permissions
[INFO] 23/Aug/2019 07:28:57 - Checking for Insecure Connections
[INFO] 23/Aug/2019 07:28:57 - Fetching Details from App Store: tw.gov.post.guard
[WARNING] 23/Aug/2019 07:28:58 - Unable to get app details
[INFO] 23/Aug/2019 07:28:58 - Starting Binary Analysis
[WARNING] 23/Aug/2019 07:28:58 - MobSF Cannot find binary in /Users/digicentre/Mobile-Security-Framework-MobSF/uploads/a9b35b6f9832daeeae2be49bff523a6c/Payload/test_UUU.app/test_UUU
[WARNING] 23/Aug/2019 07:28:58 - Skipping Otool, Classdump and Strings
[INFO] 23/Aug/2019 07:28:58 - Connecting to DB
[INFO] 23/Aug/2019 07:28:58 - Saving to Database
[ERROR] 23/Aug/2019 07:28:58 - Saving to DB
Traceback (most recent call last):
File "/Users/digicentre/Mobile-Security-Framework-MobSF/StaticAnalyzer/views/ios/db_interaction.py", line 174, in create_db_entry_ipa
MACHOINFO=bin_dict['macho'],
KeyError: 'macho'
[ERROR] 23/Aug/2019 07:28:58 - Rendering to Template
Traceback (most recent call last):
File "/Users/digicentre/Mobile-Security-Framework-MobSF/StaticAnalyzer/views/ios/db_interaction.py", line 46, in get_context_from_analysis_ipa
'binary_info': bin_dict['macho'],
KeyError: 'macho'
[ERROR] 23/Aug/2019 07:28:58 - Error Perfroming Static Analysis
Traceback (most recent call last):
File "/Users/digicentre/Mobile-Security-Framework-MobSF/StaticAnalyzer/views/ios/static_analyzer.py", line 181, in static_analyzer_ios
context['VT_RESULT'] = None
TypeError: 'NoneType' object does not support item assignment
[ERROR] 23/Aug/2019 07:28:58 - 'NoneType' object does not support item assignment
[ERROR] 23/Aug/2019 07:28:58 - Internal Server Error: /StaticAnalyzer_iOS/

# Fixed code
def binary_analysis(src, tools_dir, app_dir, executable_name):
    """Binary Analysis of IPA."""
    try:
        binary_analysis_dict = {}
        logger.info("Starting Binary Analysis")
        dirs = os.listdir(src)
        dot_app_dir = ""
        for dir_ in dirs:
            if dir_.endswith(".app"):
                dot_app_dir = dir_
                break
        # Bin Dir - Dir/Payload/x.app/
        bin_dir = os.path.join(src, dot_app_dir)
        if executable_name and is_file_exists(executable_name):
            bin_name = executable_name
        else:
            bin_name = dot_app_dir.replace(".app", "")
        # Bin Path - Dir/Payload/x.app/x
        bin_path = os.path.join(bin_dir, bin_name)
        binary_analysis_dict["libs"] = []
        binary_analysis_dict["bin_res"] = []
        binary_analysis_dict["strings"] = []
        if not is_file_exists(bin_path):
            logger.warning("MobSF Cannot find binary in %s", bin_path)
            logger.warning("Skipping Otool, Classdump and Strings")
        else:
            bin_info = get_bin_info(bin_path)
            otool_dict = otool_analysis(tools_dir, bin_name, bin_path, bin_dir)
            bin_type = detect_bin_type(otool_dict["libs"])
            cls_dump = class_dump(tools_dir, bin_path, app_dir, bin_type)
            if not cls_dump:
                cls_dump = {}
            strings_in_ipa = strings_on_ipa(bin_path)
            otool_dict["anal"] = list(filter(None, otool_dict["anal"] + [cls_dump]))
            binary_analysis_dict["libs"] = otool_dict["libs"]
            binary_analysis_dict["bin_res"] = otool_dict["anal"]
            binary_analysis_dict["strings"] = strings_in_ipa
            binary_analysis_dict["macho"] = bin_info
            binary_analysis_dict["bin_type"] = bin_type

        return binary_analysis_dict
    except Exception:
        logger.exception("iOS Binary Analysis")