From 261baca683951b6e37c824d2d44eb533045e73bd Mon Sep 17 00:00:00 2001 From: Aayush Goel <81844215+Aayush-Goel-04@users.noreply.github.com> Date: Fri, 4 Aug 2023 01:35:41 +0530 Subject: [PATCH] updated deperecated functions --- capa/features/freeze/__init__.py | 2 +- capa/features/freeze/features.py | 48 ++++++++++++++++---------------- capa/render/proto/__init__.py | 4 +-- capa/render/result_document.py | 16 +++++------ capa/render/vverbose.py | 4 +-- 5 files changed, 37 insertions(+), 37 deletions(-) diff --git a/capa/features/freeze/__init__.py b/capa/features/freeze/__init__.py index c564a3ac..ebbf7d1d 100644 --- a/capa/features/freeze/__init__.py +++ b/capa/features/freeze/__init__.py @@ -45,7 +45,7 @@ class AddressType(str, Enum): class Address(HashableModel): type: AddressType - value: Union[int, Tuple[int, int], None] + value: Union[int, Tuple[int, int], None] = None @classmethod def from_capa(cls, a: capa.features.address.Address) -> "Address": diff --git a/capa/features/freeze/features.py b/capa/features/freeze/features.py index f4f18088..dd0b1f2f 100644 --- a/capa/features/freeze/features.py +++ b/capa/features/freeze/features.py @@ -211,141 +211,141 @@ def feature_from_capa(f: capa.features.common.Feature) -> "Feature": class OSFeature(FeatureModel): type: str = "os" os: str - description: Optional[str] + description: Optional[str] = None class ArchFeature(FeatureModel): type: str = "arch" arch: str - description: Optional[str] + description: Optional[str] = None class FormatFeature(FeatureModel): type: str = "format" format: str - description: Optional[str] + description: Optional[str] = None class MatchFeature(FeatureModel): type: str = "match" match: str - description: Optional[str] + description: Optional[str] = None class CharacteristicFeature(FeatureModel): type: str = "characteristic" characteristic: str - description: Optional[str] + description: Optional[str] = None class ExportFeature(FeatureModel): type: str = "export" export: str - description: Optional[str] + description: Optional[str] = None class ImportFeature(FeatureModel): type: str = "import" import_: str = Field(alias="import") - description: Optional[str] + description: Optional[str] = None class SectionFeature(FeatureModel): type: str = "section" section: str - description: Optional[str] + description: Optional[str] = None class FunctionNameFeature(FeatureModel): type: str = "function name" function_name: str = Field(alias="function name") - description: Optional[str] + description: Optional[str] = None class SubstringFeature(FeatureModel): type: str = "substring" substring: str - description: Optional[str] + description: Optional[str] = None class RegexFeature(FeatureModel): type: str = "regex" regex: str - description: Optional[str] + description: Optional[str] = None class StringFeature(FeatureModel): type: str = "string" string: str - description: Optional[str] + description: Optional[str] = None class ClassFeature(FeatureModel): type: str = "class" class_: str = Field(alias="class") - description: Optional[str] + description: Optional[str] = None class NamespaceFeature(FeatureModel): type: str = "namespace" namespace: str - description: Optional[str] + description: Optional[str] = None class BasicBlockFeature(FeatureModel): type: str = "basic block" - description: Optional[str] + description: Optional[str] = None class APIFeature(FeatureModel): type: str = "api" api: str - description: Optional[str] + description: Optional[str] = None class PropertyFeature(FeatureModel): type: str = "property" - access: Optional[str] + access: Optional[str] = None property: str - description: Optional[str] + description: Optional[str] = None class NumberFeature(FeatureModel): type: str = "number" number: Union[int, float] - description: Optional[str] + description: Optional[str] = None class BytesFeature(FeatureModel): type: str = "bytes" bytes: str - description: Optional[str] + description: Optional[str] = None class OffsetFeature(FeatureModel): type: str = "offset" offset: int - description: Optional[str] + description: Optional[str] = None class MnemonicFeature(FeatureModel): type: str = "mnemonic" mnemonic: str - description: Optional[str] + description: Optional[str] = None class OperandNumberFeature(FeatureModel): type: str = "operand number" index: int operand_number: int = Field(alias="operand number") - description: Optional[str] + description: Optional[str] = None class OperandOffsetFeature(FeatureModel): type: str = "operand offset" index: int operand_offset: int = Field(alias="operand offset") - description: Optional[str] + description: Optional[str] = None Feature = Union[ diff --git a/capa/render/proto/__init__.py b/capa/render/proto/__init__.py index 2457b7ec..03aed65c 100644 --- a/capa/render/proto/__init__.py +++ b/capa/render/proto/__init__.py @@ -126,7 +126,7 @@ def metadata_to_pb2(meta: rd.Metadata) -> capa_pb2.Metadata: timestamp=str(meta.timestamp), version=meta.version, argv=meta.argv, - sample=google.protobuf.json_format.ParseDict(meta.sample.dict(), capa_pb2.Sample()), + sample=google.protobuf.json_format.ParseDict(meta.sample.model_dump(), capa_pb2.Sample()), analysis=capa_pb2.Analysis( format=meta.analysis.format, arch=meta.analysis.arch, @@ -393,7 +393,7 @@ def match_to_pb2(match: rd.Match) -> capa_pb2.Match: def rule_metadata_to_pb2(rule_metadata: rd.RuleMetadata) -> capa_pb2.RuleMetadata: # after manual type conversions to the RuleMetadata, we can rely on the protobuf json parser # conversions include tuple -> list and rd.Enum -> proto.enum - meta = dict_tuple_to_list_values(rule_metadata.dict()) + meta = dict_tuple_to_list_values(rule_metadata.model_dump()) meta["scope"] = scope_to_pb2(meta["scope"]) meta["attack"] = list(map(dict_tuple_to_list_values, meta.get("attack", []))) meta["mbc"] = list(map(dict_tuple_to_list_values, meta.get("mbc", []))) diff --git a/capa/render/result_document.py b/capa/render/result_document.py index 47591f24..03ea245f 100644 --- a/capa/render/result_document.py +++ b/capa/render/result_document.py @@ -7,7 +7,7 @@ # See the License for the specific language governing permissions and limitations under the License. import datetime import collections -from typing import Dict, List, Tuple, Union, Optional +from typing import Dict, List, Tuple, Union, Literal, Optional from pydantic import Field, BaseModel, ConfigDict @@ -80,7 +80,7 @@ class Analysis(Model): class Metadata(Model): timestamp: datetime.datetime version: str - argv: Optional[Tuple[str, ...]] + argv: Optional[Tuple[str, ...]] = None sample: Sample analysis: Analysis @@ -102,13 +102,13 @@ class CompoundStatement(StatementModel): class SomeStatement(StatementModel): - type: str = "some" + type: Literal["some"] = "some" description: Optional[str] = None count: int class RangeStatement(StatementModel): - type: str = "range" + type: Literal["range"] = "range" description: Optional[str] = None min: int max: int @@ -116,7 +116,7 @@ class RangeStatement(StatementModel): class SubscopeStatement(StatementModel): - type: str = "subscope" + type: Literal["subscope"] = "subscope" description: Optional[str] = None scope: capa.rules.Scope @@ -131,7 +131,7 @@ Statement = Union[ class StatementNode(FrozenModel): - type: str = "statement" + type: Literal["statement"] = "statement" statement: Statement @@ -168,7 +168,7 @@ def statement_from_capa(node: capa.engine.Statement) -> Statement: class FeatureNode(FrozenModel): - type: str = "feature" + type: Literal["feature"] = "feature" feature: frz.Feature @@ -502,7 +502,7 @@ class MaecMetadata(FrozenModel): class RuleMetadata(FrozenModel): name: str - namespace: Optional[str] + namespace: Optional[str] = None authors: Tuple[str, ...] scope: capa.rules.Scope attack: Tuple[AttackSpec, ...] = Field(alias="att&ck") diff --git a/capa/render/vverbose.py b/capa/render/vverbose.py index 59189833..03ff8c84 100644 --- a/capa/render/vverbose.py +++ b/capa/render/vverbose.py @@ -88,7 +88,7 @@ def render_statement(ostream, match: rd.Match, statement: rd.Statement, indent=0 # so, we have to inline some of the feature rendering here. child = statement.child - value = child.dict(by_alias=True).get(child.type) + value = child.model_dump(by_alias=True).get(child.type) if value: if isinstance(child, frzf.StringFeature): @@ -141,7 +141,7 @@ def render_feature(ostream, match: rd.Match, feature: frzf.Feature, indent=0): value = feature.class_ else: # convert attributes to dictionary using aliased names, if applicable - value = feature.dict(by_alias=True).get(key) + value = feature.model_dump(by_alias=True).get(key) if value is None: raise ValueError(f"{key} contains None")