Sunday, March 5, 2017

Memory leakage in 2-tier applications

The 2-tier applications use front-end to directly communicate to DB. There's no separate business logic tier. All the business logic are at client side. Thick client applications (mostly) are classic examples of that. Applications developed in .Net and Java could be found in big nos. inside any organization. Sometimes it's difficult to straightaway move to 3-tier architecture. Businesses are reluctant to accept this approach due to:
- Moving towards 3-tier involves a great amount of coding efforts and  money.
- Sometimes the applications are almost end of life and are not being retired just because of there;s no good reason to do so. 
- Most of the above applications are Intranet applications. Business claims that being an internal application, this is less susceptible to attack.

But they forget one very big risk under these claims- sensitive information in memory dumps. 

The application being 2-tier connects to DB while constructing the connection string using DB credentials, server name, DB name, port no. etc. These are at risk of being exposed when the client wants to connect to DB. 

But how an attacker can get the dump? He needs to have physical access to the victim's machine. So it's impossible. But we are not talking about an external attacker, we are talking about if the valid user of the application himself turns malicious. He can take the memory dump and find grab the sensitive information such as DB connection string. The soul of an application, ie, DB is compromised, bypassing the business layer. The business doesn't understand this.

So, any fix? There's no fix! As long as the app is 2-tier, there's no fix. However there are compensating controls- make it hard to grab the info from memory. 
- SecureString class- https://msdn.microsoft.com/en-/library/system.security.securestring(v=vs.110).aspx 
But only supported for .Net 4.5 and above. Represents text that should be kept confidential, such as by deleting it from computer memory when no longer needed. This class cannot be inherited.

- Write own code to overwrite the memory location- garbage collection. I saw a code by one developer which does it, to avoid password being exposed in memory. But you have to construct the connectionstring at some point of time, even momentarily, so that the app will connect to the DB and then quickly overwrite that. This reduces the attack window by a large extent- but not fixes it.

There's no real fix than moving to 3-tier architecture.For  2-tier we have to accept the risk.

Sunday, February 19, 2017

File Upload through Null Byte Injection continued...

Just after writing the previous post, we came across a scenario where the application was expecting a pdf and back-end was php. But the application was not accepting the Null Byte injected files as described in last post.
We found that it was validating PDF magic no. and application types in headers.
So, we repeated the the exact steps of the last post and additionally we changed the signature and content type and the application uploaded it successfully.
So we renamed our file shell.php as shell.phpA.pdf and replaced A with Null, so the strings became shell.php[NULL] .pdf, which the interpreter read and created a file shell.php on the server. Only issue is that there's not code to execute since all the contents were pdf contents. But our aim was to bypass this and it was successful. This post concludes.

Saturday, January 14, 2017

File Upload through Null Byte Injection

Sometimes, during file upload we come across situation wherein there would be check on the file extension at the client side as well as server side too. If the application does allow only .jpeg extension to be uploaded, the client side java script checks for the extension of the file before passing the request. We all know that how easily this can be defeated.
Some applications, checks for the extension at the server side also. That's not easy to bypass. However there are some ways with which it still can be bypassed. Most of server side scripts are written in high level languages such as Php, Java etc who still use some C/C++ libraries to read the file name and contents. That leads to the problem. In C/C++ a line ends with /00 or which is called Null Byte. So whenever the interpreter sees a null byte at the end of the a string, it stops reading thinking it has reached at the end of the string.
This can be used for the bypass. It works for many servers, specially php servers. The high level concept is like:

1. Create a php file with any backdoor code. Numerous examples are out there on Internet.
2. Now this code PHP code needs to be merged into the .jpeg file. There are a few command line tools available on Kali, which can do it. Suppose the resultant file is Happy.jpeg. This file contains the php backdoor code also. But the we can not execute the php commands inside the Happy.jpeg file since it's and jpeg extension which the php interpreter will not understand.
3. Now here the Null byte injection works. Change the name of Happy.jpeg to Happy.phpA.jpeg. We can choose 'A', 'B' any letter. Since we all are more familiar with hex of  'A' as 41, we use that for simplicity.
4. Now upload the file. Capture the request in the Burp, switch to the Hex view. Locate the file name in the string view. See the corresponding Hex table and replace the 41 ('A') with 00 (which is null byte).
5. Now the resultant string becomes Happy.php(space) .jpeg. Now since php interpreter uses C libraries internally, it's going to stop reading the file name post Happy.php and the file will be saved as Happy.php.
6. Now thus file can be accessed depending on the path of the upload directory along with the domain name. Eg, http://domain.com/images/Happy.php and all the commands can be executed.


Friday, December 9, 2016

Installing Burp cert in Android

I have seen many a times, even though there's already a Burp cert is installed on the Android device, the browser throws an error and Burp does not capture the request. This issue is mostly prevalent with Kitkat 4. As browser errors can be bypassed by clicking Proceed, but Banking apps keep throwing 'SSL Error' messages. Not sure what happens, but below works for me in such cases:

1. Download the Burp certificate. It'll be downloaded as 'cert.der'

2. Go to download folder, rename it as 'cert.cer' . Recommend to have a file explorer app which makes renaming easy.

3. Copy it to /storage/sdcard0 folder.

4. Go to Settings-> Security-> Trusted Credentials-> User. Remove the old Portswigger (Burp) certificates.

5. Go to Settings-> Security-> Install from device storage. Tap it and it installs your new certificate automatically. Now you are done. You'll notice all well now, the app communications being intercepted by Burp.

Maybe due to some phone models are OS versions, above issues happen. But this fix work for me.

Friday, November 18, 2016

jtool - an alternative to otool

jtool comes with a capability of running on Linux environment. Some ipa scanning tools are created to run on Linux environment where mac environment is not available. In such cases tools such as otool and class-dump-z will not work. So jtool can be an alternative to otool. For more information on jtool please refer to http://www.newosxbook.com/tools/jtool.html . It lists down various commands which have same output as otool or a equivalent. There are several commands mentioned in link.

But for our customized requirements and basis checks I have listed down the below ones after running on many binaries. The outputs are similar or equivalent to otool and class-dump-z:

Commands for checking PIE flag (ASLR) in jTool
jtool -d -v -arch | grep stack

·         Automatic Reference Counting (ARC) protection:
jtool -d -v -arch | grep _objc_release

·         To check if the device is jailbroken:
jtool -d -v -arch | grep jail

·         Dyldinfo compatible options:
jtool -function_starts  -v -arch -d objc arm64 --- prints the classes and interfaces with start addresses, not as compact and clean as class-dump-z output

·         Signtaure:
jtool --sig -arch arm64 --provides info about flag/ version/ Identifier, CDHash, CA information

·         Entitlements:
jtool --ent   -arch  arm64

·         Dumping the binary:
jtool -d

Tuesday, October 18, 2016

SQLi and Blind SQLi in search field

This continues from my earlier posts on SQLi in search fields:
http://nileshkumar83.blogspot.sg/2012/10/sql-injection-in-search-field.html

I recently found one sqli which was both in nature- generic sqli and blind sqli. As I have already said, search boxes are always an unusual suspect and we tend to generally overlook them.

But the field was not vulnerable to simple queries like ' or ''=', would have missed it if would not have tried similar to ' or ''='' or ''='. I fired Burp intruder and got a couple of more payloads which worked. Still I am not able to understand the difference between these two queries, why they yield different results.

Anyways, the sqli got exploited and the app displayed all the records from the table.

Now turn for blind sqli:
The same field was also vulnerable to blind sqli. This became more important as the automated tools such as SQLmap, failed due to some errors or the apps being unstable. So it is purely manual efforts to extract some information about the DB.

The following query greatly helped:
param= ' or ascii(substring('sqlquery',1,1))>/=ASCII value of characters or ''='

A true result for the query will return the page with some records. Here the sqlquery can be replaced with any query which can provide information about DB. For example,
ascii(substring((Select USER from DUAL),1,1))>47 , 47 is ascii equivalent of  /. After this alphabets and numerical start.

So, one by one asking the queries based on true false conditions we were able to extract DB username, Current DB name, Hostname etc. Since this was a manual and slow effort, a few proofs like them were enough to prove that Blind SQLi is possible.


Friday, September 16, 2016

Resizing VM space on MacOs

Run the following commands:

First go the path of your vdi:


/var/root/VirtualBox VMs/Linux/Linux.vdi

and then run this command:
VBoxManage modifyhd Linux.vdi --resize

Output will be similar to this:
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
and you are done.

Sometimes you require superuser privileges to go into above directories. Just type suod -s, it'll present a bash shell, using which you should be able to enter the VM directory and then follow the above commands. Should be easy.