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

Linux support #2001

Merged
merged 32 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
45bb5e0
feat: linux analysis api captured to log and report json
kenleejl Sep 14, 2023
965234c
add: strace processing module process tree
winson0123 Oct 5, 2023
6bae8c8
add: strace analysis for frontend
winson0123 Oct 6, 2023
d790edd
add: strace page
winson0123 Oct 6, 2023
090afe6
add: strace view in frontend
winson0123 Oct 6, 2023
e56acea
refractor: strace argument parsing on frontend
winson0123 Oct 9, 2023
ab84cb4
fix: strace tab
kenleejl Oct 11, 2023
db80c82
refactor: accurate args fetched by syscall number and categorisation
winson0123 Oct 20, 2023
76a6129
refactor: strace processing output similar to behavior output
winson0123 Oct 26, 2023
8387522
add: linux agent installation script
winson0123 Nov 1, 2023
5058082
refactor: strace process calls store as objects in mongodb
winson0123 Nov 8, 2023
2670a97
feat: behavioral analysis tab for linux syscalls
winson0123 Nov 9, 2023
cae3428
fix: strace process tree on behavioral tab
winson0123 Nov 10, 2023
77b5c10
feat: filename match w/ file descriptor in sycall
winson0123 Dec 5, 2023
5583f9e
fix: strace processing crashes upon unclosed file descriptors
winson0123 Dec 21, 2023
14498f6
refactor: reformatted strace processing
winson0123 Jan 25, 2024
901bd84
add: more syscalls that utilize file descriptors
winson0123 Feb 1, 2024
a2b01bb
fix: default file descriptors
winson0123 Feb 7, 2024
1e8b614
fix: allow 32bit to run on 64bit for linux
winson0123 Feb 7, 2024
92841fa
fix: syscall incorrect arguments
winson0123 Feb 19, 2024
48d08f8
refactor: strace processing readability
winson0123 Feb 23, 2024
f15de26
refactor: move "strace" to "behavior" key
winson0123 Feb 26, 2024
cbf3969
refactor: add machine platform to cape report
winson0123 Mar 6, 2024
2145cf2
refactor: migrate strace implementation to behavior
winson0123 Mar 6, 2024
60ab9c2
update: alembic upgrades and schema version
winson0123 Mar 7, 2024
f0367fe
chore: remove unused import functions
winson0123 Mar 7, 2024
a2476e3
fix: update platform accept nullable
winson0123 Mar 8, 2024
add23ac
chore: removal of systemtap-based support
winson0123 Mar 11, 2024
b75bec3
Linux and Config
doomedraven Mar 12, 2024
f2a9bfb
confs
doomedraven Mar 13, 2024
37280fa
Update config.py
doomedraven Mar 13, 2024
2581393
Merge branch 'master' into pr/2001
doomedraven Mar 13, 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
8 changes: 7 additions & 1 deletion analyzer/linux/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def run(self):
if not package_class:
raise Exception("Could not find an appropriate analysis package")
# Package initialization
kwargs = {"options": self.config.options, "timeout": self.config.timeout}
kwargs = {"options": self.config.options, "timeout": self.config.timeout, "strace_ouput": PATHS["logs"]}

# Initialize the analysis package.
# pack = package_class(self.config.get_options())
Expand Down Expand Up @@ -306,6 +306,12 @@ def run(self):
upload_to_host(package[0], os.path.join("files", package[1]))
except Exception as e:
log.warning('The package "%s" package_files function raised an exception: %s', package_class, e)
try:
# Upload the strace logs to host
for file in os.listdir(PATHS["logs"]):
upload_to_host(os.path.join(PATHS["logs"], file), os.path.join("logs", file))
except Exception as e:
log.warning('The strace log failed to transfer:', e)

# Terminate the Auxiliary modules.
log.info("Stopping auxiliary modules")
Expand Down
61 changes: 0 additions & 61 deletions analyzer/linux/lib/common/apicalls.py

This file was deleted.

54 changes: 15 additions & 39 deletions analyzer/linux/lib/core/packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
import logging
import subprocess
import timeit
from os import environ, path, sys, waitpid
from os import environ, path, sys

from lib.api.process import Process
from lib.common.apicalls import apicalls
from lib.common.results import NetlogFile

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -97,6 +96,7 @@ def __init__(self, target, **kwargs):
self.free = self.options.get("free")
self.proc = None
self.pids = []
self.strace_output = kwargs.get("strace_ouput", "/tmp")

def set_pids(self, pids):
"""Update list of monitored PIDs in the package context.
Expand All @@ -118,18 +118,9 @@ def start(self):
# Remove the trailing slash (if any)
self.target = filepath.rstrip("/")
self.prepare()
self.normal_analysis()
# self.normal_analysis()
self.strace_analysis()
return self.proc.pid
"""
if self.free:
self.normal_analysis()
return self.proc.pid
elif self.method == "apicalls":
self.apicalls_analysis()
return self.proc.pid
else:
raise Exception("Unsupported analysis method. Try 'apicalls'")
"""

def check(self):
"""Check."""
Expand Down Expand Up @@ -157,38 +148,36 @@ def finish(self):
def get_pids(self):
return []

def apicalls_analysis(self):
def strace_analysis(self):
kwargs = {"args": self.args, "timeout": self.timeout, "run_as_root": self.run_as_root}
log.info(self.target)
cmd = apicalls(self.target, **kwargs)
stap_start = timeit.default_timer()

target_cmd = f'{self.target}'
if "args" in kwargs:
target_cmd += f' {" ".join(kwargs["args"])}'

cmd = f"sudo strace -ttffn -o {self.strace_output}/strace.log {target_cmd}"
log.info(cmd)
self.proc = subprocess.Popen(
cmd, env={"XAUTHORITY": "/root/.Xauthority", "DISPLAY": ":0"}, stderr=subprocess.PIPE, shell=True
)

while b"systemtap_module_init() returned 0" not in self.proc.stderr.readline():
# log.debug(self.proc.stderr.readline())
pass

stap_stop = timeit.default_timer()
log.info("Process startup took %.2f seconds", stap_stop - stap_start)
log.info("Process started with strace")
return True

def normal_analysis(self):
kwargs = {"args": self.args, "timeout": self.timeout, "run_as_root": self.run_as_root}

# cmd = apicalls(self.target, **kwargs)
cmd = f"{self.target} {' '.join(kwargs['args'])}"
stap_start = timeit.default_timer()
process_start = timeit.default_timer()
self.proc = subprocess.Popen(
cmd, env={"XAUTHORITY": "/root/.Xauthority", "DISPLAY": ":0"}, stderr=subprocess.PIPE, shell=True
)

log.debug(self.proc.stderr.readline())

stap_stop = timeit.default_timer()
log.info("Process startup took %.2f seconds", stap_stop - stap_start)
process_stop = timeit.default_timer()
log.info("Process startup took %.2f seconds", process_start - process_stop)
return True

@staticmethod
Expand All @@ -200,19 +189,6 @@ def _upload_file(local, remote):
nf.sock.sendall(chunk) # dirty direct send, no reconnecting
nf.close()

def stop(self):
log.info("Package requested stop")
try:
r = self.proc.poll()
log.debug("stap subprocess retval %d", r)
self.proc.kill()
# subprocess.check_call(["sudo", "kill", str(self.proc.pid)])
waitpid(self.proc.pid, 0)
self._upload_file("stap.log", "logs/all.stap")
except Exception as e:
log.warning("Exception uploading log: %s", e)


def _string_to_bool(raw):
if not isinstance(raw, str):
raise Exception("Unexpected input: not a string :/")
Expand Down
2 changes: 1 addition & 1 deletion analyzer/linux/modules/auxiliary/filecollector.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def _method_name(self, event):
# log.info("Path is a directory or does not exist, ignoring: %s", event.pathname)
return

if os.path.basename(event.pathname) == "stap.log":
if "strace.log" in os.path.basename(event.pathname):
return

try:
Expand Down
87 changes: 0 additions & 87 deletions analyzer/linux/modules/auxiliary/stap.py

This file was deleted.

5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
### [12.03.2024]
* Monitor update: Initial IPv6 support - thanks @cccs-mog
* Linux support details can be seen in this [Pull Request](https://github.com/kevoreilly/CAPEv2/pull/2001)
* We remove all `x.conf` to finish the mess with the configs.
* DO NOT EDIT `.conf.default` files. cape2.sh makes a copy of them removing `.default`.
* If you don't use `cape2.sh`.
* Run: `for filename in conf/default/*.conf.default; do cp -vf "./$filename" "./$(echo "$filename" | sed -e 's/.default//g' | sed -e 's/default//g')"; done`

### [07.03.2024]
* Monitor updates:
Expand Down
Loading