-
Notifications
You must be signed in to change notification settings - Fork 431
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests(event): add hooked_syscall instrumentation test
- Loading branch information
1 parent
f5f2d01
commit 3c8251d
Showing
10 changed files
with
245 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,6 +59,7 @@ env: | |
DNS | ||
HTTP | ||
INSTTESTS: > | ||
HOOKED_SYSCALL | ||
VFS_WRITE | ||
FILE_MODIFICATION | ||
SECURITY_INODE_RENAME | ||
|
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,74 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/aquasecurity/tracee/signatures/helpers" | ||
"github.com/aquasecurity/tracee/types/detect" | ||
"github.com/aquasecurity/tracee/types/protocol" | ||
"github.com/aquasecurity/tracee/types/trace" | ||
) | ||
|
||
type e2eHookedSyscall struct { | ||
cb detect.SignatureHandler | ||
} | ||
|
||
func (sig *e2eHookedSyscall) Init(ctx detect.SignatureContext) error { | ||
sig.cb = ctx.Callback | ||
return nil | ||
} | ||
|
||
func (sig *e2eHookedSyscall) GetMetadata() (detect.SignatureMetadata, error) { | ||
return detect.SignatureMetadata{ | ||
ID: "HOOKED_SYSCALL", | ||
EventName: "HOOKED_SYSCALL", | ||
Version: "0.1.0", | ||
Name: "Hooked Syscall Test", | ||
Description: "Instrumentation events E2E Tests: Hooked Syscall", | ||
Tags: []string{"e2e", "instrumentation"}, | ||
}, nil | ||
} | ||
|
||
func (sig *e2eHookedSyscall) GetSelectedEvents() ([]detect.SignatureEventSelector, error) { | ||
return []detect.SignatureEventSelector{ | ||
{Source: "tracee", Name: "hooked_syscall"}, | ||
}, nil | ||
} | ||
|
||
func (sig *e2eHookedSyscall) OnEvent(event protocol.Event) error { | ||
eventObj, ok := event.Payload.(trace.Event) | ||
if !ok { | ||
return fmt.Errorf("failed to cast event's payload") | ||
} | ||
|
||
switch eventObj.EventName { | ||
case "hooked_syscall": | ||
syscall, err := helpers.GetTraceeStringArgumentByName(eventObj, "syscall") | ||
if err != nil { | ||
return err | ||
} | ||
owner, err := helpers.GetTraceeStringArgumentByName(eventObj, "owner") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if syscall == "uname" && owner == "hijack" { | ||
m, _ := sig.GetMetadata() | ||
sig.cb( | ||
detect.Finding{ | ||
SigMetadata: m, | ||
Event: event, | ||
Data: map[string]interface{}{}, | ||
}, | ||
) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (sig *e2eHookedSyscall) OnSignal(s detect.Signal) error { | ||
return nil | ||
} | ||
|
||
func (sig *e2eHookedSyscall) Close() {} |
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,6 @@ | ||
* | ||
!.gitignore | ||
!Makefile | ||
!hijack.c | ||
!load.sh | ||
!unload.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,13 @@ | ||
obj-m += hijack.o | ||
|
||
PWD := $(shell pwd) | ||
|
||
KBUILD_CFLAGS += -g -Wall | ||
KERNELDIR ?= /lib/modules/$(shell uname -r)/build | ||
|
||
hijack.o: | ||
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules | ||
|
||
clean: | ||
rm -f hijack.mod hijack.o hijack.mod.c hijack.mod.o hijack.ko | ||
rm -f modules.order Module.symvers |
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,56 @@ | ||
#include <linux/init.h> | ||
#include <linux/module.h> | ||
#include <linux/syscalls.h> | ||
#include <linux/utsname.h> | ||
|
||
// Example of a kernel module hijacking a system call. | ||
|
||
MODULE_LICENSE("GPL"); | ||
|
||
ulong table; | ||
module_param(table, ulong, 0); | ||
|
||
asmlinkage u64 (*orig_uname)(struct old_utsname *); | ||
|
||
asmlinkage u64 hooked_uname(struct old_utsname *name) | ||
{ | ||
printk(KERN_INFO "uname() intercepted!\n"); | ||
return orig_uname(name); | ||
} | ||
|
||
#define RO 0 | ||
#define RW 1 | ||
|
||
static int set_page(u64 addr, int flag) | ||
{ | ||
u32 level; | ||
pte_t *pte = lookup_address(addr, &level); | ||
|
||
if (pte && pte_present(*pte)) | ||
pte->pte = flag ? pte->pte | _PAGE_RW : pte->pte & ~_PAGE_RW; | ||
|
||
return 0; | ||
} | ||
|
||
static int __init hijack_init(void) | ||
{ | ||
if (!table) | ||
return -EINVAL; | ||
|
||
set_page(table, RW); | ||
orig_uname = (void *) ((u64 **) table)[__NR_uname]; | ||
((u64 **) table)[__NR_uname] = (u64 *) hooked_uname; | ||
set_page(table, RO); | ||
|
||
return 0; | ||
} | ||
|
||
static void __exit hijack_exit(void) | ||
{ | ||
set_page(table, RW); | ||
((u64 **) table)[__NR_uname] = (u64 *) orig_uname; | ||
set_page(table, RO); | ||
} | ||
|
||
module_init(hijack_init); | ||
module_exit(hijack_exit); |
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 @@ | ||
#!/bin/bash | ||
|
||
if [[ $UID -ne 0 ]]; then | ||
echo must be root | ||
exit 1 | ||
fi | ||
|
||
sudo lsmod | grep -q hijack && { | ||
echo module already loaded | ||
exit 0 | ||
} | ||
|
||
address=$(cat /proc/kallsyms | grep -E " sys_call_table$" | cut -d' ' -f1) | ||
arg="./hijack.ko table=0x$address" | ||
modprobe $arg || insmod $arg |
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,8 @@ | ||
#!/bin/bash | ||
|
||
if [[ $UID -ne 0 ]]; then | ||
echo must be root | ||
exit 1 | ||
fi | ||
|
||
rmmod hijack |
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/bash -e | ||
|
||
exit_err() { | ||
echo -n "ERROR: " | ||
echo "$@" | ||
exit 1 | ||
} | ||
|
||
# Build and load module | ||
dir="tests/e2e-inst-signatures/scripts/hijack" | ||
cd $dir || exit_err "could not cd to $dir" | ||
make && ./load.sh || exit_err "could not load module" | ||
|
||
# Unload module after 30 seconds | ||
nohup sleep 30 > /dev/null 2>&1 && ./unload.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