首页
社区
课程
招聘
[求助]Hook MSR_LSTAR的疑问
2016-4-17 01:30 11093

[求助]Hook MSR_LSTAR的疑问

2016-4-17 01:30
11093
Hello,all~
我尝试HOOK MSR_LSTAR寄存器
在虚拟机中测试
HOOK成功之后 只要进入KiSystemCall64 虚拟机就直接死掉

求解

关键代码如下:
=======================================C部分
_nt_KiSystemCall64 = (ULONG64)__readmsr(MSR_LSTAR);

ULONG64	ullAsmKiSystemCall64 = (ULONG64)&asm_KiSystemCall64;
	
__writemsr(MSR_LSTAR, ullAsmKiSystemCall64);

=======================================ASM部分
EXTERN _nt_KiSystemCall64:DQ

USERMD_STACK_GS = 10h
KERNEL_STACK_GS = 1A8h
SYSCALL_MAX_INDEX = 4096

.code

asm_KiSystemCall64 proc
	cli										
	swapgs
	mov  gs:[USERMD_STACK_GS], rsp
	mov  rsp, gs:[USERMD_STACK_GS]
	swapgs
	jmp  [_nt_KiSystemCall64]

asm_KiSystemCall64 endp

end


[CTF入门培训]顶尖高校博士及硕士团队亲授《30小时教你玩转CTF》,视频+靶场+题目!助力进入CTF世界

收藏
点赞0
打赏
分享
最新回复 (8)
雪    币: 26
活跃值: (1332)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
lukarl 2016-4-17 11:40
2
0
; *********************************************************
;
; Determine if the specific syscall should be hooked
;
; if (SyscallHookEnabled[EAX & 0xFFF] == TRUE)
;     jmp KiSystemCall64_Emulate
; else (fall-through)
;     jmp KiSystemCall64
;
; *********************************************************
SyscallEntryPoint PROC

    cli                                     ; Disable interrupts
    swapgs                                  ; swap GS base to kernel PCR
    mov         gs:[USERMD_STACK_GS], rsp   ; save user stack pointer

    cmp         rax, SYSCALL_MAX_INDEX      ; Is the index larger than the array size?
    jge         KiSystemCall64              ;

    lea         rsp, offset SyscallHookEnabled; RSP = &SyscallHookEnabled
    cmp         byte ptr [rsp + rax], 0     ; Is hooking enabled for this index?
    jne         KiSystemCall64_Emulate      ; NE = index is hooked

SyscallEntryPoint ENDP

; *********************************************************
;
; Return to the original NTOSKRNL syscall handler
; (Restore all old registers first)
;
; *********************************************************
KiSystemCall64 PROC

        mov         rsp, gs:[USERMD_STACK_GS]   ; Usermode RSP
        swapgs                                  ; Switch to usermode GS
        jmp         [NtSyscallHandler]          ; Jump back to the old syscall handler

KiSystemCall64 ENDP

; *********************************************************
;
; Emulated routine executed directly after a SYSCALL
; (See: MSR_LSTAR)
;
; *********************************************************
KiSystemCall64_Emulate PROC

    ; NOTE:
    ; First 2 lines are included in SyscallEntryPoint

    mov         rsp, gs:[KERNEL_STACK_GS]   ; set kernel stack pointer
    push        2Bh                         ; push dummy SS selector
    push        qword ptr gs:[10h]          ; push user stack pointer
    push        r11                         ; push previous EFLAGS
    push        33h                         ; push dummy 64-bit CS selector
    push        rcx                         ; push return address
    mov         rcx, r10                    ; set first argument value

    sub         rsp, 8h                     ; allocate dummy error code
    push        rbp                         ; save standard register
    sub         rsp, 158h                   ; allocate fixed frame
    lea         rbp, [rsp+80h]              ; set frame pointer
    mov         [rbp+0C0h], rbx             ; save nonvolatile registers
    mov         [rbp+0C8h], rdi             ;
    mov         [rbp+0D0h], rsi             ;
    mov         byte ptr [rbp-55h], 2h      ; set service active
    mov         rbx, gs:[188h]              ; get current thread address
    prefetchw   byte ptr [rbx+1D8h]         ; prefetch with write intent
    stmxcsr     dword ptr [rbp-54h]         ; save current MXCSR
    ldmxcsr     dword ptr gs:[180h]         ; set default MXCSR
    cmp         byte ptr [rbx+3], 0         ; test if debug enabled
    mov         word ptr [rbp+80h], 0       ; assume debug not enabled
    je          KiSS05                      ; if z, debug not enabled
    mov         [rbp-50h], rax              ; save service argument registers
    mov         [rbp-48h], rcx              ;
    mov         [rbp-40h], rdx              ;
        test        byte ptr [rbx+3],3
    mov         [rbp-38h], r8               ;
    mov         [rbp-30h], r9               ;
        je          a2

        call        [KiSaveDebugRegisterState]
a2:
    test        byte ptr [rbx+3],80h
        je          a3                       
        mov         ecx,0C0000102h
        rdmsr
        shl         rdx,20h
        or          rax,rdx
a3:
        cmp         qword ptr [rbx+0B8h],rax
        je          B0
        cmp         qword ptr [rbx+1B0h],rax
        je          B0
        mov         rdx,qword ptr [rbx+1B8h]
        bts         dword ptr [rbx+4Ch],0Bh
        dec         word ptr [rbx+1C4h]
        mov         qword ptr [rdx+80h],rax
        sti
        call        [KiUmsCallEntry]
        jmp         FA0
B0:
        test        byte ptr [rbx+3],40h
        je          FA0
        lock        bts dword ptr [rbx+100h],8
FA0:
        mov         rax,qword ptr [rbp-50h]
        mov         rcx,qword ptr [rbp-48h]
        mov         rdx,qword ptr [rbp-40h]
        mov         r8,qword ptr [rbp-38h]
        mov         r9,qword ptr [rbp-30h]
        xchg        ax,ax
KiSS05:
        sti
        mov         qword ptr [rbx+1E0h],rcx
        mov         dword ptr [rbx+1F8h],eax

  ;int         3                           ; FIXME (Syscall with debug registers active)
;  align       10h
KiSystemCall64_Emulate ENDP

KiSystemServiceStart_Emulate PROC

    mov         [rbx+1D8h], rsp
    mov         edi, eax
    shr         edi, 7
    and         edi, 20h
    and         eax, 0FFFh

KiSystemServiceStart_Emulate ENDP

KiSystemServiceRepeat_Emulate PROC
    lea     r10,[KeServiceDescriptorTable]       
        movsxd  r11,dword ptr [r10+rax*4];
        lea     r10, offset SyscallPointerTable
    mov     r10, qword ptr [r10 + rax * 8h]
        lea         r11, offset SyscallParamTable
    movzx       rax, byte ptr [r11 + rax]   ; RAX = paramter count
        cmp     edi,20h;       
        push [KiSystemServiceRepeat]
        ret
KiSystemServiceRepeat_Emulate ENDP
雪    币: 140
活跃值: (125)
能力值: ( LV5,RANK:60 )
在线值:
发帖
回帖
粉丝
karlx 1 2016-4-17 12:21
3
0
[QUOTE=lukarl;1425509]; *********************************************************
;
; Determine if the specific syscall should be hooked
;
; if (SyscallHookEnabled[EAX & 0x...[/QUOTE]

就是这个 但是问题是 为什么我提取那一小段 不能用。。。
能不能私信加个企鹅细聊下
雪    币: 26
活跃值: (1332)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
lukarl 2016-4-17 12:38
4
0
企鹅:三九四九九九四八二,这个玩意没啥复杂的
雪    币: 36
活跃值: (14)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
a晴天安好 2016-7-4 15:39
5
0
楼主,我有问题想请教一下你,我现在想获取MSR寄存器信息。能不能告知您的微信或者qq联系方式?
雪    币: 283
活跃值: (3119)
能力值: ( LV5,RANK:75 )
在线值:
发帖
回帖
粉丝
囧囧 2016-7-4 19:12
6
0
与系统的 KiSystemCall64 对照,有两个问题:

1. 为什么要 cli ?

2. 为什么不

asm_KiSystemCall64 proc
  jmp  [_nt_KiSystemCall64]
asm_KiSystemCall64 endp

   或者:

asm_KiSystemCall64 proc
               
  swapgs
  mov     gs:10h, rsp     ; 保存用户态栈
  mov     rsp, gs:1A8h    ; 切换到内核栈

  jmp  [_nt_KiSystemCall64 + 15h]

asm_KiSystemCall64 endp
雪    币: 1038
活跃值: (1216)
能力值: ( LV3,RANK:35 )
在线值:
发帖
回帖
粉丝
StriveXjun 2017-4-13 16:08
7
0
学习学习
雪    币:
能力值: ( LV1,RANK:0 )
在线值:
发帖
回帖
粉丝
39周 2022-6-13 11:17
8
0
大佬解决了这个问题吗
雪    币: 2666
活跃值: (3297)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
Mr.hack 2022-6-13 19:15
9
0
 mov  gs:[USERMD_STACK_GS], rsp
mov  rsp, gs:[USERMD_STACK_GS]
这两条汇编指令有意义吗
游客
登录 | 注册 方可回帖
返回