segunda-feira, 23 de maio de 2016

Secret Life of Hashes Divulged – Length Extension Attack Explained

Sounds interesting!! Doesn’t it? Well, it sounds to me and so I want it penned down. Let’s see how this goes.
If you feel you’ve nailed it after reading through the entire blog, then I can consider diving deep into the blogging world. It’s my second post within 24 hours and I already feel I was born for it. Let’s not bore you guys with my shit, and learn something about a new form of cryptographic attack called the Hash Length Extension Attack, instead.
It all started when I was at Goa in India (an exotic place to be in for sure) a year ago for the null conference. I know, when I say Goa, the only thought that comes to your mind is the beaches – pristine blue with golden sand and perhaps a bottle of Goa’s favorite chilled beer “Kings”.  The thought of not attending the conference crossed my mind, but then my Boss gave me an intensive look and there I was, (‘Coz my employer paid for the trip). I already knew that I was going to hate it but things changed at the blink of an eye the moment I entered the conference. One of the trainings at this conference was about an attack called a Hash Length Extension Attack. This was the first time I've heard it and it kept me on my toes for the entire session.
Half way down the session, I realized that the attack is not only super awesomely cool, but also conceptually easy to understand and perform. To make your job much easier, there are various modules available over the internet such as HashPump and hash_extender. There’s another module which I like personally, is the one from Stephen Bradshaw called hlextend. This module can be found at GitHub.
As per Wikipedia, the basic definition of this attack goes by:
In cryptography and computer security, a length extension attack is a type of attack where an attacker can use Hash (message1) and the length of message1 to calculate Hash (message1 message2). This attack can be used to sign a message when a Merkle–Damgard based hash is misused as a message authentication code, allowing for inclusion of extra information.
This attack can be done on hashes with construction H (secret message) when message and the length of secret is known. Algorithms like MD5, SHA-1, and SHA-256 that are based on the Merkle–Damgard construction are susceptible to this kind of attack.”
Before we begin, a quick go-through on how Hashing works:
·         Hash functions operate on fixed block sizes
·         Input data is split into fix-sized parts equal to the block size
·         If any of these parts is smaller than block size (mostly the last part), the missing bytes are padded
ü       PKCS7
ü       Bit Padding
·         The hash value represents the internal state of the hash function
ü       You can simply continue adding data + padding and hash again using the internal state
The Attack:
Let’s assume a web application which uses a known hash generated from an unknown secret value tied to a user entered input. The basic purpose of doing so is to check the integrity of the entered value to avoid any form of manipulation. For example, let’s assume the desired SHA1 hash value generated with an unknown secret length for a valid parameter value ‘student’ is ‘204036A1EF6E7360E536300EA78C6AEB4A9333DD’. So, if we try to access the below mentioned URL, we get all the details of a student.


Cool! I guess that’s what the application was supposed to do. It’s a valid request after all. Let’s see what happens if we try to play with the ‘param’ value. Let’s try to access the ‘/etc/passwd/’ file using path traversal. What do you think will happen?










Oops!! File not found.
Of course! And did you think it was that easy? So what do you think happened? 
Well, the requested file did not match the above SHA1 hash value since it belonged to ‘student’. In order to perform this attack successfully, we would need a valid SHA1 hash value for the ‘/../../../../etc/passwd’ file, combined with an unknown length of the secret value.
And how do you think we get that? Well, the first thing we need to do is to guess the length of the secret value. Without which it won’t be possible to generate a valid hash. In such cases, brute forcing the value by trying multiple different lengths can sometimes be possible, depending on the application.
Let’s first see what all data we already have that might come in handy:
Known Data: student
Hash of known Data: 204036A1EF6E7360E536300EA78C6AEB4A9333DD
Extended Data: /../../../../etc/passwd
Hash of Extended Data: <unknown>
Length of secret: <unknown>
Great!!
If I have to explain it in simple words, the application creates a sha1 hash value of '204036A1EF6E7360E536300EA78C6AEB4A9333DD', by concatenating an unknown secret of length ‘X’ and known data of 'student'. You wish to append the text '/../../../../etc/passwd' after 'student' and also provide a valid hash back to the application that it will produce when it concatenates your provided value (which will include the string 'student' followed by '/../../../../etc/passwd') with its secret.
I hope by now you already know what our ultimate goal is. Ready for some action!!! Here we go……
The goal is to read the /etc/passwd file, via bypassing the hash integrity check on the param parameter. This is what the script below will automate using the hlextend module to generate the extended hash values.
And the output:


Once we access the above displayed URL:


You may notice that the new value produced above contains a lot of additional data (in this case, a 0x80 followed by a number of 0x00s and a message length, which is the length of the key plus the original message, appended at the end) between the 'student' and the '/../../../../etc/passwd' - this is hex encoded padding data used by the hash algorithm that needs to be integrated into the hashed data in order for the attack to work - So strictly speaking you can't specify the EXACT value to append, only what comes after the padding, but under the right circumstances you can make the application ignore this extra padding.
So in short, if the hash algorithm used is vulnerable, it is possible to achieve this without knowing the secret value as long as you know (or can guess, perhaps by brute force) the length of that secret value. This is called a hash length extension attack.

Happy Hacking my Friends!!!

quarta-feira, 18 de maio de 2016

Kali linux: BareMetal, Virtualized or USB Stick




Few days ago, we had a discussion about what is the best approach for Kali Linux installation.
A lot of guys complain about the performance in virtualized installations - I have one word for that: BULLSHIT!

I have almost 10 years of pentesting/security experience and I have never problems with virtualized installation, except for some years ago with Wi-Fi dongles (We will discuss it later).
Based in my observations to where and when to install, here are few ideas to follow:

- New in Linux:
If you are new, you should not start with Kali, go back and read this article first: http://blog.dclabs.com.br/2016/05/wanna-be-pentester.html and related links.
From this point I will assume that you have Linux and security skills.
As was described in the link above, Kali is not for general purpose or day by day distro, so, here is the first point:

- As Kali is not a day by day distro, some applications like Skype, Flash player and others are not supported by Kali Team, in other words, non-official/external repositories can be added in your Kali in order to install tools/packets.
Also, in Kali there are tools like calendars, agenda and stuff, but these tools are not priority.
In case of problems with this kind of application, for sure you have to wait a long time to get it fixed.

Obliviously, the best approach is to use Kali virtualized in your daily distro or Windows.

But before installing it, please RTFM (Read the Fucking Manual)! Is very common for people to complain about errors during the installation and performance. Of course, they are not following the basic recommendations. http://docs.kali.org/installation/kali-linux-hard-disk-install - Check all requirements before starting!

If you are planning to use Gnome as your default desktop environment, you need more RAM memory.

I recommend XFCE, because you are a Pentester and not a Designer.
If you want something cute, forget Kali and install HannaMontana Linux. http://hannahmontana.sourceforge.net/ -> Just to help you! (:

- Performance:
If you follow the requirements, the likelihood of facing problems is very low. Nowadays, machines have a very powerful processors, very good memory and disk size, so it is easy to allocate some of these resources to a VM. However, if your machine does not have good hardware specifications (maybe it is time to throw it away and buy a new one) I recommend the bare metal installation.

This way kali is able to use the complete machine hardware resources and it will not be shared with another OS.

- System restoring and backup
If you have minimum skills regarding virtualization, you know that making virtual machines backup/restore is easier than a bare metal installation - you only need to copy a couple of files and that´s it.

Also, you can revert to the last snapshot, the system will be up in few minutes or less.
For bare metal breaking, you need to check logs, files, disk healthy and stuff. Or, in the worst case, install the system again. It’s really not a good thing if you are inside a customer for example.

-Mobility
If you have a dedicated hardware to perform pestest for customers, is a good idea to have it bare metal. However, is possible to face some hardware compatibility problems, it will be discussed in the next topic.

Nevertheless, if you don’t have a dedicated hardware and carrying a notebook is not an option, you can use OVF format (https://en.wikipedia.org/wiki/Open_Virtualization_Format).
Just put the file in a pendrive and load it in the Vbox or Vmware for example. 

Another option is to create a bootable pendrive, also,  the mobility in this case is easier than carrying a notebook in the bag all the time.

Following the links related to USB install:
http://docs.kali.org/downloading/kali-linux-live-usb-install
http://docs.kali.org/downloading/kali-linux-live-usb-persistence

One more, but not powerful option is to install Kali in your mobile device, there is a especial Kali edition called NetHunter (https://www.kali.org/kali-linux-nethunter/) for this purpose.
Also, you can proceed with a chroot installation (https://www.kali.org/tutorials/kali-linux-android-linux-deploy/)

This last two cases are very useful when you are performing a pentest with very restrict devices access. I mean, when the customer does not allow you to go inside the company with your notebook. Normally mobiles devices are allowed and pendrives are very easy to hide.

-Hardware compatibility
If you dont wanna face hardware problems, the best way is to use virtual system. 
Only one problem in my point of view (and it not is that problem):
You are not able to use your GPU to crack passwords. 

You still able to use virtualized to crack, but for sure not the same speed.
However, the passwords can be cracked in your normal Linux/windows installation, don’t try to use this "problem" as excuse to have Kali bare metal installed and look cool for your friends.

For Wi-Fi penetration tests, some years ago I had some problems because VMware and Vbox didn’t have a nice USB management, nowadays, it already fixed.
Basically 99% of the problems with Wi-Fi pentesting are because the Wi-Fi card does not support packet injection, so, check it before buying the Wi-Fi adapter.

Conclusion:

A virtualized Kali will work pretty nice for you, covering almost all the cases.
If it is not working with satisfactory performance, maybe you did something wrong.

Probably I didn’t cover all possibilities ins the article, but I hope it helps you to choose the best way to install your Kali.