Skip to content

Commit

Permalink
rendering: several fixes and added types/classes
Browse files Browse the repository at this point in the history
  • Loading branch information
yelhamer committed Oct 11, 2023
1 parent 8b287c1 commit 953b2e8
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 15 deletions.
2 changes: 0 additions & 2 deletions capa/features/extractors/cape/global_.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@
from capa.features.common import (
OS,
OS_ANY,
ARCH_ANY,
OS_LINUX,
ARCH_I386,
FORMAT_PE,
ARCH_AMD64,
FORMAT_ELF,
OS_WINDOWS,
FORMAT_UNKNOWN,
Arch,
Format,
Feature,
Expand Down
7 changes: 3 additions & 4 deletions capa/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1007,13 +1007,13 @@ def collect_metadata(
os_ = get_os(sample_path) if os_ == OS_AUTO else os_

if isinstance(extractor, StaticFeatureExtractor):
flavor = rdoc.Flavor.STATIC
meta_class: type = rdoc.StaticMetadata
elif isinstance(extractor, DynamicFeatureExtractor):
flavor = rdoc.Flavor.DYNAMIC
meta_class = rdoc.DynamicMetadata
else:
assert_never(extractor)

return rdoc.Metadata(
return meta_class(
timestamp=datetime.datetime.now(),
version=capa.version.__version__,
argv=tuple(argv) if argv else None,
Expand All @@ -1023,7 +1023,6 @@ def collect_metadata(
sha256=sha256,
path=Path(sample_path).resolve().as_posix(),
),
flavor=flavor,
analysis=get_sample_analysis(
format_,
arch,
Expand Down
10 changes: 10 additions & 0 deletions capa/render/result_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@ class Metadata(Model):
analysis: Analysis


class StaticMetadata(Metadata):
flavor: Flavor = Flavor.STATIC
analysis: StaticAnalysis


class DynamicMetadata(Metadata):
flavor: Flavor = Flavor.DYNAMIC
analysis: DynamicAnalysis


class CompoundStatementType:
AND = "and"
OR = "or"
Expand Down
15 changes: 7 additions & 8 deletions capa/render/verbose.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
See the License for the specific language governing permissions and limitations under the License.
"""
import enum
from typing import cast

import tabulate

Expand Down Expand Up @@ -75,7 +76,7 @@ def format_address(address: frz.Address) -> str:
raise ValueError("unexpected address type")


def render_static_meta(ostream, meta: rd.Metadata):
def render_static_meta(ostream, meta: rd.StaticMetadata):
"""
like:
Expand All @@ -96,7 +97,6 @@ def render_static_meta(ostream, meta: rd.Metadata):
total feature count 1918
"""

assert isinstance(meta.analysis, rd.StaticAnalysis)
rows = [
("md5", meta.sample.md5),
("sha1", meta.sample.sha1),
Expand All @@ -122,7 +122,7 @@ def render_static_meta(ostream, meta: rd.Metadata):
ostream.writeln(tabulate.tabulate(rows, tablefmt="plain"))


def render_dynamic_meta(ostream, meta: rd.Metadata):
def render_dynamic_meta(ostream, meta: rd.DynamicMetadata):
"""
like:
Expand All @@ -141,7 +141,6 @@ def render_dynamic_meta(ostream, meta: rd.Metadata):
total feature count 1918
"""

assert isinstance(meta.analysis, rd.DynamicAnalysis)
rows = [
("md5", meta.sample.md5),
("sha1", meta.sample.sha1),
Expand All @@ -166,10 +165,10 @@ def render_dynamic_meta(ostream, meta: rd.Metadata):


def render_meta(osstream, doc: rd.ResultDocument):
if isinstance(doc.meta.analysis, rd.StaticAnalysis):
render_static_meta(osstream, doc.meta)
elif isinstance(doc.meta.analysis, rd.DynamicAnalysis):
render_dynamic_meta(osstream, doc.meta)
if doc.meta.flavor is rd.Flavor.STATIC:
render_static_meta(osstream, cast(rd.StaticMetadata, doc.meta))
elif doc.meta.flavor is rd.Flavor.DYNAMIC:
render_dynamic_meta(osstream, cast(rd.DynamicMetadata, doc.meta))
else:
raise ValueError("invalid meta analysis")

Expand Down
1 change: 0 additions & 1 deletion scripts/profile-time.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
import capa.features
import capa.features.common
import capa.features.freeze
from capa.features.extractors.base_extractor import FeatureExtractor, StaticFeatureExtractor

logger = logging.getLogger("capa.profile")

Expand Down
Empty file added tests/test_dynamic_freeze.py
Empty file.

0 comments on commit 953b2e8

Please sign in to comment.