{"id":2465,"date":"2014-08-27T13:39:53","date_gmt":"2014-08-27T13:39:53","guid":{"rendered":"http:\/\/www.hexacorn.com\/blog\/?p=2465"},"modified":"2014-09-20T22:22:52","modified_gmt":"2014-09-20T22:22:52","slug":"beyond-good-ol-run-key-part-16","status":"publish","type":"post","link":"https:\/\/www.hexacorn.com\/blog\/2014\/08\/27\/beyond-good-ol-run-key-part-16\/","title":{"rendered":"Beyond good ol\u2019 Run key, Part 16"},"content":{"rendered":"<p>Documenting various persistence mechanisms would not be complete without mentioning these that could be based on legitimate and fully-documented system features. One such mechanism we are going to talk about is called &#8216;custom Power Shell profile&#8217;. It is a distant cousin of autoexec.bat and it can be abused to ensure some malware component is loaded anytime someone starts powershell host.<\/p>\n<p>There is actually a full article describing this mechanism <a href=\"http:\/\/technet.microsoft.com\/en-us\/magazine\/2008.10.windowspowershell.aspx#id0190010\">here<\/a>, so I will just quote the most important (from the forensics perspective) bit:<\/p>\n<ul>\n<li><strong>%windir%\\system32\\Windows\u00adPowerShell\\v1.0\\profile.ps1<\/strong>\n<ul>\n<li>This is for all users of the computer and for all shells.<\/li>\n<\/ul>\n<\/li>\n<li><strong>%windir%\\system32\\Windows\u00adPowerShell\\v1.0\\Microsoft.Power\u00adShell_profile.ps1<\/strong>\n<ul>\n<li>This is for all users of the computer, but it is only for the Microsoft.PowerShell shell.<\/li>\n<\/ul>\n<\/li>\n<li><strong>%UserProfile%\\Documents\\Windows\u00adPowerShell\\profile.ps1<\/strong>\n<ul>\n<li>This is for the current user only and all shells.<\/li>\n<\/ul>\n<\/li>\n<li><strong>%UserProfile%\\Documents\\WindowsPowerShell\\Microsoft.PowerShell_profile.ps1<\/strong>\n<ul>\n<li>This is for the current user only and only for the Microsoft.PowerShell shell.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>You can test it by running the following commands (obviously file writing restrictions apply depending on the OS and the user privileges):<\/p>\n<pre style=\"padding-left: 30px;\">md %UserProfile%\\Documents\\WindowsPowerShell\\\r\nmd %windir%\\system32\\WindowsPowerShell\r\nmd %windir%\\system32\\WindowsPowerShell\\v1.0\\\r\n\r\necho echo profile1 &gt;%windir%\\system32\\WindowsPowerShell\\v1.0\\profile.ps1\r\necho echo profile2 &gt;%windir%\\system32\\WindowsPowerShell\\v1.0\\Microsoft.PowerShell_profile.ps1\r\necho echo profile3 &gt;%UserProfile%\\Documents\\WindowsPowerShell\\profile.ps1\r\necho echo profile4 &gt;%UserProfile%\\Documents\\WindowsPowerShell\\Microsoft.PowerShell_profile.ps1\r\n<\/pre>\n<p>and then run PowerShell.<\/p>\n<p>Btw. If you are wondering what these commands are doing &#8211; first 3 ensure the respective directories exist; the next 4 ones create dummy profile files with a simple command &#8216;echo xyz&#8217;, where xyz is a number of the profile. When executed for testing purposes they will simply show you which profile has been loaded by PowerShell. In a real-case scenario these would be replaced with an instruction to launch malware or could be any PowerShell command.<\/p>\n<p>Anyway, back to the test. You will most likely be surprised to see that PowerShell does not load these profiles without a fight i.e. you may see a couple of error messages.<\/p>\n<p>This is because by default the OS policy prevents executing PowerShell scripts (including the profile scripts) and one has to enable them first as documented <a href=\"http:\/\/technet.microsoft.com\/library\/hh847748.aspx\">here<\/a>.<\/p>\n<p>The Windows Registry values guarding this policy are stored under respective hkcu\/hklm branches:<\/p>\n<pre>software\\policies\\microsoft\\windows\\powershell\\<\/pre>\n<pre>         EnableScripts (REG_DWORD)\r\n         ExecutionPolicy (REG_SZ)<\/pre>\n<p>One can enforce then script execution by running the following commands (hklm may replace hkcu):<\/p>\n<pre>reg add hkcu\\Software\\Policies\\Microsoft\\Windows\\PowerShell \/f \/v EnableScripts \/t reg_dword \/d 1\r\nreg add hkcu\\Software\\Policies\\Microsoft\\Windows\\PowerShell \/f \/v ExecutionPolicy \/t reg_sz \/d Unrestricted\r\n<\/pre>\n<p>Launching PowerShell now will show the following:<\/p>\n<p><a href=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2014\/08\/powershell_profile.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-2471\" src=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2014\/08\/powershell_profile.png\" alt=\"powershell_profile\" width=\"677\" height=\"343\" srcset=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2014\/08\/powershell_profile.png 677w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2014\/08\/powershell_profile-300x151.png 300w\" sizes=\"(max-width: 677px) 100vw, 677px\" \/><\/a><\/p>\n<p>You can download a batch file that I used to test the commands <a href=\"https:\/\/hexacorn.com\/examples\/2014-08-27_powershell_profile_test.bat\">here<\/a>.<\/p>\n<p>Tested on Windows 8.1 and Windows 7.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Documenting various persistence mechanisms would not be complete without mentioning these that could be based on legitimate and fully-documented system features. One such mechanism we are going to talk about is called &#8216;custom Power Shell profile&#8217;. It is a distant &hellip; <a href=\"https:\/\/www.hexacorn.com\/blog\/2014\/08\/27\/beyond-good-ol-run-key-part-16\/\">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,15,19,9],"tags":[],"_links":{"self":[{"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/posts\/2465"}],"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=2465"}],"version-history":[{"count":6,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/posts\/2465\/revisions"}],"predecessor-version":[{"id":2474,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/posts\/2465\/revisions\/2474"}],"wp:attachment":[{"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/media?parent=2465"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/categories?post=2465"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/tags?post=2465"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}