{"id":38,"date":"2020-05-07T17:10:20","date_gmt":"2020-05-07T15:10:20","guid":{"rendered":"http:\/\/pusha.be\/?p=38"},"modified":"2020-05-07T17:10:20","modified_gmt":"2020-05-07T15:10:20","slug":"exploration-of-svchost-exe-p-flag","status":"publish","type":"post","link":"https:\/\/pusha.be\/index.php\/2020\/05\/07\/exploration-of-svchost-exe-p-flag\/","title":{"rendered":"Exploration of svchost.exe \/P flag"},"content":{"rendered":"\n<p>Hey there!<\/p>\n\n\n\n<p>In this blog post, we are gonna take a look at the mysterious &#8220;\/P&#8221; flag of svchost.exe. TL;DR: P flag enforces different policies: DynamicCodePolicy, BinarySignaturePolicy and ExtensionPolicy.<\/p>\n\n\n\n<p>So first of all, what is svchost.exe? Following Wikipedia &#8220;<strong>svchost.exe<\/strong> (<strong>Service Host<\/strong>, or <strong>SvcHost<\/strong>) is a system <a href=\"https:\/\/en.wikipedia.org\/wiki\/Process_(computing)\">process<\/a> that can host from one to many <a href=\"https:\/\/en.wikipedia.org\/wiki\/Windows_service\">Windows services<\/a> in the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Windows_NT\">Windows NT<\/a> family of <a href=\"https:\/\/en.wikipedia.org\/wiki\/Operating_system\">operating systems<\/a>&#8220;. It&#8217;s basically the loader of your different services.<\/p>\n\n\n\n<p>Before diving into svchost with Cutter (or your favorite disassembler\/decompiler), it&#8217;s good to have the PDB of executable beforehand. PDB is short for &#8220;Program DataBase&#8221; and is storing the debug information of an executable. You can have it with PDBDownloader.exe (which you can grab here <a href=\"https:\/\/techcommunity.microsoft.com\/t5\/iis-support-blog\/pdb-downloader\/ba-p\/342969\">https:\/\/techcommunity.microsoft.com\/t5\/iis-support-blog\/pdb-downloader\/ba-p\/342969<\/a>) Microsoft lets you access some of the debug information for its different component so you can debug them with Windbg or import them in IDA which is cool. It&#8217;s important to note that you don&#8217;t have access to <strong>ALL<\/strong> the debug information. Only the symbols that Microsoft made public.<!--more--><\/p>\n\n\n\n<p>Now can begin the fun part. So, this static analysis was made with Cutter (GUI for radare2). Beforehand, you have an option to select a PDB file to load when you select advanced analysis. If you don&#8217;t do that, no worries, you can add it after.<\/p>\n\n\n\n<p>We are first landing in the entrypoint of our executable. You can see some common functions for an PE executable. A lot of them are just boilerplate like initialization of the arguments, security cookie, &#8230; We can go ahead and take a look at <em>wmain<\/em>.<br><img loading=\"lazy\" width=\"1376\" height=\"962\" class=\"wp-image-53\" style=\"width: 800px;\" src=\"http:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-10.06.51.png\" alt=\"\" srcset=\"https:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-10.06.51.png 1376w, https:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-10.06.51-300x210.png 300w, https:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-10.06.51-1024x716.png 1024w, https:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-10.06.51-768x537.png 768w\" sizes=\"(max-width: 1376px) 100vw, 1376px\" \/><\/p>\n\n\n\n<p>There is not a lot going in <em>wmain<\/em>. The only function that could be interesting is <em>InitializeSvcHostLib<\/em>. The others seem to register a new service, set up some timer and wait for it.<br><img loading=\"lazy\" width=\"878\" height=\"686\" class=\"wp-image-54\" style=\"width: 800px;\" src=\"http:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-10.07.10.png\" alt=\"\" srcset=\"https:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-10.07.10.png 878w, https:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-10.07.10-300x234.png 300w, https:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-10.07.10-768x600.png 768w\" sizes=\"(max-width: 878px) 100vw, 878px\" \/><\/p>\n\n\n\n<p>Now, this begins to be a lot more interesting. We see multiple calls there but what seems to be relevant for our goal is the <em>GetCommandLineW<\/em> call followed by <em>BuildCommandOption<\/em>. Surely <em>BuildCommandOption<\/em> should help us.<br><img loading=\"lazy\" width=\"1156\" height=\"1850\" class=\"wp-image-55\" style=\"width: 800px;\" src=\"http:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-10.07.45.png\" alt=\"\" srcset=\"https:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-10.07.45.png 1156w, https:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-10.07.45-187x300.png 187w, https:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-10.07.45-640x1024.png 640w, https:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-10.07.45-768x1229.png 768w, https:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-10.07.45-960x1536.png 960w\" sizes=\"(max-width: 1156px) 100vw, 1156px\" \/><\/p>\n\n\n\n<p><em>BuildCommandOption<\/em> is a lot more dense than the other functions so we need to take it piece by piece. From the previous function, we could see that the parameter passed to it is the return value of <em>GetCommandLineW<\/em> which from <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/win32\/api\/processenv\/nf-processenv-getcommandlinew\">https:\/\/docs.microsoft.com\/en-us\/windows\/win32\/api\/processenv\/nf-processenv-getcommandlinew<\/a> is the command-line string for the current process (surprising for a function called <em>GetCommandLineW<\/em>). Now, when we follow the parameter inside <em>BuildCommandOption<\/em>, we could see something interesting<br><img loading=\"lazy\" width=\"1304\" height=\"962\" class=\"wp-image-56\" style=\"width: 800px;\" src=\"http:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-10.11.12.png\" alt=\"\" srcset=\"https:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-10.11.12.png 1304w, https:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-10.11.12-300x221.png 300w, https:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-10.11.12-1024x755.png 1024w, https:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-10.11.12-768x567.png 768w\" sizes=\"(max-width: 1304px) 100vw, 1304px\" \/><br>The parameter is copied with <em>memcopy<\/em> to &#8220;ppiVar5 + 0xf&#8221; and with ppivar5 the result of <em>HeapAlloc<\/em>. From these two elements, we can say that ppiVar5 is a structure or an object. So we can try to create a structure in the <em>type<\/em> menu. Moreover, when looking at the end of the function, we can see that ppiVar5 is returned.<\/p>\n\n\n\n<p>Because I&#8217;m leazy, I won&#8217;t reverse engineer the whole function. By looking roughly at the function we can see some interesting things. We see a bunch of operations with familiar values like &#8220;0x20&#8221; (space character), &#8220;0x2d&#8221; (minus character) or &#8220;0x50&#8221; (P character). Normally, you would think that you will find a switch case or multiple &#8220;if else if&#8221; comparing a character with different hexadecimal values. But not here. Weirdly, we find subtraction followed by a comparison with 0. At the end, it&#8217;s still a comparison but not as straightforward as one would imagine.<br><img loading=\"lazy\" width=\"1182\" height=\"604\" class=\"wp-image-57\" style=\"width: 800px;\" src=\"http:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-10.12.56.png\" alt=\"\" srcset=\"https:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-10.12.56.png 1182w, https:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-10.12.56-300x153.png 300w, https:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-10.12.56-1024x523.png 1024w, https:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-10.12.56-768x392.png 768w\" sizes=\"(max-width: 1182px) 100vw, 1182px\" \/><br>Because of these values, we can make an educated guess that iVar2 (which comes from piVar8) is pointing to a character from command line and this loop is parsing it.<\/p>\n\n\n\n<p>Taking a closer look at this loop and particularly at the action made when the flag is found, we can see that our ppiVar5 is modified (at least different fields of ppiVar5 are modified). Here are the different cases:<br>&#8211; if K (0x4B) is set: *(ppiVar5 + 2) = 1<br>&#8211; else if S (0x53) is set: ppiVar5 is not modified, counter increases<br>&#8211; else if P (0x50) is set: *(ppiVar5 + 0x5c) = 1<br>We know that ppiVar5&#8217;s fields are set following the different flags that we put in our command lines. Officially we know that K is the service group and S the service from this group. But no information about P. At the end of this function, our ppiVar5 is a structure looking like that:<br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct cmd_parsed {\n    int64_t field_0;\n    int64_t field_1;\n    char k_flag;\n    char gap_0&#91;75];\n    char p_flag;\n    char gap_1&#91;27];\n    char *argv;\n};<\/code><\/pre>\n\n\n\n<p>So know, we can step back a little bit and come back to <em>InitializeSvcHostLib<\/em>. We see that the return value of <em>BuildCommandOption<\/em> is iVar4 and it&#8217;s passed to <em>CallPerInstanceInitFunctions<\/em> and so is our next stop.<\/p>\n\n\n\n<p>The first thing that we see is that Ghidra plugin for Cutter couldn&#8217;t decompile this function so we need to rely on the other plugin r2dec and disassembly. The second thing is that no <em>cmd_parsed<\/em> field set from <em>BuildCommandOption<\/em> are used but totally new ones&#8230; Hum weird. Maybe it&#8217;s time to step back again and look at our code.<\/p>\n\n\n\n<p>In <em>InitializeSvcHostLib<\/em>, we can see that the pointer to the object returned by <em>BuildCommandOption<\/em> (so in rax) is immediately copied to rbx. Now, if we follow rbx a little bit, we can see that its content is copied to rdx just after <em>BuildServiceArray<\/em>. Meaning that it&#8217;s in fact a parameter that Ghidra didn&#8217;t recognize.<br><img loading=\"lazy\" width=\"1772\" height=\"1860\" class=\"wp-image-58\" style=\"width: 800px;\" src=\"http:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-10.14.55.png\" alt=\"\" srcset=\"https:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-10.14.55.png 1772w, https:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-10.14.55-286x300.png 286w, https:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-10.14.55-976x1024.png 976w, https:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-10.14.55-768x806.png 768w, https:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-10.14.55-1463x1536.png 1463w\" sizes=\"(max-width: 1772px) 100vw, 1772px\" \/><\/p>\n\n\n\n<p>Let&#8217;s dig into <em>BuildServiceArray<\/em>. By modifying the function in order to use the ms calling convention (passing the parameter to rcx, rdx, r8 and r9 as described here <a href=\"https:\/\/docs.microsoft.com\/en-us\/cpp\/build\/x64-calling-convention?view=vs-2019\">https:\/\/docs.microsoft.com\/en-us\/cpp\/build\/x64-calling-convention?view=vs-2019<\/a>), we can follow param2 which is rdx (our <em>cmd_parsed<\/em> object).<br><img loading=\"lazy\" width=\"1580\" height=\"1870\" class=\"wp-image-59\" style=\"width: 800px;\" src=\"http:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-10.15.52.png\" alt=\"\" srcset=\"https:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-10.15.52.png 1580w, https:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-10.15.52-253x300.png 253w, https:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-10.15.52-865x1024.png 865w, https:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-10.15.52-768x909.png 768w, https:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-10.15.52-1298x1536.png 1298w\" sizes=\"(max-width: 1580px) 100vw, 1580px\" \/><br>We won&#8217;t be long in this function because param2 is quickly used by ReadPerInstanceRegistryParameters as the third parameter.<\/p>\n\n\n\n<p>From this new function, we can see that other fields are set and used from our main structure depending on different values queries from the registry key <em>Software\\Microsoft\\Windows NT\\CurrentVersion\\Svchost<\/em> like <em>CoInitializeSecurityParam<\/em> or <em>CoInitializeSecurityAllowLowBox<\/em>. But more importantly, we see that this function accesses our field set by P flag (<em>cmd_parsed.p_flag<\/em> or ppiVar5 + 0x5c).<br><img loading=\"lazy\" width=\"974\" height=\"712\" class=\"wp-image-50\" style=\"width: 800px;\" src=\"http:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-09.40.19.png\" alt=\"\" srcset=\"https:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-09.40.19.png 974w, https:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-09.40.19-300x219.png 300w, https:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-09.40.19-768x561.png 768w\" sizes=\"(max-width: 974px) 100vw, 974px\" \/><br>And as you can see, if the flag is set and the handle to the registry key is 0 (iStack664 holds the handle to the registry key), *(ppiVar5 + 0x60) is set. It&#8217;s interesting to note in what other occasion this field is modified. It is later set if the value <em>DynamicCodePolicy<\/em> exists. That&#8217;s interesting. So, maybe our P flag from the command line enforces the DynamicCodePolicy?<\/p>\n\n\n\n<p>In the next reference to uVar17 (holding the value of our P flag), we see that the field *(ppiVar5 + 0x64) is set to 1. And this field is also set when <em>BinarySignaturePolicy<\/em> exists.<br><img loading=\"lazy\" width=\"942\" height=\"452\" class=\"wp-image-51\" style=\"width: 800px;\" src=\"http:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-09.52.15.png\" alt=\"\" srcset=\"https:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-09.52.15.png 942w, https:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-09.52.15-300x144.png 300w, https:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-09.52.15-768x369.png 768w\" sizes=\"(max-width: 942px) 100vw, 942px\" \/><br>And last one, for *(ppiVar5 + 0x68) which is set when ExtensionPointsPolicy is set.<br><img loading=\"lazy\" width=\"962\" height=\"488\" class=\"wp-image-52\" style=\"width: 800px;\" src=\"http:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-10.01.59.png\" alt=\"\" srcset=\"https:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-10.01.59.png 962w, https:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-10.01.59-300x152.png 300w, https:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-10.01.59-768x390.png 768w\" sizes=\"(max-width: 962px) 100vw, 962px\" \/><\/p>\n\n\n\n<p><p>What does it mean for us? It means that if P flag is set, the different fields related to DynamicCodePolicy, BinarySignaturePolicy and ExtensionPolicy are set to 1. If it&#8217;s not the case, it depends of the different values found in the Registry Key.<\/p>At the end of this function, our structure should be like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct cmd_parsed {\n\tint64_t field_0;\n\tint64_t field_1;\n\tchar k_flag;\n\tchar gap_0;\n\tchar gap_1;\n\tchar gap_2;\n\tchar gap_3;\n\tchar gap_4;\n\tchar gap_5;\n\tchar gap_6;\n\tint64_t lpValue_reg;\n\tint64_t field_3;\n\tint64_t field_4;\n\tint64_t field_5;\n\tint64_t field_6;\n\tint64_t field_7;\n\tint64_t field_8;\n\tint64_t field_9;\n\tchar gap_7;\n\tchar gap_8;\n\tchar gap_9;\n\tchar gap_9_0;\n\tchar p_flag;\n\tchar gap_10;\n\tchar gap_11;\n\tchar gap_12;\n\tchar DynamicCodePolicy_flag;\n\tchar gap_13;\n\tchar gap_14;\n\tchar gap_15;\n\tchar BinarySignaturePolicy_flag;\n\tchar gap_16;\n\tchar gap_17;\n\tchar gap_18;\n\tchar ExtensionPointsPolicy_flag;\n\tchar gap_19;\n\tchar gap_20;\n\tchar gap_21;\n\tchar gap_22;\n\tchar gap_23;\n\tchar gap_24;\n\tchar gap_25;\n\tchar gap_26;\n\tchar gap_27;\n\tchar gap_28;\n\tchar gap_29;\n\tchar gap_30;\n\tchar gap_31;\n\tchar gap_32;\n\tchar gap_33;\n\tchar *argv;\n};<\/code><\/pre>\n\n\n\n<p>Finally, we can go out of this function. <em>BuildServiceTable<\/em> doesn&#8217;t access our structure and <em>CallPerInstanceInitFunctions<\/em> is. When analyzing it and applying the different field offsets to rbx, we can see that they are checked and followed by a call to <em>SetProcessMitigationPolicy<\/em> which confirms that these fields set up the different Mitigation Policies! And this is the end of our journey into svchost!<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" width=\"1972\" height=\"1772\" src=\"http:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-12.43.24.png\" alt=\"\" class=\"wp-image-77\" srcset=\"https:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-12.43.24.png 1972w, https:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-12.43.24-300x270.png 300w, https:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-12.43.24-1024x920.png 1024w, https:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-12.43.24-768x690.png 768w, https:\/\/pusha.be\/wp-content\/uploads\/2020\/05\/Screenshot-2020-05-07-at-12.43.24-1536x1380.png 1536w\" sizes=\"(max-width: 1972px) 100vw, 1972px\" \/><\/figure>\n\n\n\n<p>If there is any question, remark or mistake, don&#8217;t hesitate to contact me on Twitter <a rel=\"noreferrer noopener\" href=\"https:\/\/twitter.com\/redmed666\" target=\"_blank\">@redmed666<\/a><\/p>\n\n\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey there! In this blog post, we are gonna take a look at the mysterious &#8220;\/P&#8221; flag of svchost.exe. TL;DR: P flag enforces different policies: DynamicCodePolicy, BinarySignaturePolicy and ExtensionPolicy. So first of all, what is svchost.exe? Following Wikipedia &#8220;svchost.exe (Service Host, or SvcHost) is a system process that can host from one to many Windows &hellip; <a href=\"https:\/\/pusha.be\/index.php\/2020\/05\/07\/exploration-of-svchost-exe-p-flag\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Exploration of svchost.exe \/P flag<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/pusha.be\/index.php\/wp-json\/wp\/v2\/posts\/38"}],"collection":[{"href":"https:\/\/pusha.be\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pusha.be\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pusha.be\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pusha.be\/index.php\/wp-json\/wp\/v2\/comments?post=38"}],"version-history":[{"count":23,"href":"https:\/\/pusha.be\/index.php\/wp-json\/wp\/v2\/posts\/38\/revisions"}],"predecessor-version":[{"id":85,"href":"https:\/\/pusha.be\/index.php\/wp-json\/wp\/v2\/posts\/38\/revisions\/85"}],"wp:attachment":[{"href":"https:\/\/pusha.be\/index.php\/wp-json\/wp\/v2\/media?parent=38"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pusha.be\/index.php\/wp-json\/wp\/v2\/categories?post=38"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pusha.be\/index.php\/wp-json\/wp\/v2\/tags?post=38"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}