{"id":6836,"date":"2019-10-11T23:20:26","date_gmt":"2019-10-11T23:20:26","guid":{"rendered":"http:\/\/www.hexacorn.com\/blog\/?p=6836"},"modified":"2019-10-11T23:31:28","modified_gmt":"2019-10-11T23:31:28","slug":"beyond-good-ol-run-key-part-119","status":"publish","type":"post","link":"https:\/\/www.hexacorn.com\/blog\/2019\/10\/11\/beyond-good-ol-run-key-part-119\/","title":{"rendered":"Beyond good ol\u2019 Run key, Part 119"},"content":{"rendered":"\n<p>Pretty much everyone knows about the <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/win32\/debug\/configuring-automatic-debugging\">AEDebug key.<\/a><\/p>\n\n\n\n<p>Turns out this key has a twin brother, called <em>AeDebugProtected<\/em>.<\/p>\n\n\n\n<p>When the <em>WerpGetDebugger <\/em>function is called, it calls <em>NtQueryInformationProcess <\/em>to retrieve a basic information about the process. If the process&#8217;s basic info data at position 28 (32-bit!) returns a dword value that if masked with 1 is non-zero then <em>AeDebugProtected<\/em> key is used&#8230;<\/p>\n\n\n\n<p>Okay, this is confusing&#8230; Let&#8217;s step back. <\/p>\n\n\n\n<p>The traditional way of calling  <em><a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/win32\/api\/winternl\/nf-winternl-ntqueryinformationprocess\">NtQueryInformationProcess<\/a> <\/em>with <em>ProcessBasicInformation <\/em>class is typically delivered using a &#8216;classic&#8217; definition of _PROCESS_BASIC_INFORMATION structure being:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>typedef struct _PROCESS_BASIC_INFORMATION {\n    PVOID Reserved1;\n    PPEB PebBaseAddress;\n    PVOID Reserved2[2];\n    ULONG_PTR UniqueProcessId;\n    PVOID Reserved3;\n} PROCESS_BASIC_INFORMATION;<\/code><\/pre>\n\n\n\n<p>Of course, available source codes online can give us a more precise definition of this structure e.g. Process Hacker source code defines it as:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>typedef struct _PROCESS_BASIC_INFORMATION\n{\n    NTSTATUS ExitStatus;\n    PPEB PebBaseAddress;\n    ULONG_PTR AffinityMask;\n    KPRIORITY BasePriority;\n    HANDLE UniqueProcessId;\n    HANDLE InheritedFromUniqueProcessId;\n} PROCESS_BASIC_INFORMATION, *PPROCESS_BASIC_INFORMATION;<\/code><\/pre>\n\n\n\n<p>In both cases the size of a structure is 24 bytes (32-bit system!). When I looked at the code of  <em>WerpGetDebugger <\/em> I was surprised to see that some calls to <em>NtQueryInformationProcess <\/em> \/  <em>ProcessBasicInformation <\/em>rely on a structure that is 32 bytes long!<\/p>\n\n\n\n<p>Okay, so now we know there is some extra information provided by  <em>NtQueryInformationProcess <\/em>to <em>WerpGetDebugger<\/em> function and that data determines which debug key is being used. Since this <em>ntdll <\/em>function is simply passed to kernel, I went to look at the code of <em>NtQueryInformationProcess <\/em>inside <em>ntoskrnl.exe<\/em>. I quickly discovered that the function does indeed expect a structure that is either 24 or 32 bytes long. Cool.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" loading=\"lazy\" width=\"369\" height=\"271\" src=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2019\/10\/NtQueryInformationProcess-0.png\" alt=\"\" class=\"wp-image-6839\" srcset=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2019\/10\/NtQueryInformationProcess-0.png 369w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2019\/10\/NtQueryInformationProcess-0-300x220.png 300w\" sizes=\"(max-width: 369px) 100vw, 369px\" \/><\/figure>\n\n\n\n<p>Continuing my analysis I noticed that the the field at the position 28 is filled in with a result of a call to a function called <em>PsIsProtectedProcess<\/em>. <a href=\"http:\/\/download.microsoft.com\/download\/a\/f\/7\/af7777e5-7dcd-4800-8a0a-b18336565f5b\/process_vista.doc\">Protected processes [DOC warning]<\/a> is a technology described in the past, so not a biggie. And the fact this is what is being checked by the function is of course something we should have expected, given the name used by the Registry Key I mentioned earlier, however&#8230; at least we can confirm this with our code analysis&#8230;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" loading=\"lazy\" width=\"269\" height=\"47\" src=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2019\/10\/NtQueryInformationProcess-1.png\" alt=\"\" class=\"wp-image-6838\"\/><\/figure>\n\n\n\n<p>And here we are with a few conclusive bits:<\/p>\n\n\n\n<ul><li><em>NtQueryInformationProcess <\/em> \/  <em>ProcessBasicInformation<\/em> may use 2 different structure versions!<\/li><li>The field at offset 28 (32-bit!) tells us if the process is protected or not. Depending on this, different <em>AeDebug<\/em> Registry key will be used to launch debugger when the app crashes.<\/li><\/ul>\n\n\n\n<p>The longer structure can be prototyped as this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>typedef struct _PROCESS_BASIC_INFORMATION_EXT\n{\nNTSTATUS ExitStatus;\nPPEB PebBaseAddress;\nULONG_PTR AffinityMask;\nKPRIORITY BasePriority;\nHANDLE UniqueProcessId;\nHANDLE InheritedFromUniqueProcessId;\nULONG unknown;\nULONG IsProtectedProcess;\n} <\/code><\/pre>\n\n\n\n<p>At this is how we arrived at <em>Beyond good ol\u2019 Run key, Part 119<\/em>.<\/p>\n\n\n\n<p>Okay, not quite yet.<\/p>\n\n\n\n<p>The value under this Protected Registry key that the  <em>WerpGetDebugger<\/em>  function will use is not <em>Debugger<\/em>, but <em>ProtectedDebugger<\/em>. Yup, we are talking:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\AeDebugProtected\\ProtectedDebugger=&lt;exe><\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Pretty much everyone knows about the AEDebug key. Turns out this key has a twin brother, called AeDebugProtected. When the WerpGetDebugger function is called, it calls NtQueryInformationProcess to retrieve a basic information about the process. If the process&#8217;s basic info &hellip; <a href=\"https:\/\/www.hexacorn.com\/blog\/2019\/10\/11\/beyond-good-ol-run-key-part-119\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[13,35],"tags":[],"_links":{"self":[{"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/posts\/6836"}],"collection":[{"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/comments?post=6836"}],"version-history":[{"count":5,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/posts\/6836\/revisions"}],"predecessor-version":[{"id":6843,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/posts\/6836\/revisions\/6843"}],"wp:attachment":[{"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/media?parent=6836"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/categories?post=6836"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/tags?post=6836"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}