-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added bpftrace backend with tests, Added docker
Feature/bpftrace backend
- Loading branch information
Showing
54 changed files
with
2,184 additions
and
655 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Try secimport with bpftrace | ||
|
||
## How to Use | ||
|
||
1. Install Docker: https://docs.docker.com/get-docker | ||
2. ./build.sh | ||
- will build a docker image with | ||
- python with dtrace static USDT instrumentations | ||
- bpftrace | ||
- secimport code | ||
- ~1GB in size | ||
3. ./run.sh | ||
- Runs temporary example sandbox using bpftrace | ||
- Then, it will execute os.system('ps'). | ||
- the process should be killed. | ||
- Once the process is killed, it prints the logs of the sandbox. | ||
|
||
|
||
## FAQ | ||
|
||
### How it runs on macOS? | ||
- The Docker for mac runs Linux on a hypervisor called hyperkit, and docker runs inside it, so you can use Linux features. | ||
|
||
### Can we trace a macOS host with this docker? | ||
- Not at the moment. The bpftrace runs inside a Linux VM. | ||
- For macOS, there is dtrace. | ||
|
||
===================== | ||
|
||
Based on the great example repo: https://github.com/mmisono/try-bpftrace-in-mac |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#!/bin/bash | ||
|
||
if [[ "$PWD" =~ docker$ ]] | ||
then | ||
echo "Building secimport docker container..."; | ||
else | ||
echo "Please run this script from the secimport/docker directory."; | ||
exit 1; | ||
fi | ||
|
||
# linukit kernel version | ||
KERNEL_VERSION=`docker run --rm -it alpine uname -r | cut -d'-' -f1` | ||
BPFTRACE_VERSION=${BPFTRACE_VERSION:-v0.16.0} | ||
PYTHON_VERSION=${PYTHON_VERSION:-"3.10.0"} | ||
|
||
pushd docker | ||
|
||
docker build \ | ||
--build-arg KERNEL_VERSION=${KERNEL_VERSION} \ | ||
--build-arg BPFTRACE_VERSION=${BPFTRACE_VERSION} \ | ||
--build-arg PYTHON_VERSION=${PYTHON_VERSION} \ | ||
-t secimport:${KERNEL_VERSION} . | ||
|
||
popd | ||
|
||
echo "You can now use the ./run.sh script to try secimport." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
ARG KERNEL_VERSION | ||
|
||
FROM linuxkit/kernel:${KERNEL_VERSION} as ksrc | ||
FROM ubuntu:20.04 AS build | ||
|
||
ARG BPFTRACE_VERSION | ||
ARG PYTHON_VERSION | ||
|
||
WORKDIR /kernel | ||
COPY --from=ksrc /kernel-dev.tar . | ||
RUN tar xf kernel-dev.tar | ||
|
||
WORKDIR /workspace | ||
ARG DEBIAN_FRONTEND=noninteractive | ||
|
||
# TODO: add openssl (longer build time, but pip will work for our interpreter) | ||
RUN echo "Installing prerequisites" && \ | ||
apt-get update && apt-get install sudo build-essential libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev curl wget auditd vim tmux git binutils unzip gcc systemtap-sdt-dev cmake zlib1g-dev -y | ||
RUN echo "Installing python with dtrace" && \ | ||
curl -o Python-${PYTHON_VERSION}.tgz https://www.python.org/ftp/python/${PYTHON_VERSION}/Python-${PYTHON_VERSION}.tgz && tar -xzf Python-${PYTHON_VERSION}.tgz && \ | ||
cd Python-${PYTHON_VERSION} && ./configure --with-dtrace --prefix=/usr/local/openssl --prefix=$(pwd) --with-ensurepip=install && make && make install | ||
RUN echo "Installing bpftrace" && \ | ||
wget https://github.com/iovisor/bpftrace/releases/download/${BPFTRACE_VERSION}/bpftrace && \ | ||
chmod +x bpftrace && \ | ||
mv bpftrace /bin && \ | ||
wget https://github.com/iovisor/bpftrace/archive/${BPFTRACE_VERSION}.zip && \ | ||
unzip ${BPFTRACE_VERSION}.zip && \ | ||
cp -r bpftrace*/tools /workspace/bpftrace/ && \ | ||
echo "Done building bpftrace" && \ | ||
mv /kernel/usr/src/linux-headers* /kernel/usr/src/linux-headers | ||
|
||
ENV BPFTRACE_KERNEL_SOURCE=/kernel/usr/src/linux-headers | ||
COPY setup.sh . | ||
COPY sandbox.bt . | ||
COPY run_sandbox.sh . | ||
RUN chmod 755 sandbox.bt run_sandbox.sh | ||
|
||
ENTRYPOINT ["/bin/sh", "/workspace/setup.sh"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/usr/bin/env bash | ||
|
||
# "--unsafe" is required to run system command for remediation. | ||
# If process termination on violoation is not needed, | ||
# You can remove this argument. | ||
|
||
echo "Starting secimport sandbox with bpftrace backend, the sandbox should kill the python process..." | ||
bpftrace -c "/workspace/Python-3.10.0/python -c __import__('os').system('ps')" -o sandbox.log sandbox.bt --unsafe || echo "The process was killed, as expected." | ||
echo "The sandbox bpftrace code is at sandbox.bt" | ||
echo "The sandbox log is at sandbox.log" | ||
# tail -n 20 sandbox.log | ||
# less +G sandbox.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#!/usr/bin/env bpftrace | ||
|
||
BEGIN { | ||
printf("STARTED\n") | ||
} | ||
|
||
|
||
usdt:/workspace/Python-3.10.0/python:function__entry { | ||
@["depth"]++; | ||
@entrypoints[str(arg0)] = @["depth"]; | ||
@globals["previous_module"] = @globals["current_module"]; | ||
@globals["current_module"] = str(arg0); | ||
printf("%s, %s, depth=%d\n", str(arg0), str(arg1), @["depth"]) ; | ||
} | ||
|
||
usdt:/workspace/Python-3.10.0/python:function__return { | ||
@["depth"]--; | ||
} | ||
|
||
tracepoint:raw_syscalls:sys_enter /comm == "python"/ { | ||
if(args->id == 59){ | ||
printf("KILLING PROCESS %s - EXECUTED execve;\n", str(pid)); | ||
system("pkill -9 args"); // optional | ||
printf("Killed process %s", str(pid)); | ||
exit(); // optional | ||
} | ||
printf("%s SYSCALL %ld depth=%d previous=%s current=%s \n", probe, args->id, @["depth"], @globals["previous_module"], @globals["current_module"] ); | ||
} | ||
|
||
END { | ||
clear(@entrypoints); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/bin/sh | ||
mount -t debugfs none /sys/kernel/debug/ | ||
sysctl -w kernel.kptr_restrict=0 >/dev/null 2>&1 | ||
sysctl -w kernel.perf_event_paranoid=2 >/dev/null 2>&1 | ||
cd /workspace/ | ||
/bin/bash | ||
# /bin/bash ./run_sandbox.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/bin/bash | ||
|
||
if [[ "$PWD" =~ docker$ ]] | ||
then | ||
echo "Running secimport docker container..."; | ||
else | ||
echo "Please run this script from the secimport/docker directory."; | ||
exit 1; | ||
fi | ||
|
||
KERNEL_VERSION=`docker run --rm -it alpine uname -r | cut -d'-' -f1` | ||
|
||
cd .. # back to repo root dir | ||
docker run --rm --name=secimport --privileged -v "$(pwd)/src/secimport":"/workspace/secimport/" -it secimport:${KERNEL_VERSION} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/usr/bin/env bash | ||
|
||
# "--unsafe" is required to run system command for remediation. | ||
# If process termination on violoation is not needed, | ||
# You can remove this argument. | ||
|
||
echo "Starting secimport sandbox with python shell..." | ||
bpftrace -c "/workspace/Python-3.10.0/python -c __import__('os').system('ps')" -o sandbox.log sandbox.bt --unsafe | ||
|
||
# The process is killed becused we ran os.system inside our sandbox. | ||
# Watch the logs: | ||
less +G sandbox.log | ||
# OR: | ||
# tail -n 20 sandbox.log | ||
echo "The sandbox log is at ./sandbox.log" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Oops, something went wrong.