{"id":2578,"date":"2022-08-30T15:26:02","date_gmt":"2022-08-30T15:26:02","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/02\/04\/why-i-can-not-do-sql-injection-testing-using-mysql-and-php-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:26:02","modified_gmt":"2022-08-30T15:26:02","slug":"why-i-can-not-do-sql-injection-testing-using-mysql-and-php-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/why-i-can-not-do-sql-injection-testing-using-mysql-and-php-collection-of-common-programming-errors\/","title":{"rendered":"why i can not do sql injection testing using mysql and php-Collection of common programming errors"},"content":{"rendered":"<p>i was trying to check a web site for an sql injection attack and amazed to see it not very very simple to prevent because below is the simple code .<\/p>\n<pre><code>$sql=\"select * from user_acount where login_id='\".$username.\"' and password='\".$password.\"' and status='1' \";\n<\/code><\/pre>\n<p>i can not do any sql injection to test it. i wrote the following<\/p>\n<p>1st Attempt to check sql injection<\/p>\n<pre><code>Login:  admin'--\nPassword:'i typed nothing here '\n<\/code><\/pre>\n<p>Result Wrong password you cannot login.<\/p>\n<p>2nd:<\/p>\n<pre><code>Login:  admin or 1=1 --' \nPassword:''\n<\/code><\/pre>\n<p>Result Wrong password you cannot login.<\/p>\n<p>3rd:<\/p>\n<pre><code>Login:  admin' or 1=1 \nPassword:''\n<\/code><\/pre>\n<p>4th: Login: admin or 1=1&#8242;&#8211; Password:&#8221;<\/p>\n<p>Result Wrong password you cannot login.<\/p>\n<p>Can anyone please explain what is stopping me ? i am not using prepared statements nor i am using any filter class neither i have real_escape_string ?<\/p>\n<ol>\n<li>\n<p>Two possibilities:<\/p>\n<ol>\n<li>\n<p>There is no <code>login_id<\/code> with <code>admin<\/code>, therefore the query looks like:<\/p>\n<pre><code>select * from user_acount where login_id='admin'\n<\/code><\/pre>\n<\/li>\n<li>\n<p><code>magic_quotes<\/code> have been enabled, resulting in queries like:<\/p>\n<pre><code>select * from user_acount where login_id='admin\\'--' and password='' and status='1'\nselect * from user_acount where login_id='admin or 1=1 --\\'' and password='' and status='1'\n<\/code><\/pre>\n<p>The last query will always fail, even if magic quotes was turned off:<\/p>\n<pre><code>select * from user_acount where login_id='admin or 1=1 --'' and password='' and status='1'\n<\/code><\/pre>\n<p>Because <code>--<\/code> comments within strings do not work, the query is interpreted like:<\/p>\n<pre><code>select * from user_acount where login_id='STRING'' and password='' and status='1'\n<\/code><\/pre>\n<p>As you can see, this will result in a syntax error after <code>'STRING'<\/code><\/p>\n<\/li>\n<\/ol>\n<\/li>\n<li>\n<ul>\n<li>Magic Quotes might be turned on (so user input is automatically processed to be better suited for composing database queries).<\/li>\n<li>Register Globals might be turned off (so you would have to use <code>$_REQUEST['username']<\/code> or related instead of <code>$username<\/code>).<\/li>\n<li>There might be a bug in your programm, that prevents the input <code>admin'--<\/code> from being stored in <code>$username<\/code> (e. g. form element names do not match variable names).<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>You are entering a password which includes quotes. This breaks your SQL query, and presumably the code does not distinguish between an error and a legitimate &#8220;no such user&#8221; result, so you get the wrong password message.<\/p>\n<p>Try leaving the password blank.<\/p>\n<\/li>\n<li>\n<p>You don&#8217;t actually call the <code>real_escape_string()<\/code> method, but I think <code>magic_quotes_gpc<\/code> is set to true.<\/p>\n<blockquote>\n<p>When magic_quotes are on, all &#8216; (single-quote), &#8221; (double quote), \\ (backslash) and NUL&#8217;s are escaped with a backslash automatically.<\/p>\n<\/blockquote>\n<p>Runtime Configuration &#8211; magic_quotes_gpc<\/p>\n<\/li>\n<li>\n<p>Have you tried with:<\/p>\n<pre><code>Login:  admin' or '1=1  \nPassword: admin' or '1=1\n<\/code><\/pre>\n<p>Also check for magic quotes turned on<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2014-02-04 01:42:34. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>i was trying to check a web site for an sql injection attack and amazed to see it not very very simple to prevent because below is the simple code . $sql=&#8221;select * from user_acount where login_id='&#8221;.$username.&#8221;&#8216; and password='&#8221;.$password.&#8221;&#8216; and status=&#8217;1&#8217; &#8220;; i can not do any sql injection to test it. i wrote the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2578","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2578","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2578"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2578\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2578"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2578"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2578"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}