{"id":9271,"date":"2006-11-22T19:49:00","date_gmt":"2006-11-22T19:49:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2006\/11\/22\/protecting-against-malicious-code-injection\/"},"modified":"2019-02-18T13:21:09","modified_gmt":"2019-02-18T20:21:09","slug":"protecting-against-malicious-code-injection","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/protecting-against-malicious-code-injection\/","title":{"rendered":"Protecting Against Malicious Code Injection"},"content":{"rendered":"<p>If you are writing scripts that accept input from users &#8211; you&nbsp;need to be aware of the potential dangers of Malicious Code-Injection.&nbsp;&nbsp;Below is a good good article on this topic:<\/p>\n<p><a href=\"http:\/\/www.site-reference.com\/articles\/Website-Development\/Malicious-Code-Injection-It-s-Not-Just-for-SQL-Anymore.html\">http:\/\/www.site-reference.com\/articles\/Website-Development\/Malicious-Code-Injection-It-s-Not-Just-for-SQL-Anymore.html<\/a><\/p>\n<p>Here is a line from that article:&nbsp;<\/p>\n<blockquote>\n<p>All code injection attacks work on the same principle: a hacker piggybacks malicious code onto good code through an input field in the application. Therefore, the protection instead has to come from the code within the application itself. <\/p>\n<\/blockquote>\n<p>The basic idea is that you write a program or script which takes input from the user and then uses that input in a command.&nbsp; An evil user pjrovides input which includes&nbsp;a statement terminator and then an evil command.&nbsp; If the program\/script doesn&#8217;t protect against this &#8211; bad things can and do happen.&nbsp; <\/p>\n<p>The historical problem with protecting against code injection is that&nbsp;it relies upon the scripter to do this work and it is sometimes&nbsp;difficult to write this protection code.&nbsp; PowerShell&#8217;s parser was written to specifically protect you against Malicious&nbsp;Code Injection attacks (with no work on your part!).&nbsp; There is one exception (&#8220;Invoke-Expression&#8221;) that you need to be aware of and treat with the utmost of respect.&nbsp;&nbsp;<\/p>\n<p>Remember that the semicolon (&#8220;;&#8221;)&nbsp;is PowerShell&#8217;s statement seperator.&nbsp; The following illustrates how that works:<br \/>PS&gt; Write-Host Message1; Write-Host Message2<br \/>Message1<br \/>Message2<\/p>\n<p>When the parser sees the &#8220;;&#8221; it considers that the end of the first command and starts to parse the second command and then executes both of them.&nbsp; For the next examples, I&#8217;ll try to get PowerShell to run the evil command:&nbsp; &#8220;Write-Host Busted&#8221;.&nbsp; Image that I have a script which takes user input and assigns it to $x and then uses $x as a parameter to a command (in this case we&#8217;ll just use the Write-Host command but the principle works everywhere).<\/p>\n<p>PS&gt; $x = &#8220;this is a test&#8221;<br \/>PS&gt; Write-host $x<br \/>this is a test<\/p>\n<p>Now imagine a evil user tries to use inject some code:<\/p>\n<p>PS&gt; $x=&#8221;Message1&#8243; ; Write-Host $x<br \/>Message1<br \/>PS&gt; $x=&#8221;Message1;Write-Host Busted&#8221; ; Write-Host $x<br \/>Message1;Write-Host Busted<\/p>\n<p>Notice that&nbsp;it did not work &#8211; the entire string (statement terminator and all) was output.&nbsp; The reason for this is that PowerShell&#8217;s parser binds the value to the parameter &#8211; it doesn&#8217;t replace the text in-line and THEN do the parse.&nbsp; If you think about it &#8211; it has to.&nbsp; Many of PowerShell parameters are OBJECTS (i.e. not strings) therefore it cannot do put the text in-line and parse &#8211; there is no text to put in-line.<\/p>\n<p>NOTE:&nbsp; The exception to this rule is INVOKE-EXPRESSION.&nbsp; This will expand all variables and then do the parse (and thus is subject to&nbsp;Code Injection attacks).&nbsp;&nbsp; Now Invoke-Expression can be extraordinarily powerful so it is not that you want to never use it but you&nbsp;need to&nbsp;be VERY VERY careful about using it.&nbsp; In particular, you are probably on safe ground if the data ONLY comes from the program itself.&nbsp; If you include any data provided from the user &#8211; you need to protect yourself from Code Injection.&nbsp; Here is an example of the danger.<\/p>\n<p>PS&gt; $x=&#8221;Write-Host Message1;Write-Host Busted&#8221; ; Invoke-Expression $x<br \/>Message1<br \/>Busted<\/p>\n<p>The bottom line to all of this is that PowerShell&#8217;s Parser provides intrinsic protection for a class of problems that use to be difficult to protect against.&nbsp; We often get asked about the security of PowerShell and I point out a couple things:<\/p>\n<ul>\n<li>First is that we are super humble about security.&nbsp;&nbsp;The bad guys continue to get smarter so while we&#8217;ve done a ton of work and raised the bar signficantly, we&#8217;ll always invest in security.<\/li>\n<li>Second is that this project was started AFTER Bill&#8217;s famous TrustWorthy Computing Memo so everyone got trained on security and&nbsp;everything was done with an eye towards security.<\/li>\n<li>Third is that our security &#8220;buddy&#8221; is superstar Michael Howard ( <a href=\"http:\/\/www.microsoft.com\/mspress\/books\/5957.aspx\">http:\/\/www.microsoft.com\/mspress\/books\/5957.aspx<\/a>&nbsp;).&nbsp; You just don&#8217;t get any better than that.&nbsp; Full stop.<\/li>\n<\/ul>\n<p>Anyway &#8211; there are lots of visible security features\/elements in PowerShell (Secure by default, Execution Policies, Digitial Sigatures, warnings on scripts that get downloaded from the internet, double-clicking a script invokes notepad, etc, etc, etc) but there is also&nbsp;a ton of non-visible security features\/elements as well. The way our parser protects scripts from malicious code injection is just one of those features.&nbsp; <\/p>\n<p>&nbsp;Enjoy!<\/p>\n<p>Jeffrey Snover [MSFT]<br \/>Windows PowerShell\/MMC Architect<br \/>Visit the Windows PowerShell Team blog at:&nbsp;&nbsp;&nbsp; <a href=\"http:\/\/blogs.msdn.com\/PowerShell\">http:\/\/blogs.msdn.com\/PowerShell<\/a><br \/>Visit the Windows PowerShell ScriptCenter at:&nbsp; <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\">http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you are writing scripts that accept input from users &#8211; you&nbsp;need to be aware of the potential dangers of Malicious Code-Injection.&nbsp;&nbsp;Below is a good good article on this topic: http:\/\/www.site-reference.com\/articles\/Website-Development\/Malicious-Code-Injection-It-s-Not-Just-for-SQL-Anymore.html Here is a line from that article:&nbsp; All code injection attacks work on the same principle: a hacker piggybacks malicious code onto good code [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-9271","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"blog_post_summary":"<p>If you are writing scripts that accept input from users &#8211; you&nbsp;need to be aware of the potential dangers of Malicious Code-Injection.&nbsp;&nbsp;Below is a good good article on this topic: http:\/\/www.site-reference.com\/articles\/Website-Development\/Malicious-Code-Injection-It-s-Not-Just-for-SQL-Anymore.html Here is a line from that article:&nbsp; All code injection attacks work on the same principle: a hacker piggybacks malicious code onto good code [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/9271","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=9271"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/9271\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=9271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=9271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=9271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}