{"id":3635,"date":"2016-05-30T11:50:38","date_gmt":"2016-05-30T11:50:38","guid":{"rendered":"http:\/\/www.hexacorn.com\/blog\/?p=3635"},"modified":"2016-05-30T11:50:38","modified_gmt":"2016-05-30T11:50:38","slug":"beyond-good-ol-run-key-part-39","status":"publish","type":"post","link":"https:\/\/www.hexacorn.com\/blog\/2016\/05\/30\/beyond-good-ol-run-key-part-39\/","title":{"rendered":"Beyond good ol\u2019 Run key, Part 39"},"content":{"rendered":"<p>In my previous post in this series I talked about the Logon scripts. This nice concept of being able to run some code when system-wide, or user-wide events happen is very useful in a managed environment.<\/p>\n<p>The Logon event is one of a number of events that can be recognized by the system and as a response, an appropriate script can be executed. Other events include Logoff, Startup and Shutdown events. Apart from the already described mechanism, one can rely on Group Policies and use GPEDIT.MSC to set up event-aware scripts on the local system.<\/p>\n<p>For example:<\/p>\n<ul>\n<li>Startup and Shutdown<\/li>\n<\/ul>\n<p><a href=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/pic1-1.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-3636\" src=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/pic1-1-300x170.png\" alt=\"pic1\" width=\"500\" height=\"284\" srcset=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/pic1-1-300x170.png 300w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/pic1-1.png 653w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<ul>\n<li>Logon and Logoff<\/li>\n<\/ul>\n<p><a href=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/pic2-1.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-3637\" src=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/pic2-1-300x170.png\" alt=\"pic2\" width=\"500\" height=\"284\" srcset=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/pic2-1-300x170.png 300w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/pic2-1.png 653w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p>(note, the screenshots are from XP cuz it&#8217;s holiday and I am lazy, but same applies to newer versions of Windows)<\/p>\n<p>Once the appropriate Event is selected f.ex. Startup:<\/p>\n<p><a href=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/pic3-1.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-3638 size-full\" src=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/pic3-1.png\" alt=\"pic3\" width=\"404\" height=\"455\" srcset=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/pic3-1.png 404w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/pic3-1-266x300.png 266w\" sizes=\"(max-width: 404px) 100vw, 404px\" \/><\/a><\/p>\n<p>We can add the script using a dialog box:<\/p>\n<p><a href=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/pic4-1.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-3639 size-full\" src=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/pic4-1.png\" alt=\"pic4\" width=\"384\" height=\"195\" srcset=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/pic4-1.png 384w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/pic4-1-300x152.png 300w\" sizes=\"(max-width: 384px) 100vw, 384px\" \/><\/a><\/p>\n<p>We can also repeat the sequence and add a number of scripts:<\/p>\n<p><a href=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/pic6.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-3641 size-full\" src=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/pic6.png\" alt=\"pic6\" width=\"404\" height=\"455\" srcset=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/pic6.png 404w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/pic6-266x300.png 266w\" sizes=\"(max-width: 404px) 100vw, 404px\" \/><\/a><\/p>\n<p>The system stores the scripts in the following location:<\/p>\n<ul>\n<li>c:\\WINDOWS\\System32\\GroupPolicy\\Machine\\Scripts<\/li>\n<\/ul>\n<p><a href=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/pic5.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-3640\" src=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/pic5-300x120.png\" alt=\"pic5\" width=\"501\" height=\"200\" srcset=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/pic5-300x120.png 300w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/pic5-768x307.png 768w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/pic5.png 769w\" sizes=\"(max-width: 501px) 100vw, 501px\" \/><\/a><\/p>\n<p>Inside the folder we can see the following files:<\/p>\n<ul>\n<li>The config file:\n<ul>\n<li>c:\\WINDOWS\\System32\\GroupPolicy\\Machine\\Scripts\\scripts.ini<\/li>\n<\/ul>\n<\/li>\n<li>and our scripts:\n<ul>\n<li>c:\\WINDOWS\\System32\\GroupPolicy\\Machine\\Scripts\\Startup\\Startup1.bat<\/li>\n<li>c:\\WINDOWS\\System32\\GroupPolicy\\Machine\\Scripts\\Startup\\Startup2.bat<\/li>\n<li>c:\\WINDOWS\\System32\\GroupPolicy\\Machine\\Scripts\\Shutdown\\Shutdown1.bat<\/li>\n<li>c:\\WINDOWS\\System32\\GroupPolicy\\Machine\\Scripts\\Shutdown\\Shutdown2.bat<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>Note: I have added 2 scripts for each event.<\/p>\n<p>The content of the files is as follows:<\/p>\n<p><a href=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/pich.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-3642\" src=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/pich-300x142.png\" alt=\"pich\" width=\"500\" height=\"236\" srcset=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/pich-300x142.png 300w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/pich.png 669w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p>And the same story for Logon\/Logoff events:<\/p>\n<ul>\n<li>The config file:\n<ul>\n<li>c:\\WINDOWS\\System32\\GroupPolicy\\User\\Scripts\\scripts.ini<\/li>\n<\/ul>\n<\/li>\n<li>and our scripts:\n<ul>\n<li>c:\\WINDOWS\\System32\\GroupPolicy\\User\\Scripts\\Logoff\\Logoff1.bat<\/li>\n<li>c:\\WINDOWS\\System32\\GroupPolicy\\User\\Scripts\\Logoff\\Logoff2.bat<\/li>\n<li>c:\\WINDOWS\\System32\\GroupPolicy\\User\\Scripts\\Logon\\Logon1.bat<\/li>\n<li>c:\\WINDOWS\\System32\\GroupPolicy\\User\\Scripts\\Logon\\Logon2.bat<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>with the content as follows:<\/p>\n<p><a href=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/picf.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-3643\" src=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/picf-300x144.png\" alt=\"picf\" width=\"500\" height=\"240\" srcset=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/picf-300x144.png 300w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/picf.png 669w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><br \/>\nIf we restart the computer now (making all the events happen: Logoff, Shutdown, Startup, Logon), we can observe that our scripts are all executed one by one &#8211; as indicated by the &#8216;trace&#8217; files created by each script:<\/p>\n<p><a href=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/pice.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-3644\" src=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/pice-300x152.png\" alt=\"pice\" width=\"501\" height=\"253\" srcset=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/pice-300x152.png 300w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/pice.png 669w\" sizes=\"(max-width: 501px) 100vw, 501px\" \/><\/a>I also temporarily modified the startup script to pause before continuing &#8211; just to show that the scripts are executed by userinit.exe:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-3653 size-full\" src=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/pic8.png\" alt=\"pic8\" width=\"460\" height=\"580\" srcset=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/pic8.png 460w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/pic8-238x300.png 238w\" sizes=\"(max-width: 460px) 100vw, 460px\" \/><\/p>\n<p>Trivia fact: while preparing the post I noticed that an old autoruns present on my test VM shows some of the scripts incorrectly i.e. under the HKLM\\&#8230;\\Run key:<\/p>\n<p><a href=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/picj.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-3645\" src=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/picj-300x168.png\" alt=\"picj\" width=\"500\" height=\"280\" srcset=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/picj-300x168.png 300w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/picj-768x430.png 768w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/picj.png 1010w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p>The latest version works like a charm&#8230; or not \ud83d\ude42 &#8211; note that scripts are duplicated and Logoff event shows 6 entries instead of 2:<\/p>\n<p><a href=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/picm.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-3646\" src=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/picm-300x174.png\" alt=\"picm\" width=\"500\" height=\"290\" srcset=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/picm-300x174.png 300w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/picm-768x445.png 768w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/picm.png 937w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a>If in doubt, we can always visit the Registry entries where the information about these 4 events is stored:<\/p>\n<p><a href=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/picl.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-3647\" src=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/picl-300x136.png\" alt=\"picl\" width=\"500\" height=\"227\" srcset=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/picl-300x136.png 300w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/picl-768x348.png 768w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/picl.png 894w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/pick.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-3648\" src=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/pick-300x121.png\" alt=\"pick\" width=\"500\" height=\"202\" srcset=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/pick-300x121.png 300w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/pick-768x310.png 768w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/pick.png 894w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p>Inspecting the Registry entries we can notice that the &#8230;\\Scripts\\Logon\\&lt;number&gt;\\FileSysPath properties can be modified:<\/p>\n<p><a href=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/picr.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-3649\" src=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/picr-300x149.png\" alt=\"picr\" width=\"500\" height=\"248\" srcset=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/picr-300x149.png 300w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/picr.png 626w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p>In my case I modify it to c:\\test and I am recreating the Logon\/Logoff scripts folder structure there:<\/p>\n<p><a href=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/pico.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-3650 size-full\" src=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/pico.png\" alt=\"pico\" width=\"358\" height=\"161\" srcset=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/pico.png 358w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/pico-300x135.png 300w\" sizes=\"(max-width: 358px) 100vw, 358px\" \/><\/a><\/p>\n<p>Luckily, autoruns picks it up too:<\/p>\n<p><a href=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/picp.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-3651\" src=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/picp-300x175.png\" alt=\"picp\" width=\"500\" height=\"291\" srcset=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/picp-300x175.png 300w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/picp-768x448.png 768w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2016\/05\/picp.png 923w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p>Last, but not last &#8211; if such entries already exist on the system, an attacker could simply append some commands to existing scripts, or hijack execution of existing commands using many of existing tricks (f.ex. path interception, path companion, etc.).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my previous post in this series I talked about the Logon scripts. This nice concept of being able to run some code when system-wide, or user-wide events happen is very useful in a managed environment. The Logon event is &hellip; <a href=\"https:\/\/www.hexacorn.com\/blog\/2016\/05\/30\/beyond-good-ol-run-key-part-39\/\">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,46,9],"tags":[],"_links":{"self":[{"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/posts\/3635"}],"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=3635"}],"version-history":[{"count":2,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/posts\/3635\/revisions"}],"predecessor-version":[{"id":3654,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/posts\/3635\/revisions\/3654"}],"wp:attachment":[{"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/media?parent=3635"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/categories?post=3635"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/tags?post=3635"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}