mirror of
https://github.com/mandiant/capa.git
synced 2025-12-12 15:49:46 -08:00
@@ -320,7 +320,7 @@ def loads(s: str) -> capa.features.extractors.base_extractor.FeatureExtractor:
|
||||
"""deserialize a set of features (as a NullFeatureExtractor) from a string."""
|
||||
import capa.features.extractors.null as null
|
||||
|
||||
freeze = Freeze.parse_raw(s)
|
||||
freeze = Freeze.model_validate_json(s)
|
||||
if freeze.version != 2:
|
||||
raise ValueError(f"unsupported freeze format version: {freeze.version}")
|
||||
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
# Unless required by applicable law or agreed to in writing, software distributed under the License
|
||||
# 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 datetime
|
||||
import contextlib
|
||||
@@ -223,7 +222,7 @@ def load_and_verify_cached_results() -> Optional[rdoc.ResultDocument]:
|
||||
logger.debug("loading cached capa results from netnode '%s'", CAPA_NETNODE)
|
||||
|
||||
n = netnode.Netnode(CAPA_NETNODE)
|
||||
doc = rdoc.ResultDocument.parse_obj(json.loads(n[NETNODE_RESULTS]))
|
||||
doc = rdoc.ResultDocument.model_validate_json(n[NETNODE_RESULTS])
|
||||
|
||||
for rule in rutils.capability_rules(doc):
|
||||
for location_, _ in rule.matches:
|
||||
|
||||
@@ -1223,7 +1223,7 @@ def main(argv: Optional[List[str]] = None):
|
||||
|
||||
if format_ == FORMAT_RESULT:
|
||||
# result document directly parses into meta, capabilities
|
||||
result_doc = capa.render.result_document.ResultDocument.parse_file(args.sample)
|
||||
result_doc = capa.render.result_document.ResultDocument.from_file(Path(args.sample))
|
||||
meta, capabilities = result_doc.to_capa()
|
||||
|
||||
else:
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
import datetime
|
||||
import collections
|
||||
from typing import Dict, List, Tuple, Union, Literal, Optional
|
||||
from pathlib import Path
|
||||
|
||||
from pydantic import Field, BaseModel, ConfigDict
|
||||
|
||||
@@ -596,3 +597,7 @@ class ResultDocument(FrozenModel):
|
||||
capabilities[rule_name].append((addr.to_capa(), result))
|
||||
|
||||
return self.meta, capabilities
|
||||
|
||||
@classmethod
|
||||
def from_file(cls, path: Path) -> "ResultDocument":
|
||||
return cls.model_validate_json(path.read_text(encoding="utf-8"))
|
||||
|
||||
@@ -30,6 +30,7 @@ See the License for the specific language governing permissions and limitations
|
||||
"""
|
||||
import logging
|
||||
import binascii
|
||||
from pathlib import Path
|
||||
|
||||
import ida_nalt
|
||||
import ida_funcs
|
||||
@@ -68,7 +69,7 @@ def main():
|
||||
if not path:
|
||||
return 0
|
||||
|
||||
result_doc = capa.render.result_document.ResultDocument.parse_file(path)
|
||||
result_doc = capa.render.result_document.ResultDocument.from_file(Path(path))
|
||||
meta, capabilities = result_doc.to_capa()
|
||||
|
||||
# in IDA 7.4, the MD5 hash may be truncated, for example:
|
||||
|
||||
@@ -31,6 +31,7 @@ Example:
|
||||
import sys
|
||||
import logging
|
||||
import argparse
|
||||
from pathlib import Path
|
||||
|
||||
import capa.render.proto
|
||||
import capa.render.result_document
|
||||
@@ -64,7 +65,7 @@ def main(argv=None):
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
logging.getLogger().setLevel(logging.INFO)
|
||||
|
||||
rd = capa.render.result_document.ResultDocument.parse_file(args.json)
|
||||
rd = capa.render.result_document.ResultDocument.from_file(Path(args.json))
|
||||
pb = capa.render.proto.doc_to_pb2(rd)
|
||||
|
||||
sys.stdout.buffer.write(pb.SerializeToString(deterministic=True))
|
||||
|
||||
@@ -1182,8 +1182,8 @@ def _039a6_dotnetfile_extractor():
|
||||
return get_dnfile_extractor(get_data_path_by_name("_039a6"))
|
||||
|
||||
|
||||
def get_result_doc(path):
|
||||
return capa.render.result_document.ResultDocument.parse_file(path)
|
||||
def get_result_doc(path: Path):
|
||||
return capa.render.result_document.ResultDocument.from_file(path)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
|
||||
@@ -237,7 +237,7 @@ def assert_round_trip(rd: rdoc.ResultDocument):
|
||||
one = rd
|
||||
|
||||
doc = one.model_dump_json(exclude_none=True)
|
||||
two = rdoc.ResultDocument.parse_raw(doc)
|
||||
two = rdoc.ResultDocument.model_validate_json(doc)
|
||||
|
||||
# show the round trip works
|
||||
# first by comparing the objects directly,
|
||||
@@ -272,13 +272,13 @@ def test_round_trip(request, rd_file):
|
||||
|
||||
def test_json_to_rdoc():
|
||||
path = fixtures.get_data_path_by_name("pma01-01-rd")
|
||||
assert isinstance(rdoc.ResultDocument.parse_file(path), rdoc.ResultDocument)
|
||||
assert isinstance(rdoc.ResultDocument.from_file(path), rdoc.ResultDocument)
|
||||
|
||||
|
||||
def test_rdoc_to_capa():
|
||||
path = fixtures.get_data_path_by_name("pma01-01-rd")
|
||||
|
||||
rd = rdoc.ResultDocument.parse_file(path)
|
||||
rd = rdoc.ResultDocument.from_file(path)
|
||||
|
||||
meta, capabilites = rd.to_capa()
|
||||
assert isinstance(meta, rdoc.Metadata)
|
||||
|
||||
Reference in New Issue
Block a user