You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have been trying to hook this function for a while and can not get anything to compile. I was able to hook kill_pid, however, that did not hook the kill syscall.
OK, it makes sense then. From some point they changed syscalls notation and argument passing. So, now you have to look for __x64_sys_kill with long (*)(const struct pt_regs *) proto instead of sys_kill. The code may look like this (didn't test):
But I can't figure out where you found the prototype for the updated sys_kill function. As far as I can tell, it's not in the source code. But I also know that can't be right, so I'm a little stuck. Google has turned up nothing so far.
You could ether hook ksys_msgctl(int, int, struct msqid_ds *) as a kernel function (in case if it is exported as a ksym with name -- check by grepping /proc/kallsyms) or hook __x64_sys_msgctl/sys_msgctl as a system call handler.
hi,
I am trying to hook sys_open and some other syscalls to intercept the file system activity, on kernel 4.17/4.18 on x86_64.
/proc/kallsyms shows __x64_sys_open, but not ksys_open.
Not sure if I need to hook ksys_open or __x64_sys_open
I am getting compilation error of 'undelcared identifier' for sys_open & __x64_sys_open.
What are the right headers I need to include ?
Along with sys_open, I need to hook sys_creat, sys_openat, sys_execve, sys_truncate, sys_ftruncate, sys_write, etc...
Before kernel 4.17, I was able to find the symbol for sys_xyz, but from Kernel 4.17 onward, I am not sure if I need to hook __x64_sys_xyz or ksys_xyz or sys_xyz.
In syscalls.h Some system call are declared with /__ARCH_WANT_SYSCALL_DEPRECATED/
If I were to use hook in a traditional way of finding a specific syscall table address and hook it,
which specific syscall API to hook
sys_open or __x64_sys_open or ksys_open
sys_open or __x64_sys_creat or ksys_creat
sys_openat or __x64_sys_openat or ksys_openat
sys_execve or __x64_sys_exeeve or ksys_execve
etc ...
and what are the header files __x64_sys_creat or __x64_sys_openat are declared ?
It will be great if someone can share a sample with what is needed to hook syscalls on kernels from 4.17 onward.
Dear @KTalinki, you don't need to have headers to hook __x64_sys_XXX as they all have the same prototype (like I shown before). As for the ksys_xxx() symbols I'd suggest you to look for the Linux kernel source code to see how they are used. Finally, you're the kernel developer and you have to be able to dig the source code.
thank you @milabs , the way our existing code is structured, it uses the declaration of the syscalls from .../include/linux/syscalls.h
But on kernel 4.17 with __x64_sys_open, is not declared in <syscalls.h>
And <arch/x85/include/generated/asm/syscalls_64.h> has the declaration something like
__SYSCALL_64(2, __X64_sys_open,)
The issue I am running in to is compilation errors for __x64_sys_open and __SYSCALL_64.
Including <arch/x85/include/generated/asm/syscalls_64.h> is not helping.
@milabs hello,I want to hook sys_exevce. I'm using the KHOOK_EXT macro,which work well on the 64-bit machines,for example Redhat6.8 x86_64 or CentOS8 x86_64.The code is as follows:
Linux Redhat6.8 2.6.32-642.el6.x86_64:
Activity
milabs commentedon Apr 8, 2019
Hey, thank you for using KHOOK.
As for hooking system calls you have to hook
sys_XXXsymbols likesys_kill. See the example below:Just use the right prototype for this
sys_kill...alichtman commentedon Apr 8, 2019
Oh, whoops. My brain was not working correctly last night. Thanks for the help!
However, the example did not work for me. When I run
$ kill <some_pid>in bash, this debug statement isn't printed.I tried changing the first parameter to a
pid_ttype, and that did not solve the problem either.I'm pretty sure that
$ killcallssys_kill(), but I'm not sure if that's true.milabs commentedon Apr 8, 2019
It's weird. I've tried to
kill -9 1and the dmesg shows me:[ 4630.052329] sys_kill -- bash pid 1 sig 9By the way, my kernel is
4.8.0-53-genericalichtman commentedon Apr 8, 2019
I'm working on
4.18.0-17-generic. Still not seeing the same behavior you're describing. Hmm.milabs commentedon Apr 9, 2019
OK, it makes sense then. From some point they changed syscalls notation and argument passing. So, now you have to look for
__x64_sys_killwithlong (*)(const struct pt_regs *)proto instead ofsys_kill. The code may look like this (didn't test):alichtman commentedon Apr 10, 2019
Oh, alright, cool! I'll try this out when I have a minute.
alichtman commentedon Apr 13, 2019
Worked perfectly. Thanks for the help!
milabs commentedon Apr 15, 2019
Updated the README with this example. Thank you for the question.
alichtman commentedon Apr 16, 2019
Can you explain where you found this new function signature?
I'm having trouble reproducing this with other syscalls.
For example, I have this hook for
msgctl, and it's not being tripped.Here is a relevant LKML email: https://lore.kernel.org/lkml/tip-d5a00528b58cdb2c71206e18bd021e34c4eab878@git.kernel.org/
But I can't figure out where you found the prototype for the updated sys_kill function. As far as I can tell, it's not in the source code. But I also know that can't be right, so I'm a little stuck. Google has turned up nothing so far.
Edit: I found this: https://github.com/torvalds/linux/blob/618d919cae2fcaadc752f27ddac8b939da8b441a/arch/x86/include/asm/syscall_wrapper.h#L125
and updated my code to:
However, this still does not hook
msgctlcalls.milabs commentedon Apr 16, 2019
TLDR:
__x64_ksys_msgctl->__x64_sys_msgctlhttps://elixir.bootlin.com/linux/latest/source/ipc/msg.c#L614
You could ether hook
ksys_msgctl(int, int, struct msqid_ds *)as a kernel function (in case if it is exported as a ksym with name -- check by grepping /proc/kallsyms) or hook__x64_sys_msgctl/sys_msgctlas a system call handler.alichtman commentedon Apr 16, 2019
ksys_msgctldoes appear in/proc/kallsyms, and I've made this change:__x64_ksys_msgctl -> __x64_sys_msgctl.It's still not being hooked properly.
The syscall definition you pasted in was the reason I initially had
sys_msgctlhooked like this:Should I change that ^ to
__x64_sys_msgctl?I feel like I’m missing some core understanding of what I’m doing. Do you have any recommendations for topics I should read up on?
milabs commentedon Apr 16, 2019
YES (but I didn't test it)
milabs commentedon Apr 16, 2019
This works well for me:
main.c$ dmesg
[108112.452874] a.out -- msqid:100 cmd:200 ptr:000000008b1f8f3d1 remaining item
KTalinki commentedon May 3, 2019
hi,
I am trying to hook sys_open and some other syscalls to intercept the file system activity, on kernel 4.17/4.18 on x86_64.
/proc/kallsyms shows __x64_sys_open, but not ksys_open.
Not sure if I need to hook ksys_open or __x64_sys_open
I am getting compilation error of 'undelcared identifier' for sys_open & __x64_sys_open.
What are the right headers I need to include ?
Along with sys_open, I need to hook sys_creat, sys_openat, sys_execve, sys_truncate, sys_ftruncate, sys_write, etc...
Before kernel 4.17, I was able to find the symbol for sys_xyz, but from Kernel 4.17 onward, I am not sure if I need to hook __x64_sys_xyz or ksys_xyz or sys_xyz.
In syscalls.h Some system call are declared with /__ARCH_WANT_SYSCALL_DEPRECATED/
Please help,
thank you,
Kumar T
milabs commentedon May 3, 2019
@KTalinki Use the following code for every "modern" syscall handler (replace
XXXwithopenor so):KTalinki commentedon May 3, 2019
Thank you milabs.
If I were to use hook in a traditional way of finding a specific syscall table address and hook it,
which specific syscall API to hook
sys_open or __x64_sys_open or ksys_open
sys_open or __x64_sys_creat or ksys_creat
sys_openat or __x64_sys_openat or ksys_openat
sys_execve or __x64_sys_exeeve or ksys_execve
etc ...
and what are the header files __x64_sys_creat or __x64_sys_openat are declared ?
It will be great if someone can share a sample with what is needed to hook syscalls on kernels from 4.17 onward.
thank you,
Kumar T
milabs commentedon May 3, 2019
Dear @KTalinki, you don't need to have headers to hook
__x64_sys_XXXas they all have the same prototype (like I shown before). As for theksys_xxx()symbols I'd suggest you to look for the Linux kernel source code to see how they are used. Finally, you're the kernel developer and you have to be able to dig the source code.KTalinki commentedon May 7, 2019
thank you @milabs , the way our existing code is structured, it uses the declaration of the syscalls from .../include/linux/syscalls.h
But on kernel 4.17 with __x64_sys_open, is not declared in <syscalls.h>
And <arch/x85/include/generated/asm/syscalls_64.h> has the declaration something like
__SYSCALL_64(2, __X64_sys_open,)
The issue I am running in to is compilation errors for __x64_sys_open and __SYSCALL_64.
Including <arch/x85/include/generated/asm/syscalls_64.h> is not helping.
Any help is appreciated,
thank you,
Kumar
milabs commentedon May 8, 2019
@KTalinki Again, you don't have to include ANY header to hook __x64_XXX, just use the
KHOOK_EXTmacro:Which is the error with using this code?
wbt165 commentedon May 12, 2020
@milabs hello,I want to hook sys_exevce. I'm using the KHOOK_EXT macro,which work well on the 64-bit machines,for example Redhat6.8 x86_64 or CentOS8 x86_64.The code is as follows:
Linux Redhat6.8 2.6.32-642.el6.x86_64:
CentOS8 x86_64:
BUT,I have a problem on the 32-bit machine. I test it on Linux Redhat 6.8 2.6.32-642.el6.i686.The code is as follows:
I Found the sys_execve() symbols from the source code as follow:

After I insmod, I execute "ls" or any other commands. ERROR as following:

I suspect there is a problem with this code executing on a 32-bit machine.I hope you can take a look in your busy schedule.
Thanks for your help!
milabs commentedon May 12, 2020
@wbt165 I'd recommend you to hook
do_execveinstead ofsys_execvefor exec case. Could you try to hook it that way?wbt165 commentedon May 13, 2020
@milabs Thanks for your response!
I test
do_execve,and the results as follows:do_execvework well on Linux Redhat6.8 2.6.32-642.el6.x86_64 ]do_execveCANNOT hook on CentOS8 4.18.0-147.8.1.el8_1.x86_64, because the debug info is not printed out]do_execveCANNOT work on Linux Redhat6.8 2.6.32-642.el6.i686]After I insmod, I execute "ls" or any other commands. ERROR as following:


The debug info is as follow:
Thanks for your help!
milabs commentedon May 15, 2020
@wbt165 Unfortunately, 32-bit stub is not implemented as I never had such requirement for myself (see https://github.com/milabs/khook/blob/master/khook/x86/stub.S#L24). It would be great if you'll try to implement missing part of the macro.
milabs commentedon May 15, 2020
If you could test that code on 32-bit system?
wbt165 commentedon May 18, 2020
@milabs Unfortunately,this code has NO effect……
milabs commentedon May 18, 2020
Made a branch for you to test:
https://github.com/milabs/khook/tree/i686-fix
Could you please test it?
wbt165 commentedon May 19, 2020
@milabs Thank you for creating a branch! I tested it on Linux Redhat6.8 2.6.32-642.el6.i686, but it didn't work. The code is as follow:
After I insmod, I execute "ls" or any other commands. ERROR as following:


The debug info is as follow: