Skip to content

Made karta more efficient #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 5 additions & 23 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,28 +1,10 @@
src/*.vpj
src/*.vpw
src/*.vpwhist
src/*.vtg
src/*.pyc
src/libs/*.pyc
src/config/*.pyc
src/core/*.pyc
src/disassembler/*.pyc
src/disassembler/IDA/*.pyc
*.pyc
__pycache__

/build/*
/dist/*
/Karta.egg-info/*
/build
/dist
Karta.egg-info

docs/_build/*
docs/_build/*/*
docs/_build/*/*/*
docs/_build/*/*/*/*
docs/_static/*
docs/_templates/*
/*.pyc
/*/*.pyc
/*/*/*.pyc
/*/*/*/*.pyc
/src/thumbs_up/analyzers/__pycache__/*.pyc
/src/*/*/__pycache__/*.pyc
/src/*/*/__pycache__/*.pyc
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ We believe that there are 3 main use cases for this IDA plugin:
https://karta.readthedocs.io/

## Installation (Python 3 & IDA >= 7.4)
For the latest versions, using Python 3, simply git clone the repository and run the ```setup.py install``` script.
For the latest versions, using Python 3, simply git clone the repository and run ```pip3 install .```.
To install the plugin for use in ida run
```python -m karta.installers.ida_installer```
Python 3 is supported since versions v2.0.0 and above.

## Installation (Python 2 & IDA < 7.4)
Expand Down
4 changes: 3 additions & 1 deletion docs/Installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ Prerequisites
* [sark](https://github.com/tmr232/Sark)

Using the ```setup.py``` script, one can install all of these prerequisites, and be ready to go:
```./setup.py install```
```pip3 install .```
To install the plugin for use in ida run
```python -m karta.installers.ida_installer```

Installing the Plugin
------------------------
Expand Down
15 changes: 11 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/python
#!/usr/bin/python3

from setuptools import setup, find_packages
from codecs import open

with open("README.md", "r") as fh:
long_description = fh.read()
Expand All @@ -15,11 +14,19 @@
long_description_content_type="text/markdown",
url='https://github.com/CheckPointSW/Karta',
license='MIT',
packages=find_packages(),
packages=find_packages(where="src"),
package_dir={"": "src"},
install_requires=['elementals', 'sark', 'pydocstyle', 'flake8', 'click', 'scikit-learn'],
python_requires='>=3',
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License (MIT License)",
"Operating System :: OS Independent",
],
zip_safe=False)
entry_points={
'console_scripts': [
'karta_analyze_src = karta.karta_analyze_src:main'
]
},
zip_safe=False
)
7 changes: 0 additions & 7 deletions src/__init__.py

This file was deleted.

62 changes: 0 additions & 62 deletions src/disassembler/IDA/ida_cmd_api.py

This file was deleted.

Empty file added src/karta/__init__.py
Empty file.
10 changes: 6 additions & 4 deletions src/analyze_src_file.py → src/karta/analyze_src_file.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from config.utils import *
from disassembler.factory import createDisassemblerHandler
from function_context import SourceContext, BinaryContext, IslandContext
from elementals import Logger
import logging
import traceback
from elementals import Logger

# cannot have realtive imports for a script running in ida
from karta.config.utils import *
from karta.disassembler.factory import createDisassemblerHandler
from karta.function_context import SourceContext, BinaryContext, IslandContext

def analyzeFile():
"""Analyzes all of the (source) functions for a single compiled file."""
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/config/anchor.py → src/karta/config/anchor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .anchor_config import *
from config.utils import *
from .utils import *

def isAnchor(context, seen_strings, seen_consts, functions_list, logger):
"""Check if the given context represents an Anchor function.
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
68 changes: 57 additions & 11 deletions src/config/utils.py → src/karta/config/utils.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
from .score_config import *
import json
import os

from .score_config import *

#################################
## Basic Global Configurations ##
#################################

DISASSEMBLER_PATH = "/opt/ida-7.4/ida"
SCRIPT_PATH = os.path.abspath("analyze_src_file.py")
DISASSEMBLER_PATH = None
CONFIG_DIR = os.path.dirname(os.path.realpath(__file__))
DEFAULT_DISASSEMBLER = os.path.join(CONFIG_DIR, "default_disassembler_path")
SCRIPT_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), "..", "analyze_src_file.py")

LIBRARY_NAME = "Karta"
STATE_FILE_SUFFIX = "_file_state.json"
Expand Down Expand Up @@ -455,24 +458,67 @@ def isMatching():
"""
return matching_mode

def setDisassemblerPath(prompter):
"""Update the disassembler path according to input from the user.
def addDisassembler(name, path):
"""Add an installation of a dissasembler.

Args:
prompter (prompter): prompter elementals instance
name (str): name of the installation file
path (str): directory of the disassembler installtion
"""
global DISASSEMBLER_PATH
with open(os.path.join(CONFIG_DIR, name), "w") as f:
f.write(path)

def disassemblerInstallationExists(name):
"""Check whether there is an existing installation with the filename.

Args:
name (str): filename of the installtion to check

Return Value:
return true if such installtion exists
"""
return os.path.exists(os.path.join(CONFIG_DIR, name))

def getDisassembler(name):
"""Get directory of disassembler from configuration by it's configuration file name.

Args:
name (str): name of the disassembler to search for
"""
if disassemblerInstallationExists(name):
with open(os.path.join(CONFIG_DIR, name), "r") as f:
return f.read()

new_path = prompter.input(f"Please insert the command (path) needed in order to execute your disassembler (IDA for instance) ({DISASSEMBLER_PATH}): ")
if len(new_path.strip()) != 0:
DISASSEMBLER_PATH = new_path
def setDefaultDisassembler(name):
"""Set the default disassembler in the configuration file.

def getDisasPath():
Args:
name (str): name of the file that contains the default disassembler to use.
"""
with open(os.path.join(CONFIG_DIR, DEFAULT_DISASSEMBLER), "w") as f:
if os.path.isfile(os.path.join(CONFIG_DIR, name)):
f.write(name)

def getDisasPath(prompter):
"""Return the updated path to the disassembler.

Return Value:
The (updated) path to the disassembler program
"""
global DISASSEMBLER_PATH
if DISASSEMBLER_PATH is not None:
pass
elif os.path.isfile(DEFAULT_DISASSEMBLER):
actual_disas_path = None
with open(DEFAULT_DISASSEMBLER, 'r') as f:
actual_disas_path = f.read()
full_disas_name_path = os.path.join(CONFIG_DIR, actual_disas_path)
if os.path.isfile(full_disas_name_path):
with open(full_disas_name_path, 'r') as f:
DISASSEMBLER_PATH = f.read()
else:
DISASSEMBLER_PATH = prompter.input("Please enter a path to your disassembler: ")
prompter.info("I may suggest using one of the installers to set a default disassembler")
return DISASSEMBLER_PATH

def libraryName():
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from config.utils import *
from collections import defaultdict
import config.anchor as anchor
import time
from collections import defaultdict

from ..config.utils import *
from ..config import anchor


class MatchEngine(object):
"""A class that handles the book-keeping for the matching process.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import idaapi
import sark
from config.utils import *
from hashlib import md5

from ...config.utils import *

class AnalyzerIDA(object):
"""Logic instance for the IDA disassembler API. Contains the heart of Karta's canonical representation.

Expand All @@ -22,6 +23,7 @@ def __init__(self, disas):
disas (disassembler): disassembler layer instance
"""
self.disas = disas
idaapi.auto_wait()

def funcNameInner(self, raw_func_name):
"""Return the name of the function (including windows name fixes).
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import logging

# Dependencies that only exist inside IDA
import idautils
import idaapi
Expand All @@ -9,9 +11,9 @@
import sark
from .ida_analysis_api import AnalyzerIDA
# Basic dependencies (only basic python packages)
from config.utils import *
from disassembler.disas_api import DisasAPI
import logging
from ...config.utils import *
from ..disas_api import DisasAPI


class IdaLogHandler(logging.Handler):
"""Integrate the log messages with IDA's output window."""
Expand Down
Loading