Getting Results of a System-call
Intercepting a SYSCALL and SYSRET result
One of the exciting features of HyperDbg is its ability to trace and trigger events in the case of both SYSCALLs and SYSRETs. It's a straightforward task to intercept, monitor, or change syscall parameters; however, it's a bit challenging when it comes to SYSRETs.
In this example, we're going to create an automated script to intercept both system calls and then intercept the result of the system call when it returns from the kernel to the user mode.
Based on our Windows internals knowledge, we know that a thread might be either in user-mode or in the kernel mode, so if we intercept a thread turns to the kernel, by executing the SYSCALL instruction, we can expect that the target thread will perform an SYSRET after finishing its execution in the kernel.
It's clear that if we save the Thread Id of our target thread from the syscall event, we can check it later in the SYSRET event.
There is another problem here. Two threads might simultaneously trigger the same syscall; thus, we need to synchronize somehow. To overcome this issue, we use spinlocks.
First, we create two global variables, one to hold the spinlocks' lock, and the second global variable stores the Thread Id that executed our target system call.
1
? .thread_id = 0;
2
? .thread_id_lock = 0;
Copied!
Next, in the SYSRET event, we check whether the current thread is stored at the .thread_id global variable or not. If it is stored, then it's the thread that we want to see its results, and if not, there is no interest for interception. At last, we set the .thread_id to 0 to indicate that we finish investigating this thread.
1
!sysret script {
2
3
if (.thread_id == $tid) {
4
5
printf("[%llx] result of syscall: %llx\n", $tid, @rax);
6
7
//
8
// Reset the thread id holder
9
//
10
.thread_id = 0;
11
}
12
}
Copied!
As the next step, we check for the Process Id and the syscall number. If the process is our target process and the system call number stored at the RAX register is our target system call, then we keep the Thread Id into the .thread_id variable.
There might be two or more threads that execute the system call. We only intercept one of them and ignore another one if both of them execute the same syscall at once. The check is performed in the critical section between spinlocks to avoid two threads serving the same check simultaneously.
1
!syscall script {
2
3
if ($pid == $arg1 && @rax == $arg2) {
4
5
spinlock_lock(&.thread_id_lock);
6
7
if (.thread_id == 0) {
8
9
//
10
// Save the thread id for the SYSRET event
11
//
12
.thread_id = $tid;
13
14
//
15
// Show the parameters
16
//
17
printf("[%llx] syscall num: %llx, arg1: %llx, arg2: %llx, arg3: %llx, arg4: %llx\n", $tid, @rax, @rcx, @rdx, @r8, @r9);
18
19
}
20
21
spinlock_unlock(&.thread_id_lock);
22
}
23
}
Copied!
As you might have noticed, we run the SYSRET event before running the syscall event. The reason for it came from the fact that if we set the SYSCALL event before the SYSRET event, a syscall might set the .thread_id to a Thread Id. As we're not configuring the SYSRET event yet, the thread might finish its execution (run SYSRET) before configuring the SYSCALL event, and thus, it never works properly.
All in all, putting everything together makes the following script, we'll create a file named c:\users\sina\desktop\script.ds.
1
? .thread_id = 0;
2
? .thread_id_lock = 0;
3
4
!sysret script {
5
6
if (.thread_id == $tid) {
7
8
printf("[%llx] result of syscall: %llx\n", $tid, @rax);
9
10
//
11
// Reset the thread id holder
12
//
13
.thread_id = 0;
14
}
15
}
16
17
!syscall script {
18
19
if ($pid == $arg1 && @rax == $arg2) {
20
21
spinlock_lock(&.thread_id_lock);
22
23
if (.thread_id == 0) {
24
25
//
26
// Save the thread id for the SYSRET event
27
//
28
.thread_id = $tid;
29
30
//
31
// Show the parameters
32
//
33
printf("[%llx] syscall num: %llx, arg1: %llx, arg2: %llx, arg3: %llx, arg4: %llx\n", $tid, @rax, @rcx, @rdx, @r8, @r9);
34
35
}
36
37
spinlock_unlock(&.thread_id_lock);
38
}
39
}
Copied!
After that, we run our script by using the below command.
1
? .script c:\users\sina\desktop\script.ds 1240 55
Copied!
You can see the results in the following picture.
Copy link
Edit on GitHub