Skip to content

Commit

Permalink
Merge pull request #1585 from mandiant/fix/issue-1584
Browse files Browse the repository at this point in the history
fix import-to-ida due to changes in the result document format in v5
  • Loading branch information
mr-tz authored Jul 6, 2023
2 parents 46ff798 + bf5b261 commit bbafedc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
- Add logging and print redirect to tqdm for capa main [#749](https://github.com/mandiant/capa/issues/749) [@Aayush-Goel-04](https://github.com/aayush-goel-04)
- extractor: fix binja installation path detection does not work with Python 3.11
- tests: refine the IDA test runner script #1513 @williballenthin
- import-to-ida: fix bug introduced with JSON report changes in v5 #1584 @williballenthin

### capa explorer IDA Pro plugin

Expand Down
38 changes: 21 additions & 17 deletions scripts/import-to-ida.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@
is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and limitations under the License.
"""
import json
import logging
import binascii

import ida_nalt
import ida_funcs
import ida_kernwin

import capa.rules
import capa.features.freeze
import capa.render.result_document

logger = logging.getLogger("capa")


Expand Down Expand Up @@ -64,37 +68,37 @@ def main():
if not path:
return 0

with open(path, "rb") as f:
doc = json.loads(f.read().decode("utf-8"))

if "meta" not in doc or "rules" not in doc:
logger.error("doesn't appear to be a capa report")
return -1
result_doc = capa.render.result_document.ResultDocument.parse_file(path)
meta, capabilities = result_doc.to_capa()

# in IDA 7.4, the MD5 hash may be truncated, for example:
# wanted: 84882c9d43e23d63b82004fae74ebb61
# found: b'84882C9D43E23D63B82004FAE74EBB6\x00'
#
# see: https://github.com/idapython/bin/issues/11
a = doc["meta"]["sample"]["md5"].lower()
b = ida_nalt.retrieve_input_file_md5().lower()
a = meta.sample.md5.lower()
b = binascii.hexlify(ida_nalt.retrieve_input_file_md5()).decode("ascii").lower()
if not a.startswith(b):
logger.error("sample mismatch")
return -2

rows = []
for rule in doc["rules"].values():
if rule["meta"].get("lib"):
for name in capabilities.keys():
rule = result_doc.rules[name]
if rule.meta.lib:
continue
if rule["meta"].get("capa/subscope"):
if rule.meta.is_subscope_rule:
continue
if rule["meta"]["scope"] != "function":
if rule.meta.scope != capa.rules.Scope.FUNCTION:
continue

name = rule["meta"]["name"]
ns = rule["meta"].get("namespace", "")
for va in rule["matches"].keys():
va = int(va)
ns = rule.meta.namespace

for address, _ in rule.matches:
if address.type != capa.features.freeze.AddressType.ABSOLUTE:
continue

va = address.value
rows.append((ns, name, va))

# order by (namespace, name) so that like things show up together
Expand Down

0 comments on commit bbafedc

Please sign in to comment.