{"id":10364,"date":"2026-05-06T09:12:36","date_gmt":"2026-05-06T09:12:36","guid":{"rendered":"https:\/\/www.hexacorn.com\/blog\/?p=10364"},"modified":"2026-05-06T09:12:36","modified_gmt":"2026-05-06T09:12:36","slug":"1-little-known-secret-of-forfiles-exe-part-2","status":"publish","type":"post","link":"https:\/\/www.hexacorn.com\/blog\/2026\/05\/06\/1-little-known-secret-of-forfiles-exe-part-2\/","title":{"rendered":"1 little known secret of forfiles.exe, part 2"},"content":{"rendered":"\n<p>In this <a href=\"https:\/\/www.hexacorn.com\/blog\/2023\/12\/31\/1-little-known-secret-of-forfiles-exe\/\" data-type=\"post\" data-id=\"9021\">old post<\/a> I have demonstrated how to abuse <em>forfiles.exe<\/em> to run your &#8216;cmd.exe&#8217; of choice.<\/p>\n\n\n\n<p>There is one more trick we can do with this tool.<\/p>\n\n\n\n<p>When<em> forfiles.exe<\/em> enumerates the files it executes a default command <em>cmd \/c echo @file<\/em>. It turns out that the execution of this command expands environment variables as well&#8230;<\/p>\n\n\n\n<p>So&#8230;<\/p>\n\n\n\n<p>If we create a file called <em>%foo%<\/em>, and make sure that there is an environmental variable called <em>foo, <\/em>we can now control the <em>cmd \/c echo @file<\/em> command and force it to do some unusual stuff.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n\n<ul>\n<li>create a file called <em>%foo%<\/em><\/li>\n\n\n\n<li>set <em>foo <\/em>to <em>&#8220;&amp;calc.exe&amp;&#8221;<\/em><\/li>\n\n\n\n<li>run <em>forfiles<\/em><\/li>\n<\/ul>\n\n\n\n<p>&#8211; it will enumerate files in a directory, print their names, and when it will come across the <em>%foo%<\/em> file, it will also &#8230; execute calculator&#8230;<\/p>\n\n\n\n<p>This happens because <em>cmd \/c echo @file<\/em> gets expanded to <em>cmd \/c echo %foo%<\/em> which in turn will be executed as &#8220;echo <em>&#8220;&amp;calc.exe&amp;&#8221;<\/em>&#8220;.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this old post I have demonstrated how to abuse forfiles.exe to run your &#8216;cmd.exe&#8217; of choice. There is one more trick we can do with this tool. When forfiles.exe enumerates the files it executes a default command cmd \/c &hellip; <a href=\"https:\/\/www.hexacorn.com\/blog\/2026\/05\/06\/1-little-known-secret-of-forfiles-exe-part-2\/\">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":[126,56,64],"tags":[],"_links":{"self":[{"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/posts\/10364"}],"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=10364"}],"version-history":[{"count":2,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/posts\/10364\/revisions"}],"predecessor-version":[{"id":10366,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/posts\/10364\/revisions\/10366"}],"wp:attachment":[{"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/media?parent=10364"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/categories?post=10364"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/tags?post=10364"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}