Skip to content

Commit

Permalink
Merge pull request #1 from zznop/ui-integration
Browse files Browse the repository at this point in the history
Added support for running as a BN plugin
  • Loading branch information
Brandon Miller authored Jan 14, 2022
2 parents f27927f + 9402175 commit 3e30cc6
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 59 deletions.
95 changes: 95 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
"""
Binary Ninja plugin for recovering kernel build configuration settings using BNIL
"""

import argparse
import logging
from binaryninja import (BinaryViewType, BinaryView, PluginCommand,
SaveFileNameField, get_form_input, BackgroundTaskThread)

class RecoverKConfigBackground(BackgroundTaskThread):
"""Class for running kernel configuration recovery in background
"""

def __init__(self, view: BinaryView, outpath: str) -> None:
BackgroundTaskThread.__init__(self, 'Recovering Linux kernel configuration', False)
self.outpath = outpath
self.view = view

def run(self):
"""Run analysis task
"""

self.view.reanalyze()
self.view.update_analysis_and_wait()
kconfigr = KConfigRecover(self.view)
config = kconfigr.recover()
save_kconfig(config, self.outpath)
self.progress = ""

def run_from_ui(view: BinaryView) -> None:
"""Run as a plugin under the UI
Args:
view: Binary view
"""

logger = logging.getLogger()
logger.setLevel(logging.INFO)

config_field = SaveFileNameField('Configuration Output Path')
get_form_input([config_field], 'Kernel Configuration Recovery Options')
outpath = 'generated.config'
if config_field.result != '':
outpath = config_field.result

kconfig_task = RecoverKConfigBackground(view, outpath)
kconfig_task.start()

def parse_args() -> argparse.Namespace:
"""Parses command line arguments.
Returns:
Parsed command line arguments.
"""

parser = argparse.ArgumentParser()
parser.add_argument(
'bndb', help='File path to kernel ELF or Binary Ninja database')
parser.add_argument(
'kconfig', help='File path to save recovered kernel configuration')
parser.add_argument('-d',
'--debug',
action='store_true',
help='Enable debug logging')
return parser.parse_args()

def run_headless() -> None:
"""Parse command line arguments and run app.
"""

args = parse_args()

logger = logging.getLogger()
if args.debug:
logger.setLevel(logging.DEBUG)
else:
logger.setLevel(logging.INFO)

logging.info('Opening "%s" and getting view...', args.bndb)
view = BinaryViewType.get_view_of_file(args.bndb)
logging.info('Running BN analysis, this may take some time...')

kconfig_task = RecoverKConfigBackground(view, args.kconfig)
kconfig_task.start()

if __name__ == '__main__':
from kconfig import KConfigRecover, save_kconfig
run_headless()
else:
from .kconfig import KConfigRecover, save_kconfig
PluginCommand.register(
"Recover Linux kernel config",
"Analyze Linux kernel binary and recover kernel configuration options",
run_from_ui,
)
58 changes: 0 additions & 58 deletions bn_kconfig_recover.py

This file was deleted.

2 changes: 1 addition & 1 deletion kconfig/recover.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class KConfigRecover:
"""Class that uses BN API to attempt to recover kernel configurations.
"""

def __init__(self, view: BinaryView):
def __init__(self, view: BinaryView) -> None:
self.view = view
self.helpers = {
'General Setup': {
Expand Down
24 changes: 24 additions & 0 deletions plugin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"pluginmetadataversion": 2,
"name": "BN KConfig Recover",
"type": [
"helper"
],
"api": [
"python3"
],
"description": "Recover Linux kernel build configurations",
"longdescription": "",
"license": {
"name": "MIT",
"text": "Copyright (c) 2022 Brandon Miller\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE."
},
"platforms": [
"Darwin",
"Linux",
"Windows"
],
"version": "0.2",
"author": "Brandon Miller",
"minimumbinaryninjaversion": 3164
}

0 comments on commit 3e30cc6

Please sign in to comment.