-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathe9syscall
executable file
·121 lines (106 loc) · 3.15 KB
/
e9syscall
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/bin/bash
#
# Copyright (C) 2023 National University of Singapore
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
if [ -t 1 ]
then
RED="\033[31m"
GREEN="\033[32m"
YELLOW="\033[33m"
BOLD="\033[1m"
OFF="\033[0m"
else
RED=
GREEN=
YELLOW=
BOLD=
OFF=
fi
usage()
{
echo -e "${RED}usage${OFF}: $0 [OPTIONS] hook.c" >&2
echo >&2
echo "OPTIONS:" >&2
echo " -l FILE" >&2
echo " Use FILE as the libc.so [default: /lib/x86_64-linux-gnu/libc.so.6]" >&2
echo >&2
exit 1
}
LIBC=/lib/x86_64-linux-gnu/libc.so.6
while getopts "l:" OPTION
do
case "$OPTION"
in
l)
LIBC=$OPTARG
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
if [ $# != 1 ]
then
usage
fi
HOOK=$1
HOOKNAME=`basename "$HOOK" .c`
set -e
# STEP (1): check if e9patch is installed:
if [ ! -x ./e9patch ]
then
echo -e "${RED}error${OFF}: e9patch is not installed (run ./build.sh first) " >&2
fi
# STEP (2): build the hook code:
echo -e "${GREEN}$0${OFF}: building hook ($HOOK)..."
./e9compile.sh e9syscall-rt.c -O2 -Werror -include "$HOOK" -I "$PWD"
mv e9syscall-rt "e9syscall_hook_$HOOKNAME"
# STEP (3): patch libc:
echo -e "${GREEN}$0${OFF}: patching libc ($LIBC)..."
echo "./e9tool -M 'asm=\"syscall\"' -P 'if intercept(&rax,rdi,rsi,rdx,r10,r8,r9)@'\"e9syscall_hook_$HOOKNAME break\" "$LIBC" -o "libc-$HOOKNAME.so""
./e9tool \
-M 'asm="syscall"' \
-P 'if intercept(&rax,rdi,rsi,rdx,r10,r8,r9)@'"e9syscall_hook_$HOOKNAME break" \
"$LIBC" -o "libc-$HOOKNAME.so" | tee "e9patch-$HOOKNAME.log"
echo -e "${GREEN}$0${OFF}: done!"
echo
echo -e "${YELLOW} ___ _ _ "
echo -e " ___ / _ \\ ___ _ _ ___ ___ __ _| | |"
echo -e " / _ \\ (_) / __| | | / __|/ __/ _\` | | |"
echo -e " | __/\\__, \\__ \\ |_| \\__ \\ (_| (_| | | |"
echo -e " \\___| /_/|___/\\__, |___/\\___\\__,_|_|_|"
echo -e " |___/${OFF}"
echo
if grep -e 'num_patched .*(100.00%)' "e9patch-$HOOKNAME.log" > /dev/null
then
echo -e "${GREEN}SUCCESS${OFF}!"
echo
echo -e "Successfully built the patched library ${YELLOW}libc-$HOOKNAME.so${OFF}."
else
echo -e "${RED}WARNING${OFF}!"
echo
echo -e "Built the patched library ${YELLOW}libc-$HOOKNAME.so${OFF}, however 100% coverage was not"
echo "achieved. This means that some system calls will not be intercepted."
fi
echo
echo "To use, run the following command:"
echo
echo " LD_PRELOAD=\"$PWD/libc-$HOOKNAME.so\" command ..."
echo
echo "For example:"
echo
echo " LD_PRELOAD=\"$PWD/libc-$HOOKNAME.so\" ls -l"
echo