2.2.XP系统中系统调用的内核入口KiSystemService函数的实现

windows系统调用int 2e处理过程 在X86体系架构中,中断处理向量由一个中断描述符表保存。中断描述符表的每个描述符项包括一个中断处理函数的地址、一个32位的描述符属性和中断之后中断所在代码的特权级别。下面是一个描述符项的宏定义,每个处理器包含256个这样的宏定义用于组成一个数组变量kidt。在系统初始化时,kidt当中的元素经过一些处理之后会填充到IDT表项当中。其中,当bits被定义为INT_32_DPL0时,表明发生的是异常; 阅读详情

2.2.XP系统中系统调用的内核入口KiSystemService函数的实现

2.2.XP系统中系统调用的内核入口KiSystemService函数的实现


_KiSystemService

改代码是汇编,xp的核心代码C写的,部分汇编写的。

_KiSystemService        proc

        ENTER_SYSCALL   kss_a, kss_t    ; set up trap frame and save state

?FpoValue = 0

;
; (eax) = Service number
; (edx) = Callers stack pointer
; (esi) = Current thread address
;
; All other registers have been saved and are free.
;
; Check if the service number within valid range
;

_KiSystemServiceRepeat:
        mov     edi, eax                ; copy system service number
        shr     edi, SERVICE_TABLE_SHIFT ; isolate service table number
        and     edi, SERVICE_TABLE_MASK ;
        mov     ecx, edi                ; save service table number
        add     edi, [esi]+ThServiceTable ; compute service descriptor address
        mov     ebx, eax                ; save system service number
        and     eax, SERVICE_NUMBER_MASK ; isolate service table offset

;
; If the specified system service number is not within range, then attempt
; to convert the thread to a GUI thread and retry the service dispatch.
;

        cmp     eax, [edi]+SdLimit      ; check if valid service
        jae     Kss_ErrorHandler        ; if ae, try to convert to GUI thread

;
; If the service is a GUI service and the GDI user batch queue is not empty,
; then call the appropriate service to flush the user batch.
;

        cmp     ecx, SERVICE_TABLE_TEST ; test if GUI service
        jne     short Kss40             ; if ne, not GUI service
        mov     ecx, PCR[PcTeb]         ; get current thread TEB address
        xor     ebx, ebx                ; get number of batched GDI calls

KiSystemServiceAccessTeb:
        or      ebx, [ecx]+TbGdiBatchCount ; may cause an inpage exception

        jz      short Kss40             ; if z, no batched calls
        push    edx                     ; save address of user arguments
        push    eax                     ; save service number
        call    [_KeGdiFlushUserBatch]  ; flush GDI user batch
        pop     eax                     ; restore service number
        pop     edx                     ; restore address of user arguments

;
; The arguments are passed on the stack. Therefore they always need to get
; copied since additional space has been allocated on the stack for the
; machine state frame.  Note that we don't check for the zero argument case -
; copy is always done regardless of the number of arguments because the
; zero argument case is very rare.
;

Kss40:  inc     dword ptr PCR[PcPrcbData+PbSystemCalls] ; system calls

if DBG
        mov     ecx, [edi]+SdCount      ; get count table address
        jecxz   Kss45                   ; if zero, table not specified
        inc     dword ptr [ecx+eax*4]   ; increment service count
Kss45:  push    dword ptr [esi]+ThApcStateIndex ; (ebp-4)
        push    dword ptr [esi]+ThKernelApcDisable ; (ebp-8)

        ;
        ; work around errata 19 which can in some cases cause an
        ; extra dword to be moved in the rep movsd below. In the DBG
        ; build, this will usually case a bugcheck 1 where ebp-8 is no longer
        ; the kernel apc disable count
        ;

        sub     esp,4


?FpoValue = ?FpoValue+3
endif

FPOFRAME ?FpoValue, 0

        mov     esi, edx                ; (esi)->User arguments
        mov     ebx, [edi]+SdNumber     ; get argument table address
        xor     ecx, ecx
        mov     cl, byte ptr [ebx+eax]  ; (ecx) = argument size
        mov     edi, [edi]+SdBase       ; get service table address
        mov     ebx, [edi+eax*4]        ; (ebx)-> service routine
        sub     esp, ecx                ; allocate space for arguments
        shr     ecx, 2                  ; (ecx) = number of argument DWORDs
        mov     edi, esp                ; (es:edi)->location to receive 1st arg
        cmp     esi, _MmUserProbeAddress ; check if user address
        jae     kss80                   ; if ae, then not user address

KiSystemServiceCopyArguments:
        rep     movsd                   ; copy the arguments to top of stack.
                                        ; Since we usually copy more than 3
                                        ; arguments.  rep movsd is faster than
                                        ; mov instructions.
if DBG
;
; Check for user mode call into system at elevated IRQL.
;

        test    byte ptr [ebp]+TsSegCs,MODE_MASK
        jz      short kss50a                  ; kernel mode, skip test
        stdCall _KeGetCurrentIrql
        or      al, al                  ; bogus irql, go bugcheck
        jnz     kss100
kss50a:

        test    _MmInjectUserInpageErrors, 2
        jz      short @f
        stdCall _MmTrimProcessMemory, <0>
        jmp     short kssdoit
@@:

        mov     eax,PCR[PcPrcbData+PbCurrentThread]
        mov     eax,[eax]+ThApcState+AsProcess
        test    dword ptr [eax]+PrFlags,0100000h ; is this a inpage-err process?
        je      short @f
        stdCall _MmTrimProcessMemory, <0>
@@:
endif

;
; Make actual call to system service
;
kssdoit:
        CAPSTARTX <_KiSystemService,ebx>
        call    ebx                     ; call system service
        CAPENDX <_KiSystemService>

kss60:

if DBG
        mov     ecx,PCR[PcPrcbData+PbCurrentThread] ; (ecx)-> Current Thread

;
; Check for return to user mode at elevated IRQL.
;
        test    byte ptr [ebp]+TsSegCs,MODE_MASK
        jz      short kss50b
        mov     esi, eax
        stdCall _KeGetCurrentIrql
        or      al, al
        jnz     kss100                  ; bogus irql, go bugcheck
        mov     eax, esi
kss50b:

;
; Check that APC state has not changed
;
        mov     edx, [ebp-4]
        cmp     dl, [ecx]+ThApcStateIndex
        jne     kss120

        mov     edx, [ebp-8]
        cmp     edx, [ecx]+ThKernelApcDisable
        jne     kss120

endif

kss61:

;
; Upon return, (eax)= status code
;

        mov     esp, ebp                ; deallocate stack space for arguments

;
; Restore old trap frame address from the current trap frame.
;

kss70:  mov     ecx, PCR[PcPrcbData+PbCurrentThread] ; get current thread address
        mov     edx, [ebp].TsEdx        ; restore previous trap frame address
        mov     [ecx].ThTrapFrame, edx  ;

;
;   System service's private version of KiExceptionExit
;   (Also used by KiDebugService)
;
;   Check for pending APC interrupts, if found, dispatch to them
;   (saving eax in frame first).
;
        public  _KiServiceExit
_KiServiceExit:

        cli                                         ; disable interrupts
        DISPATCH_USER_APC   ebp, ReturnCurrentEax

;
; Exit from SystemService
;

        EXIT_ALL    NoRestoreSegs, NoRestoreVolatile

;
; The address of the argument list is not a user address. If the previous mode
; is user, then return an access violation as the status of the system service.
; Otherwise, copy the argument list and execute the system service.
;

kss80:  test    byte ptr [ebp].TsSegCs, MODE_MASK ; test previous mode
        jz      KiSystemServiceCopyArguments ; if z, previous mode kernel
        mov     eax, STATUS_ACCESS_VIOLATION ; set service status
        jmp     kss60                   ;

;++
;
;   _KiServiceExit2 - same as _KiServiceExit BUT the full trap_frame
;       context is restored
;
;--
        public  _KiServiceExit2
_KiServiceExit2:

        cli                             ; disable interrupts
        DISPATCH_USER_APC   ebp

;
; Exit from SystemService
;

        EXIT_ALL                            ; RestoreAll




if DBG
kss100: push    PCR[PcIrql]                 ; put bogus value on stack for dbg
?FpoValue = ?FpoValue + 1
FPOFRAME ?FpoValue, 0
        mov     byte ptr PCR[PcIrql],0      ; avoid recursive trap
        cli

;
; IRQL_GT_ZERO_AT_SYSTEM_SERVICE Returning to usermode at elevated IRQL.
;
; KeBugCheckEx(IRQL_GT_ZERO_AT_SYSTEM_SERVICE,
;              System Call Handler (address of system routine),
;              Irql,
;              0,
;              0,
;              );
;
        stdCall   _KeBugCheckEx,<IRQL_GT_ZERO_AT_SYSTEM_SERVICE, ebx, eax, 0, 0>

;
; APC_INDEX_MISMATCH This is probably caused by the system call entering
; critical regions and not leaving them.   This is fatal.   Include as
; much information in the bugcheck as possible.
;
; KeBugCheckEx(APC_INDEX_MISMATCH,
;              System Call Handler (address of system routine),
;              Thread->ApcStateIndex << 8 | Saved ApcStateIndex,
;              Thread->KernelApcDisable,
;              Saved KernelApcDisable
;              );
;

kss120: mov       eax, [ebp]-4
        mov       ah,  [ecx]+ThApcStateIndex
        stdCall   _KeBugCheckEx,<APC_INDEX_MISMATCH, ebx, eax, [ecx]+ThKernelApcDisable, dword ptr [ebp]-8>

endif
        ret

;
; If the sysenter instruction was executed in 16 bit mode, generate
; an error rather than trying to process the system call.   There is
; no way to return to the correct code in user mode.
;

Kfsc90:
        push    0                           ; save VX86 Es, Ds, Fs, Gs
        push    0
        push    0
        push    0

        push    01bh                        ; save transition CS
        push    0                           ; can't know user esp
        push    EFLAGS_INTERRUPT_MASK+EFLAGS_V86_MASK+2h; eflags with VX86 set
        push    01bh                        ; CS
        push    0                           ; don't know original EIP
        jmp     _KiTrap06                   ; turn exception into illegal op.

_KiSystemService endp

Windows10内核逆向-1-系统调用 Syscall KiSystemCall64的逆向 阅读详情

相关推荐

静态分析KiSystemService

 KiSystemService 是INT 2EH 后调用的 2000所有的native api 都使用INT 2EH, xp下 GDC函数 进入内核调用INT 2EH其他native api 调用KiFastCall.text:00465651 _KiSystemService proc near              ; CODE XREF: ZwAcceptConnectPort(x,x

dalixux的专栏 3662

系统调用002 KiSystemService函数逆向分析

文章目录前言保存现场_KTRAP_FRAMEKRPCExceptionList_KTHREAD结构体先前模式抬高堆栈判断当前权限更新_KTRAP_FRAME保存三环的寄存器环境判断调试状态 前言 之前我们详细了解了API从三环进到零环的过程,API会通过两种方式进入到零环,如果通过中断门的方式进入零环,最终会进入到KiSystemService这个函数。接下来就来分析KiSystemService...

鬼手的博客 2454

滴水逆向系统调用

四、API函数的调用过程(保存现场) 了解KiSystemService需要了解几个结构体 无论是通过中断门还是KifastcallEntry进入0环,三环的所有寄存器都会存到Trap_Frame结构体中 中断门当权限发生变化时,中断门会像0环堆栈中压入五个值,分别是三环的ss,三环的esp,三环的eflag,三环的cs还有三环的eip 中断门进入0环提权会压入这五个值,KiSystem...

若面朝大海边竹妮春暖花开的博客 1131

Windows系统调用学习笔记(四)—— 系统服务表&SSDT

Windows系统调用学习笔记(四)—— 系统服务表前言要点回顾系统服务表实验:分析 KiSystemService 与 KiFastCallEntry 共同代码 前言 一、学习自滴水编程达人中级班课程,官网:https://bcdaren.com 二、海东老师牛逼! 要点回顾 API进入0环后调用的函数: 中断门 – KiSystemService 快速调用 – KiFastCallEnt...

lzyddf的博客 3798

3.逆向分析KiSystemService(填充 _KTRAP_FRAME 部分)

一、回顾 想要分析透彻,必须得了解之前的那些结构体(_KUSER_SHARED_DATA、_Trap_Frame、_KPCR、 _KTHREAD) 没看的同学先看一下之前的文章逆向分析ReadProcessMemory---->KiFastCallEntry 二、分析KiSystemService 由于上一篇分析KiFastCallEntry文章的难度更大,分析_KiSystemService的难度小一些,所以前面步骤省略,直接开始分析KiSystemService .text:00466481 _KiSy

Mikasys的博客 622

LoRa项目实战:手把手教你为ESP32选配和焊接天线(从PCB到信号测试)

本文详细介绍了为ESP32选配和焊接LoRa天线的全流程,包括天线选型、PCB布局设计、焊接工艺及信号测试。重点解析了PCB板载天线、导线天线和陶瓷贴片天线的适用场景与性能特点,并提供专业的焊接技巧和信号测试方案,帮助开发者优化LoRa模块的无线通信性能。

weixin_30470643的博客 394

Windows内核神功葵花宝典之系统调用篇

  用户态进入系统态有以下几种方式:   中断(Interrupt)。在允许中断的情况下,有外部设备的中断请求到达,CPU自动转入系统空间,并从预定地址执行指令。中断只发生在两条指令之间,不会打断正在执行的指令。对CPU而言,进入中断是被动的,所以中断的发生可以看作是“异步”的。   异常(Exception)。不管是用户空间或系统空间,执行指令失败都会引起一次“异常”,CPU因此转入系统空间,并...

flukeshen的博客 1068

利用Python脚本生成IDA Pro许可证文件idapro.hexlic的完整指南

本文提供了一份详细的Python脚本指南,用于自动化生成IDA Pro的许可证文件idapro.hexlic。文章深入解析了脚本原理,包括构建许可证JSON、RSA密钥修补与签名生成机制,并附有完整的操作流程与常见问题排查,帮助逆向工程人员快速完成本地化许可部署。

weixin_29313547的博客 613

系统调用笔记(1)

API函数的调用过程(3环部分) Windows API Application Programming Interface,简称 API 函数。 Windows有多少个API? 主要是存放在 C:\WINDOWS\system32 下面所有的dll,dll里面函数都是API 几个重要的DLL Kernel32.dll:最核心的功能模块,比如管理内存、进程和线程相关的函数等...

kernweak的博客 1782

数据字典实战:如何用Excel快速搭建你的第一个数据字典(附模板下载)

本文针对中小团队在软件开发中面临的术语不一致问题,提供了一套使用Excel快速搭建数据字典的实战指南。文章详细讲解了核心字段设计、填充示例、高阶技巧及团队协作维护流程,旨在通过这一轻量级工具统一团队共识、提升开发效率,并附有实用模板下载。

weixin_29303701的博客 380

34、婴儿监视器应用开发指南

本文详细介绍了基于 ESP32 和 Edge Impulse 开发婴儿监视器应用的全过程,涵盖音频处理、机器学习推理、ESP RainMaker 平台集成以及内存监控等功能。通过实现 AppAudio、AppRmaker 和 AppMem 三个核心类,完成了婴儿哭声的实时检测与远程通知,并通过合理的内存管理优化,有效节省了 ESP32 的内部 RAM 资源。文章还提供了完整的代码示例和配置说明,适用于物联网和嵌入式 AI 应用开发爱好者参考学习。

python9snake的博客 95

[Windows] 系统调用(R3 进入 R0)

背景 一个线程由用户态进入内核态的途径有3种典型的方式: 通过 int 0x2e(软中断自陷) 或 SysEnter (快速系统调用),主动进入内核。 引发异常或硬件中断,被迫进入内核。 通过 int 0x2e 进入内核(xp以下) 一般 R3 的 API 最终都会去调用 NT 函数,在 NT 函数中根据该 API 对应的索引号去 SSDT / SSDT Shadow 中查找对应的方法,最后通过 SSDT / SSDT Shadow 进入内核。 ...

LYSM 2034

YOLOv5入门实践(1)——手把手带你环境配置搭建

YOLOv5入门实践第一步,手把手带你配置环境

路人贾的博客 2万+

理解Windows x64 SSDT与win32k影子服务表:NtCall64系统调用Fuzzer背后的理论基石

NtCall64 是一个面向 Windows x64 的**系统调用 Fuzzer**(syscall fuzzer),它通过模糊测试内核系统调用表——ntoskrnl 的 SSDT(KiServiceTable)和 win32k 的影子服务表(Shadow SSDT)——来挖掘稳定性问题与潜在漏洞。要读懂这个工具为什么"敢直接乱调系统调用",必须先搞懂三件事:SSDT 是什么、win32k 影子

gitblog_00781的博客 889

Redhawk:什么是timing window?

STA timing file中timing window的作用和计算方式

m0_61544122的博客 1万+

【Agent从入门到实践】40 容错优化:异常处理、重试机制、降级策略

各位小伙伴,咱们的多Agent框架一路优化到现在,决策快、执行效率高、资源用得省,看似啥都行,但只要一遇到网络波动、工具故障、数据异常调用web_search工具时网络超时,整个检索任务直接失败,写作Agent没数据可用向量库连接异常,向量检索直接崩掉,依赖检索结果的Agent全趴窝单个Agent执行出错,整个并行任务流程直接中断,前功尽弃高并发下部分工具调用失败,框架没有备用方案,只能返回任务失败说白了,咱们之前的框架只有正常执行逻辑,没有异常兜底逻辑。

人工智能AI技术 912

内核知识第12讲,SSDT表.以用户模式到系统模式的两种方式.

           内核知识第12讲,SSDT表.以用户模式到系统模式的两种方式. 一丶IDT解析. 我们知道.IDT表中存放着各种中断信息.比如当我们调用int 3的时候,则会调用IDT表中的第三项来进行调用.而函数地址则是操作系统给的. 因为中断是CPU和操作系统通信的一种方式. 查看IDTR (IDT表)第三项. IDT首地址给出了,其中表项是7ff 我...

weixin_30648587的博客 99

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

Zw函数与Nt函数的分别与联系

in ring3: lkd> ? ntdll!ZwOpenProcess Evaluate expression: 2089999739 = 7c92dd7b lkd> ?ntdll!NtOpenProcess Evaluate expression: 2089999739 = 7c92dd7b 可以看到,在ntdll中,ZwOpenProcess和NtOpenProcess其实是同

swanabin的专栏 989

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

SSDT相关知识

一 、SSDT(System Services Descriptor Table),系统服务描述符表。 这个表就是一个把ring3的Win32 API和ring0的内核API联系起来。SSDT并不仅仅只包含一个庞大的地址索引表,它还包含着一些其它有用的信息,诸如地址索引的基地址、服务函数个数等。通过修改此表的函数地址可以对常用windows函数及API进行hook,从而实现对一些关心的系统动作进

agoago_2009的专栏 904

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

_KiSystemServiceRepeat函数在_KiSystemService 函数里面

_KiSystemServiceRepeat: _KiServiceExit2 是两个非常重要的符号。

linux-0.11调试教程 1183

Windows内核情景分析-系统调用3

系统调用1和系统调用2说了这么多,还未进入int 0x2e所对应的系统服务函数KiSystemService(),这里来看下这个函数内部的处理;通过0x2e和IDTR从IDT中 查询获取对应的方法KiSystemService()。 所以这里 int 0x2e  对应  KiSystemService 入口 sysenter 对应的是 KiFastCallEntry 入口,这里不要被这些函数给...

airushaonian 722

Windows系统调用学习笔记(三)—— 保存现场

Windows系统调用学习笔记(三)—— 保存现场前言要点回顾API进入0环后调用的函数:基本概念Trap Frame结构线程相关的结构体ETHREADKTHREADCPU相关的结构体KPCR_NT_TIBKPRCB 前言 一、学习自滴水编程达人中级班课程,官网:https://bcdaren.com 二、海东老师牛逼! 要点回顾 API进入0环后调用的函数: 中断门 – KiSystemSe...

lzyddf的博客 2678

【2021.04.27】API函数的调用过程(保存现场)

内容回顾 前文提到了API进入0环以后会调用的函数:KiSystemService()、KiFastCallEntry()。 前文的练习 进入0环后,原来的寄存器存在哪里? 如何根据系统调用号(EAX寄存器中储存)找到要执行的内核函数? 调用时的参数是储存到3环的堆栈,如何传递给内核函数? 2种调用方式入口(KiSystemService()、KiFastCallEntry())是不一样的,它们是如何返回到3环的? 进入0环后,原来的寄存器存在哪里?本文将以KiSystemService(

oalken的博客 949

浅析Windows系统调用——2种切换到内核模式的方法

首先总结2种切换到内核模式方法的各自流程,以 Windows API WriteFile() 调用为例:内存法(中断法):(用户模式)WriteFile() -> ntdll!NtWriteFile() -> ntdll!KiIntSystemCall() -> int 2Eh -> 查找 IDT (中断描述符表)的内存地址,偏移0x2E处 ->...

weixin_34391445的博客 1336

KiFastCallEntry逆向&_KiSystemService 逆向

调用NTDLL下的系统服务存根函数时,会指定一个系统服务号,在内核下,会通过_KeServiceDescriptorTable查找到指定的系统服务例程。这个过程就是系统服务分发,系统服务分发主要通过SDT表进行分发,系统可支持多个SDT表,根据传入的系统服务号进行拆分,0-11位是号的索引,12位是表的索引号。..................

weixin_45678798的博客 599
上一篇: 2.1.ReactOS系统中中断描述符表进行初始化
下一篇: 2.5 windows xp,ReactOS系统快速系统调用的实现
zhyjhacker
博客等级 码龄17年 3203粉丝 406原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值