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

Extracts web domains and IP address and implements tests #2031

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
1b6bdaa
This pull request extracts web domains and IP addresses from files an…
aaronatp Mar 19, 2024
999f656
Update domain_ip_helpers.py
aaronatp Mar 19, 2024
80fc4d4
Update extract_domain_and_ip.py
aaronatp Mar 19, 2024
2bb09f8
Update test_domain_ip_extractor.py
aaronatp Mar 19, 2024
61c4ad5
Adds to changelog and fixes string concatenation style error
aaronatp Mar 19, 2024
627f187
Merge branch 'master' into master
aaronatp Mar 19, 2024
9b41074
Help debug why it won't build with PyInstaller 3.11
aaronatp Mar 19, 2024
6236b8f
Update capa/capabilities/extract_domain_and_ip.py
aaronatp Mar 19, 2024
f9f6bb4
Update capa/capabilities/extract_domain_and_ip.py
aaronatp Mar 19, 2024
aa5f542
Update verbose.py
aaronatp Mar 19, 2024
2e9c1a9
Update vverbose.py
aaronatp Mar 19, 2024
11585c3
Update extract_domain_and_ip.py
aaronatp Mar 19, 2024
58b0336
Fix PyInstaller 3.11 installation error with get_signatures
aaronatp Mar 19, 2024
c55169b
Fix get_extractor_from_doc issue for CAPE
aaronatp Mar 19, 2024
85dfa55
Fix 'backend' assignment in get_extractor_from_doc
aaronatp Mar 20, 2024
5afd175
Fix 'format' in get_extractor_from_doc
aaronatp Mar 20, 2024
78d0efc
Merge branch 'master' into master
aaronatp Mar 20, 2024
681e6c3
Reformat imports
aaronatp Mar 20, 2024
fb3ed8a
Reformat multi-line string
aaronatp Mar 20, 2024
0443caf
Correct Flake8 errors
aaronatp Mar 20, 2024
a000461
Update domain_ip_helpers.py
aaronatp Mar 22, 2024
93943e5
Update domain_ip_helpers.py
aaronatp Mar 22, 2024
32b4778
Update capa/render/verbose.py
aaronatp Mar 22, 2024
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
### New Features

- add function in capa/helpers to load plain and compressed JSON reports #1883 @Rohit1123

- document Antivirus warnings and VirusTotal false positive detections #2028 @RionEV @mr-tz

- extracts and prints web domains/IP addresses and potential WinAPI networking functions #2031 @aaronatp

### Breaking Changes


Expand Down
122 changes: 122 additions & 0 deletions capa/capabilities/domain_ip_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
# Copyright (C) 2024 Mandiant, Inc. All Rights Reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at: [package root]/LICENSE.txt
# 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 logging
from pathlib import Path

from capa.helpers import get_auto_format
from capa.exceptions import UnsupportedFormatError
from capa.features.common import FORMAT_CAPE, FORMAT_DOTNET, FORMAT_FREEZE, FORMAT_UNKNOWN
from capa.render.result_document import ResultDocument
from capa.features.extractors.base_extractor import FeatureExtractor

logger = logging.getLogger(__name__)


def get_file_path(doc: ResultDocument) -> Path:
return Path(doc.meta.sample.path)


def get_sigpaths_from_doc(doc: ResultDocument):
import capa.loader
from capa.main import get_default_root

if doc.meta.argv:
try:
if "-s" in list(doc.meta.argv):
idx = doc.meta.argv.index("-s")
sigpath = Path(doc.meta.argv[idx + 1])
if "./" in str(sigpath):
fixed_str = str(sigpath).split("./")[1]
sigpath = Path(fixed_str)

elif "--signatures" in list(doc.meta.argv):
idx = doc.meta.argv.index("--signatures")
sigpath = Path(doc.meta.argv[idx + 1])
if "./" in str(sigpath):
fixed_str = str(sigpath).split("./")[1]
sigpath = Path(fixed_str)

else:
sigpath = get_default_root() / "sigs"

return capa.loader.get_signatures(sigpath)

except AttributeError:
raise NotImplementedError("Confirm that argv is an attribute of doc.meta")

else:
logger.debug("'doc.meta' has not attribute 'argv'")


def get_extractor_from_doc(doc: ResultDocument) -> FeatureExtractor:
# import here to avoid circular import
from capa.loader import BACKEND_VIV, BACKEND_CAPE, BACKEND_DOTNET, BACKEND_FREEZE, get_extractor

path = get_file_path(doc)
os = doc.meta.analysis.os

if doc.meta.argv:
args = tuple(doc.meta.argv)
else:
CommandLineArgumentsError("Couldn't find command line arguments!")

for i in range(len(args)):
if args[i] == any(["-f", "--format"]):
format = args[i + 1]
break
else:
format = ""

if format == "":
format = get_auto_format(path)
if format == FORMAT_UNKNOWN:
raise UnsupportedFormatError(f"Couldn't get format for {path.name}")

for i in range(len(args)):
if args[i] == any(["-b", "--backend"]):
backend = args[i + 1]
break
elif format == FORMAT_CAPE:
backend = BACKEND_CAPE
break
elif format == FORMAT_DOTNET:
backend = BACKEND_DOTNET
break
elif format == FORMAT_FREEZE:
backend = BACKEND_FREEZE
break
else:
backend = ""

if backend == "":
backend = BACKEND_VIV

sigpath = get_sigpaths_from_doc(doc)

import capa.helpers

logger.debug(f"running standable == {capa.helpers.is_running_standalone()}")

raise QuickExitError()

return get_extractor(
input_path=path,
input_format=format,
os_=os,
backend=backend,
sigpaths=sigpath,
)


class CommandLineArgumentsError(BaseException):
pass


class QuickExitError(BaseException):
pass
Loading
Loading