Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sync master #1615

Closed
wants to merge 39 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
25a6d78
ruff: update config
williballenthin Jul 6, 2023
6f0d1f7
add pre-commit config
williballenthin Jul 6, 2023
75a76b4
setup: add pre-commit dev dependency
williballenthin Jul 6, 2023
691ef1c
remove old linter configs
williballenthin Jul 6, 2023
f17edb3
ci: use pre-commit to invoke linters
williballenthin Jul 6, 2023
adbfb8d
doc: installation: document pre-commit
williballenthin Jul 6, 2023
47074fd
fix ruff issues
williballenthin Jul 6, 2023
9441da4
isort
williballenthin Jul 6, 2023
90e607f
flake8
williballenthin Jul 6, 2023
511aa0f
doc: installation: more details on pre-commit
williballenthin Jul 6, 2023
e675bef
ci: invoke linter directly
williballenthin Jul 6, 2023
a43d2c1
tests: fix fixture imports
williballenthin Jul 6, 2023
982dc46
add flake8-bugbear linter
williballenthin Jul 6, 2023
9f6165f
doc: installation: better enumerate current linters
williballenthin Jul 6, 2023
3ad4de7
gitignore
williballenthin Jul 6, 2023
ff47270
add flake8-encoding plugin
williballenthin Jul 6, 2023
13a8e25
introduce flake8-comprehensions
williballenthin Jul 6, 2023
3ca233e
Merge branch 'master' into fix/issue-1579
williballenthin Jul 7, 2023
8c86011
changelog
williballenthin Jul 6, 2023
fc1dd40
Sync capa rules submodule
capa-bot Jul 8, 2023
54203f3
introduce flake8-logging-format linter
williballenthin Jul 9, 2023
7fe738e
introduce flake8-no-implicit-concat linter
williballenthin Jul 9, 2023
106b12e
move flake8 config to its own config file
williballenthin Jul 9, 2023
4a49543
introduce flake8-print linter
williballenthin Jul 9, 2023
ae10a2e
introduce flake8-todos linter
williballenthin Jul 9, 2023
118b955
features: fix circular import
williballenthin Jul 9, 2023
dd2bbc9
migrate to pyproject.toml
williballenthin Jul 9, 2023
fb17619
changelog
williballenthin Jul 9, 2023
cb289e3
ci: publish: use trusted publishing
williballenthin Jul 9, 2023
81b9643
ci: publish to PyPI using trusted publishing
williballenthin Jul 10, 2023
1f8aa7c
changelog
williballenthin Jul 10, 2023
430f9da
Merge branch 'master' into fix/issue-1579
williballenthin Jul 10, 2023
d89dd49
add issue links for TODOs
williballenthin Jul 9, 2023
a712bf3
Sync capa rules submodule
capa-bot Jul 10, 2023
f983307
Merge branch 'master' into fix/issue-1579
williballenthin Jul 10, 2023
506d677
Merge pull request #1591 from mandiant/fix/issue-1579
williballenthin Jul 10, 2023
ac12d5a
Merge pull request #1611 from mandiant/fix/issue-1301
williballenthin Jul 10, 2023
320539b
Merge branch 'master' into fix/issue-1491
williballenthin Jul 10, 2023
1373fab
Merge pull request #1613 from mandiant/fix/issue-1491
williballenthin Jul 10, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .github/flake8.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[flake8]
max-line-length = 120

extend-ignore =
# E203: whitespace before ':' (black does this)
E203,
# F401: `foo` imported but unused (prefer ruff)
F401,
# F811 Redefinition of unused `foo` (prefer ruff)
F811,
# E501 line too long (prefer black)
E501,
# B010 Do not call setattr with a constant attribute value
B010,
# G200 Logging statement uses exception in arguments
G200


per-file-ignores =
# T201 print found.
#
# scripts are meant to print output
scripts/*: T201
# capa.exe is meant to print output
capa/main.py: T201
# IDA tests emit results to output window so need to print
tests/test_ida_features.py: T201
# utility used to find the Binary Ninja API via invoking python.exe
capa/features/extractors/binja/find_binja_api.py: T201
63 changes: 57 additions & 6 deletions .github/ruff.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,61 @@
# Enable pycodestyle (`E`) codes
select = ["E"]
# Enable the pycodestyle (`E`) and Pyflakes (`F`) rules by default.
# Unlike Flake8, Ruff doesn't enable pycodestyle warnings (`W`) or
# McCabe complexity (`C901`) by default.
select = ["E", "F"]

# Allow autofix for all enabled rules (when `--fix`) is provided.
fixable = ["ALL"]
unfixable = []

# E402 module level import not at top of file
# E722 do not use bare 'except'
ignore = ["E402", "E722"]
exclude = ["*_pb2.py", "*_pb2.pyi"]
# E501 line too long
ignore = ["E402", "E722", "E501"]

line-length = 120

exclude = [
# Exclude a variety of commonly ignored directories.
".bzr",
".direnv",
".eggs",
".git",
".git-rewrite",
".hg",
".mypy_cache",
".nox",
".pants.d",
".pytype",
".ruff_cache",
".svn",
".tox",
".venv",
"__pypackages__",
"_build",
"buck-out",
"build",
"dist",
"node_modules",
"venv",
# protobuf generated files
"*_pb2.py",
"*_pb2.pyi"
]

# Same as pycodestyle.
line-length = 180
[per-file-ignores]
# until we address #1592 and move test fixtures into conftest.py
# then we need to ignore imports done to enable pytest fixtures.
#
# F401: `foo` imported but unused
# F811 Redefinition of unused `foo`
"tests/test_main.py" = ["F401", "F811"]
"tests/test_proto.py" = ["F401", "F811"]
"tests/test_freeze.py" = ["F401", "F811"]
"tests/test_function_id.py" = ["F401", "F811"]
"tests/test_viv_features.py" = ["F401", "F811"]
"tests/test_binja_features.py" = ["F401", "F811"]
"tests/test_pefile_features.py" = ["F401", "F811"]
"tests/test_dnfile_features.py" = ["F401", "F811"]
"tests/test_dotnet_features.py" = ["F401", "F811"]
"tests/test_result_document.py" = ["F401", "F811"]
"tests/test_dotnetfile_features.py" = ["F401", "F811"]
10 changes: 0 additions & 10 deletions .github/tox.ini

This file was deleted.

40 changes: 28 additions & 12 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
# This workflows will upload a Python Package using Twine when a release is created
# For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries

# use PyPI trusted publishing, as described here:

Check failure

Code scanning / Scorecard

Token-Permissions High

score is 0: no topLevel permission defined
Remediation tip: update your workflow using https://app.stepsecurity.io
Click Remediation section below for further remediation help
# https://blog.trailofbits.com/2023/05/23/trusted-publishing-a-new-benchmark-for-packaging-security/
name: publish to pypi

on:
release:
types: [published]

jobs:
deploy:
runs-on: ubuntu-20.04
pypi-publish:
runs-on: ubuntu-latest
environment:
name: release
permissions:
id-token: write
steps:
- uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c # v3.3.0
- name: Set up Python
Expand All @@ -19,11 +22,24 @@
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install setuptools wheel twine
- name: Build and publish
env:
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
pip install -e .[build]
Dismissed Show dismissed Hide dismissed
- name: build package
run: |
python setup.py sdist bdist_wheel
twine upload --skip-existing dist/*
python -m build
- name: upload package artifacts
uses: actions/upload-artifact@0b7f8abb1508181956e8e162db84b466c27e18ce # v3.1.2
with:
name: ${{ matrix.asset_name }}
path: dist/*
- name: upload package to GH Release
uses: svenstaro/upload-release-action@2728235f7dc9ff598bd86ce3c274b74f802d2208 # v2
with:
repo_token: ${{ secrets.GITHUB_TOKEN}}
file: dist/*
tag: ${{ github.ref }}
- name: publish package
uses: pypa/gh-action-pypi-publish@f5622bde02b04381239da3573277701ceca8f6a0 # release/v1
with:
skip-existing: true
verbose: true
print-hash: true
14 changes: 7 additions & 7 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ jobs:
- name: Install dependencies
run: pip install -e .[dev]
- name: Lint with ruff
run: ruff check --config .github/ruff.toml .
run: pre-commit run ruff
- name: Lint with isort
run: isort --profile black --length-sort --line-width 120 --skip-glob "*_pb2.py" -c .
run: pre-commit run isort
- name: Lint with black
run: black -l 120 --extend-exclude ".*_pb2.py" --check .
- name: Lint with pycodestyle
run: pycodestyle --exclude="*_pb2.py" --show-source capa/ scripts/ tests/
run: pre-commit run black
- name: Lint with flake8
run: pre-commit run flake8
- name: Check types with mypy
run: mypy --config-file .github/mypy/mypy.ini --check-untyped-defs capa/ scripts/ tests/
run: pre-commit run mypy

rule_linter:
runs-on: ubuntu-20.04
Expand All @@ -56,7 +56,7 @@ jobs:
with:
python-version: "3.8"
- name: Install capa
run: pip install -e .
run: pip install -e .[dev]
- name: Run rule linter
run: python scripts/lint.py rules/

Expand Down
7 changes: 2 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,10 @@ venv.bak/
*.viv
*.idb
*.i64
.vscode

!rules/lib

# hooks/ci.sh output
isort-output.log
black-output.log
rule-linter-output.log
.vscode
scripts/perf/*.txt
scripts/perf/*.svg
scripts/perf/*.zip
Expand Down
111 changes: 111 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# install the pre-commit hooks:
#
# ❯ pre-commit install --hook-type pre-commit
# pre-commit installed at .git/hooks/pre-commit
#
# ❯ pre-commit install --hook-type pre-push
# pre-commit installed at .git/hooks/pre-push
#
# run all linters liks:
#
# ❯ pre-commit run --all-files
# isort....................................................................Passed
# black....................................................................Passed
# ruff.....................................................................Passed
# flake8...................................................................Passed
# mypy.....................................................................Passed
#
# run a single linter like:
#
# ❯ pre-commit run --all-files isort
# isort....................................................................Passed

repos:
- repo: local
hooks:
- id: isort
name: isort
stages: [commit, push]
language: system
entry: isort
args:
- "--length-sort"
- "--profile"
- "black"
- "--line-length=120"
- "--skip-glob"
- "*_pb2.py"
- "capa/"
- "scripts/"
- "tests/"
always_run: true
pass_filenames: false

- repo: local
hooks:
- id: black
name: black
stages: [commit, push]
language: system
entry: black
args:
- "--line-length=120"
- "--extend-exclude"
- ".*_pb2.py"
- "capa/"
- "scripts/"
- "tests/"
always_run: true
pass_filenames: false

- repo: local
hooks:
- id: ruff
name: ruff
stages: [commit, push]
language: system
entry: ruff
args:
- "check"
- "--config"
- ".github/ruff.toml"
- "capa/"
- "scripts/"
- "tests/"
always_run: true
pass_filenames: false

- repo: local
hooks:
- id: flake8
name: flake8
stages: [commit, push]
language: system
entry: flake8
args:
- "--config"
- ".github/flake8.ini"
- "--extend-exclude"
- "capa/render/proto/capa_pb2.py"
- "capa/"
- "scripts/"
- "tests/"
always_run: true
pass_filenames: false

- repo: local
hooks:
- id: mypy
name: mypy
stages: [commit, push]
language: system
entry: mypy
args:
- "--check-untyped-defs"
- "--ignore-missing-imports"
- "--config-file=.github/mypy/mypy.ini"
- "capa/"
- "scripts/"
- "tests/"
always_run: true
pass_filenames: false
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
### New Features
- Utility script to detect feature overlap between new and existing CAPA rules [#1451](https://github.com/mandiant/capa/issues/1451) [@Aayush-Goel-04](https://github.com/aayush-goel-04)
- use fancy box drawing characters for default output #1586 @williballenthin
- use [pre-commit](https://pre-commit.com/) to invoke linters #1579 @williballenthin
- publish via PyPI trusted publishing #1491 @williballenthin
- migrate to pyproject.toml #1301 @williballenthin

### Breaking Changes
- Update Metadata type in capa main [#1411](https://github.com/mandiant/capa/issues/1411) [@Aayush-Goel-04](https://github.com/aayush-goel-04) @manasghandat
- Python 3.8 is now the minimum supported Python version #1578 @williballenthin

### New Rules (22)
### New Rules (23)

- load-code/shellcode/execute-shellcode-via-windows-callback-function [email protected] [email protected]
- nursery/execute-shellcode-via-indirect-call [email protected]
Expand All @@ -34,6 +37,7 @@
- anti-analysis/anti-debugging/debugger-evasion/hide-thread-from-debugger [email protected] [email protected]
- host-interaction/memory/create-new-application-domain-in-dotnet [email protected]
- host-interaction/gui/switch-active-desktop [email protected]
- host-interaction/service/query-service-configuration @mr-tz
-

### Bug Fixes
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/flare-capa)](https://pypi.org/project/flare-capa)
[![Last release](https://img.shields.io/github/v/release/mandiant/capa)](https://github.com/mandiant/capa/releases)
[![Number of rules](https://img.shields.io/badge/rules-810-blue.svg)](https://github.com/mandiant/capa-rules)
[![Number of rules](https://img.shields.io/badge/rules-811-blue.svg)](https://github.com/mandiant/capa-rules)
[![CI status](https://github.com/mandiant/capa/workflows/CI/badge.svg)](https://github.com/mandiant/capa/actions?query=workflow%3ACI+event%3Apush+branch%3Amaster)
[![Downloads](https://img.shields.io/github/downloads/mandiant/capa/total)](https://github.com/mandiant/capa/releases)
[![License](https://img.shields.io/badge/license-Apache--2.0-green.svg)](LICENSE.txt)
Expand Down
6 changes: 3 additions & 3 deletions capa/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import copy
import collections
from typing import TYPE_CHECKING, Set, Dict, List, Tuple, Union, Mapping, Iterable, Iterator, cast
from typing import TYPE_CHECKING, Set, Dict, List, Tuple, Union, Mapping, Iterable, Iterator

import capa.perf
import capa.features.common
Expand Down Expand Up @@ -71,7 +71,7 @@ def get_children(self) -> Iterator[Union["Statement", Feature]]:
yield child

if hasattr(self, "children"):
for child in getattr(self, "children"):
for child in self.children:
assert isinstance(child, (Statement, Feature))
yield child

Expand All @@ -83,7 +83,7 @@ def replace_child(self, existing, new):
self.child = new

if hasattr(self, "children"):
children = getattr(self, "children")
children = self.children
for i, child in enumerate(children):
if child is existing:
children[i] = new
Expand Down
Loading
Loading