KiDispatchException

[复制链接]

该用户从未签到

2380

主题

2433

帖子

9139

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
9139
QQ
跳转到指定楼层
楼主
发表于 2022-4-15 20:05:11 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

想要查看内容赶紧注册登陆吧!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x

  1. KiDispatchException
  2. VOID
  3.         KiDispatchException (
  4.         IN PEXCEPTION_RECORD ExceptionRecord,
  5.         IN PKEXCEPTION_FRAME ExceptionFrame,
  6.         IN PKTRAP_FRAME TrapFrame,
  7.         IN KPROCESSOR_MODE PreviousMode,
  8.         IN BOOLEAN FirstChance
  9.         )
  10. {
  11.         CONTEXT ExtendedContext;
  12.         ULONG   Flags;
  13.         ULONG   ContextLength;
  14.         PEXTEND_CONTEXT_AREA ExtenContextArea;
  15.         NTSTATUS status;
  16.         ULONG64 FaultingRsp;
  17.         BOOLEAN bText;
  18.         EXCEPTION_RECORD ExceptionRecord1;
  19.         PMACHINE_FRAME MachineFrame1;
  20.         ULONG64 UserStack1;
  21.         ULONG64 UserStack2;
  22.         ULONG64 UserStack3;
  23.         ULONG64 FaultingRspTemp;
  24.         CONTEXT_EX UserContextEx1;
  25.         CONTEXT_EX UserContextEx2;

  26.         *(PBYTE)((ULONGLONG)&ExtendedContext) = PreviousMode;
  27.         KeGetCurrentPrcb()->KeExceptionDispatchCount += 1;
  28.         Flags = 0x10001F;

  29.         if (PreviousMode)
  30.         {
  31.                 if (_bittest((long*)&(*(ULONGLONG*)KeFeatureBits), 0x17u))
  32.                 {
  33.                         if ( *(ULONGLONG*)KeEnabledXStateFeatures & 0xFFFFFFFFFFFFFFFCui64 )
  34.                                 Flags = 0x10005F;
  35.                 }
  36.         }
  37.         ((Old_RtlGetExtendedContextLength)RtlGetExtendedContextLength)(Flags,&ContextLength);
  38.         //*(PDWORD)((ULONGLONG)&ExtendedContext+4) = Flags;
  39.         ExtendedContext.ContextFlags = Flags;
  40.         status = ((Old_RtlInitializeExtendedContext)RtlInitializeExtendedContext)(&ExtendedContext,Flags,&ExtenContextArea);
  41.         if ((Flags&0x10040) == 0x10040)
  42.         {
  43.                 *(ULONGLONG*)(*(PDWORD)((ULONGLONG)ExtenContextArea+0x10)+(ULONGLONG)ExtenContextArea) = \
  44.                         *(ULONGLONG*)KeEnabledXStateFeatures & 0xFFFFFFFFFFFFFFFCui64;
  45.         }

  46.         ((Old_KeContextFromKframes)KeContextFromKframes)(TrapFrame,ExceptionFrame,&ExtendedContext);

  47.         if (ExceptionRecord->ExceptionCode == STATUS_BREAKPOINT)
  48.                 ExtendedContext.Rip--;

  49.         if (((Old_KiPreprocessFault)KiPreprocessFault)(
  50.                 ExceptionRecord,
  51.                 TrapFrame,
  52.                 &ExtendedContext,
  53.                 PreviousMode) != FALSE)
  54.         {
  55.                 goto Handled1;
  56.         }

  57.         if (PreviousMode == KernelMode)
  58.         {
  59.                 if (FirstChance != PreviousMode)
  60.                 {

  61.                         if (((PKDEBUG_ROUTINE)(*(PULONG64)KiDebugRoutine))(TrapFrame,
  62.                                 ExceptionFrame,
  63.                                 ExceptionRecord,
  64.                                 &ExtendedContext,
  65.                                 PreviousMode,
  66.                                 FALSE) != FALSE)
  67.                         {
  68.                                 goto Handled1;
  69.                         }

  70.                         if (((Old_RtlDispatchException)RtlDispatchException)(ExceptionRecord, &ExtendedContext) != FALSE)
  71.                         {
  72.                                 goto Handled1;
  73.                         }
  74.                 }

  75.                 if (((PKDEBUG_ROUTINE)(*(PULONG64)KiDebugRoutine))(
  76.                         TrapFrame,
  77.                         ExceptionFrame,
  78.                         ExceptionRecord,
  79.                         &ExtendedContext,
  80.                         FALSE,
  81.                         TRUE) != FALSE)
  82.                 {
  83.                         goto Handled1;
  84.                 }

  85.                 KeBugCheckEx(KMODE_EXCEPTION_NOT_HANDLED,
  86.                         ExceptionRecord->ExceptionCode,
  87.                         (ULONG64)ExceptionRecord->ExceptionAddress,
  88.                         ExceptionRecord->ExceptionInformation[0],
  89.                         ExceptionRecord->ExceptionInformation[1]);
  90.         }
  91.         else
  92.         {
  93.                 if (( ((PEPROCESS_S)PsGetCurrentProcess())->Wow64Process != NULL) &&
  94.                         (ExceptionRecord->ExceptionCode == STATUS_DATATYPE_MISALIGNMENT) &&
  95.                         ((TrapFrame->EFlags & EFLAGS_AC_MASK) != 0))
  96.                 {
  97.                                 TrapFrame->EFlags &= ~EFLAGS_AC_MASK;
  98.                                 goto TheEnd1;
  99.                 }

  100.                 if ((ExtendedContext.SegCs & 0xfff8) == KGDT64_R3_CMCODE)
  101.                 {
  102.                         switch (ExceptionRecord->ExceptionCode)
  103.                         {
  104.                         case STATUS_BREAKPOINT:
  105.                                 ExceptionRecord->ExceptionCode = STATUS_WX86_BREAKPOINT;
  106.                                 break;

  107.                         case STATUS_SINGLE_STEP:
  108.                                 ExceptionRecord->ExceptionCode = STATUS_WX86_SINGLE_STEP;
  109.                                 break;
  110.                         }

  111.                         FaultingRsp = (ExtendedContext.Rsp & 0xfffffff0UI64);

  112.                 } else {
  113.                         FaultingRsp = ExtendedContext.Rsp;
  114.                 }

  115.                 if (STATUS_SINGLE_STEP == ExceptionRecord->ExceptionCode)
  116.                 {
  117.                         PETHREAD_S CurThread;
  118.                         CurThread = (PETHREAD_S)PsGetCurrentThread();
  119.                         if (CurThread)
  120.                         {
  121.                                 if ((*(PBYTE)((ULONGLONG)CurThread+3))&0x40)
  122.                                 {
  123.                                         *(PULONGLONG)(*(PULONGLONG)((ULONGLONG)CurThread + 0x1B8) + 0x58) |= 0x10;
  124.                                 }
  125.                         }
  126.                 }

  127.                 memset(&ExceptionRecord->ExceptionInformation[ExceptionRecord->NumberParameters],
  128.                         0,
  129.                         ExceptionRecord - &ExceptionRecord->ExceptionInformation[ExceptionRecord->NumberParameters] + sizeof(EXCEPTION_RECORD));

  130.                 if (FirstChance == TRUE)
  131.                 {
  132.                         bText = FALSE;
  133.                         if (ExceptionRecord->ExceptionCode == STATUS_BREAKPOINT ||
  134.                                 ExceptionRecord->ExceptionCode == STATUS_WX86_BREAKPOINT)
  135.                         {
  136.                                 if (ExceptionRecord->NumberParameters  > 0 &&
  137.                                         ExceptionRecord->ExceptionInformation[0] != NULL)
  138.                                 {
  139.                                         bText = TRUE;
  140.                                 }
  141.                         }
  142.                         //KiDispatchException + 0x241  eb KiDispatchException + 0x241 0x90 0xe9
  143.                         if (((PEPROCESS_S)PsGetCurrentProcess())->DebugPort != NULL ||
  144.                                 *(ULONGLONG*)KdIgnoreUmExceptions != 0)
  145.                         {
  146.                                 if (bText == FALSE)
  147.                                 {
  148.                                         goto __Leep2;
  149.                                 }
  150.                         }

  151.                         if (((PKDEBUG_ROUTINE)(*(PULONG64)KiDebugRoutine))(
  152.                                 TrapFrame,
  153.                                 ExceptionFrame,
  154.                                 ExceptionRecord,
  155.                                 &ExtendedContext,
  156.                                 PreviousMode,
  157.                                 NULL) != FALSE)
  158.                         {
  159.                                 goto Handled1;
  160.                         }

  161.             __Leep2:
  162.                         if (DbgkForwardException(ExceptionRecord, TRUE, FALSE)) {
  163.                                 goto TheEnd1;
  164.                         }

  165.                         TrapFrame->EFlags &= ~EFLAGS_TF_MASK;

  166.                         ExceptionRecord1.ExceptionCode = STATUS_ACCESS_VIOLATION;

  167.                         memmove(&ExceptionRecord->ExceptionCode,&ExceptionRecord1.ExceptionCode,sizeof(EXCEPTION_RECORD));

  168.                         FaultingRspTemp = FaultingRsp;//r15
  169.                         if ((Flags&0x10040) == 0x10040)
  170.                         {
  171.                                 FaultingRspTemp -= ExtenContextArea->Unkwon5;
  172.                                 FaultingRspTemp = FaultingRspTemp&0x0FFFFFFFFFFFFFFC0;
  173.                         }
  174.                         repeat:
  175.                         __try
  176.                         {
  177.                                 MachineFrame1 =
  178.                                         (PMACHINE_FRAME)((FaultingRspTemp - sizeof(MACHINE_FRAME)) & ~STACK_ROUND);

  179.                                 UserStack1 = (ULONG64)MachineFrame1 - EXCEPTION_RECORD_LENGTH;
  180.                                 UserContextEx1.All.Offset = (UserStack1 - 0x20);
  181.                                 UserStack2 = UserContextEx1.All.Offset - CONTEXT_LENGTH;
  182.                                 UserContextEx2.All.Offset = UserStack2 - UserContextEx1.All.Offset;
  183.                                 UserContextEx2.All.Length = FaultingRsp - UserStack2;
  184.                                 UserContextEx2.Legacy.Offset = UserStack2 - UserContextEx1.All.Offset;
  185.                                 UserContextEx2.Legacy.Length = 0x4D0;
  186.                                 UserContextEx2.XState.Offset = FaultingRspTemp - UserContextEx1.All.Offset;
  187.                                 UserContextEx2.XState.Length = FaultingRsp - FaultingRspTemp;

  188.                                 ProbeForWriteSmallStructure((PVOID)UserStack2,
  189.                                         sizeof(MACHINE_FRAME) + EXCEPTION_RECORD_LENGTH + CONTEXT_LENGTH,
  190.                                         STACK_ALIGN);

  191.                                 MachineFrame1->Rsp = FaultingRsp;
  192.                                 MachineFrame1->Rip = ExtendedContext.Rip;

  193.                                 //*(PEXCEPTION_RECORD)UserStack1 = *ExceptionRecord;
  194.                                 memmove((PVOID)UserStack1,ExceptionRecord,0x98);
  195.                                 ((Old_RtlpCopyExtendedContext)RtlpCopyExtendedContext)(
  196.                                         TRUE,
  197.                                         UserContextEx1.All.Offset,
  198.                                         &UserContextEx2.All.Offset,
  199.                                         Flags,
  200.                                         ExtenContextArea,
  201.                                         0);
  202.                                 //*(PCONTEXT)UserContextEx1 = *(PCONTEXT)UserContextEx2.All.Offset;
  203.                                 memmove(&UserContextEx1,&UserContextEx2,0x18);

  204.                                 TrapFrame->Rsp = UserStack2; //+0x180  r14       
  205.                                 _disable();
  206.                                 TrapFrame->SegCs = KGDT64_R3_CODE | RPL_MASK; // +0x170
  207.                                 TrapFrame->Rip = (ULONG64)KeUserExceptionDispatcher;
  208.                                 if (((PEPROCESS_S)PsGetCurrentProcess())->Pcb.InstrumentationCallback)
  209.                                 {
  210.                                         TrapFrame->R10 = TrapFrame->Rip;
  211.                                         TrapFrame->Rip = (ULONG64)((PEPROCESS_S)PsGetCurrentProcess())->Pcb.InstrumentationCallback;
  212.                                 }
  213.                                 _enable();
  214.                                 goto TheEnd1;
  215.                         } __except (KiCopyInformation(&ExceptionRecord1,
  216.                                 (GetExceptionInformation())->ExceptionRecord))
  217.                         {
  218.                                 if (ExceptionRecord1.ExceptionCode == STATUS_STACK_OVERFLOW)
  219.                                 {
  220.                                         ExceptionRecord1.ExceptionAddress = ExceptionRecord->ExceptionAddress;
  221.                                         *ExceptionRecord = ExceptionRecord1;
  222.                                         goto repeat;
  223.                                 }
  224.                         }
  225.                 }
  226.                 else
  227.                 {
  228.                         if (DbgkForwardException(ExceptionRecord, TRUE, TRUE)) {
  229.                                 goto TheEnd1;
  230.                         }else if (DbgkForwardException(ExceptionRecord, FALSE, TRUE)){
  231.                                 goto TheEnd1;
  232.                         }else{
  233.                                 ZwTerminateProcess(NtCurrentProcess(), ExceptionRecord->ExceptionCode);
  234.                                 KeBugCheckEx(
  235.                                         KERNEL_MODE_EXCEPTION_NOT_HANDLED,
  236.                                         ExceptionRecord->ExceptionCode,
  237.                                         (ULONG)ExceptionRecord->ExceptionAddress,
  238.                                         (ULONG)TrapFrame,
  239.                                         0);
  240.                         }
  241.                 }
  242.         }


  243. Handled1:
  244.         ((Old_KeContextToKframes)KeContextToKframes)(
  245.                 TrapFrame,
  246.                 ExceptionFrame,
  247.                 &ExtendedContext,
  248.                 ExtendedContext.ContextFlags,
  249.                 PreviousMode);
  250. TheEnd1:;
  251. }
复制代码
分享到:  QQ好友和群QQ好友和群
收藏收藏
回复

使用道具 举报

该用户从未签到

0

主题

7

帖子

7

积分

邀请会员

积分
7
沙发
发表于 2022-5-25 11:33:50 | 只看该作者
帅哥  头文件呢
回复 支持 反对

使用道具 举报

快速回复高级模式
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表