Skip to content

Commit

Permalink
feat(analysis) - Change static unpacking and dynamic unpacking to the…
Browse files Browse the repository at this point in the history
…ir real full api values.
  • Loading branch information
yoniabrahamy committed Nov 6, 2019
1 parent 2570ffd commit e774378
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 19 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,7 @@ result = analysis.result()

## Code examples
You can find more code examples under [analyze-python-sdk/examples/](https://github.com/intezer/analyze-python-sdk/tree/master/examples) directory

## Changelog
### 0.16
- In `Analysis`: Change `dynamic_unpacking` and `static_unpacking` to `disable_dynamic_unpacking` and `disable_static_unpacking`
2 changes: 1 addition & 1 deletion intezer_sdk/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.15'
__version__ = '0.16'
16 changes: 8 additions & 8 deletions intezer_sdk/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def __init__(self,
file_path: str = None,
file_hash: str = None,
file_stream: typing.BinaryIO = None,
dynamic_unpacking: bool = None,
static_unpacking: bool = None,
disable_dynamic_unpacking: bool = False,
disable_static_unpacking: bool = False,
api: IntezerApi = None,
file_name: str = None,
code_item_type: str = None) -> None:
Expand All @@ -34,8 +34,8 @@ def __init__(self,
self.status = None
self.analysis_id = None
self._file_hash = file_hash
self._dynamic_unpacking = dynamic_unpacking
self._static_unpacking = static_unpacking
self._disable_dynamic_unpacking = disable_dynamic_unpacking
self._disable_static_unpacking = disable_static_unpacking
self._file_path = file_path
self._file_stream = file_stream
self._file_name = file_name
Expand All @@ -49,13 +49,13 @@ def send(self, wait: bool = False) -> None:

if self._file_hash:
self.analysis_id = self._api.analyze_by_hash(self._file_hash,
self._dynamic_unpacking,
self._static_unpacking)
self._disable_dynamic_unpacking,
self._disable_static_unpacking)
else:
self.analysis_id = self._api.analyze_by_file(self._file_path,
self._file_stream,
dynamic_unpacking=self._dynamic_unpacking,
static_unpacking=self._static_unpacking,
disable_dynamic_unpacking=self._disable_dynamic_unpacking,
disable_static_unpacking=self._disable_static_unpacking,
file_name=self._file_name,
code_item_type=self._code_item_type)

Expand Down
25 changes: 15 additions & 10 deletions intezer_sdk/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ def _request(self, method: str, path: str, data: dict = None, headers: dict = No

return response

def analyze_by_hash(self, file_hash: str, dynamic_unpacking: bool = None, static_unpacking: bool = None) -> str:
data = self._param_initialize(dynamic_unpacking, static_unpacking)
def analyze_by_hash(self,
file_hash: str,
disable_dynamic_unpacking: bool = None,
disable_static_unpacking: bool = None) -> str:
data = self._param_initialize(disable_dynamic_unpacking, disable_static_unpacking)

data['hash'] = file_hash
response = self._request(path='/analyze-by-hash', data=data, method='POST')
Expand All @@ -63,11 +66,11 @@ def _analyze_file_stream(self, file_stream: typing.BinaryIO, file_name: str, opt
def analyze_by_file(self,
file_path: str = None,
file_stream: typing.BinaryIO = None,
dynamic_unpacking: bool = None,
static_unpacking: bool = None,
disable_dynamic_unpacking: bool = None,
disable_static_unpacking: bool = None,
file_name: str = None,
code_item_type: str = None) -> str:
options = self._param_initialize(dynamic_unpacking, static_unpacking, code_item_type)
options = self._param_initialize(disable_dynamic_unpacking, disable_static_unpacking, code_item_type)

if file_stream:
return self._analyze_file_stream(file_stream, file_name, options)
Expand Down Expand Up @@ -139,13 +142,15 @@ def set_session(self):
self._session.headers['User-Agent'] = consts.USER_AGENT

@staticmethod
def _param_initialize(dynamic_unpacking: bool = None, static_unpacking: bool = None, code_item_type: str = None):
def _param_initialize(disable_dynamic_unpacking: bool = None,
disable_static_unpacking: bool = None,
code_item_type: str = None):
data = {}

if dynamic_unpacking is not None:
data['disable_dynamic_execution'] = not dynamic_unpacking
if static_unpacking is not None:
data['disable_static_extraction'] = not static_unpacking
if disable_dynamic_unpacking:
data['disable_dynamic_execution'] = disable_dynamic_unpacking
if disable_static_unpacking:
data['disable_static_extraction'] = disable_static_unpacking
if code_item_type:
data['code_item_type'] = code_item_type

Expand Down
27 changes: 27 additions & 0 deletions tests/unit/test_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,33 @@ def test_send_analysis_by_file_and_get_report(self):
self.assertEqual(analysis.status, consts.AnalysisStatusCode.FINISH)
self.assertEqual(analysis.result(), 'report')

def test_send_analysis_by_file_with_disable_unpacking(self):
# Arrange
with responses.RequestsMock() as mock:
mock.add('POST',
url=self.full_url + '/analyze',
status=201,
json={'result_url': 'a/sd/asd'})
mock.add('GET',
url=self.full_url + '/analyses/asd',
status=200,
json={'result': 'report'})
analysis = Analysis(file_path='a',
disable_dynamic_unpacking=True,
disable_static_unpacking=True)
with patch(self.patch_prop, mock_open(read_data='data')):
# Act
analysis.send(wait=True)

# Assert
self.assertEqual(analysis.status, consts.AnalysisStatusCode.FINISH)
self.assertEqual(analysis.result(), 'report')
request_body = mock.calls[0].request.body.decode()
self.assertTrue('Content-Disposition: form-data; name="disable_static_extraction"\r\n\r\nTrue'
in request_body)
self.assertTrue('Content-Disposition: form-data; name="disable_dynamic_execution"\r\n\r\nTrue'
in request_body)

def test_send_analysis_by_sha256_that_dont_exist_raise_error(self):
# Arrange
with responses.RequestsMock() as mock:
Expand Down

0 comments on commit e774378

Please sign in to comment.