Category Archives: tricks

Set Environment Variables in Memory (JAVA)

Potentially evil snippet of code that could help me solve a long-standing problem that I encountered in developing one simple JAVA based exploit. Posting it here for further investigation when I have the time!

protected static void setEnv(Map newenv)
{
  try
    {
        Class processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment");
        Field theEnvironmentField = processEnvironmentClass.getDeclaredField("theEnvironment");
        theEnvironmentField.setAccessible(true);
        Map env = (Map) theEnvironmentField.get(null);
        env.putAll(newenv);
        Field theCaseInsensitiveEnvironmentField = processEnvironmentClass.getDeclaredField("theCaseInsensitiveEnvironment");
        theCaseInsensitiveEnvironmentField.setAccessible(true);
        Map cienv = (Map)     theCaseInsensitiveEnvironmentField.get(null);
        cienv.putAll(newenv);
    }
    catch (NoSuchFieldException e)
    {
      try {
        Class[] classes = Collections.class.getDeclaredClasses();
        Map env = System.getenv();
        for(Class cl : classes) {
            if("java.util.Collections$UnmodifiableMap".equals(cl.getName())) {
                Field field = cl.getDeclaredField("m");
                field.setAccessible(true);
                Object obj = field.get(env);
                Map map = (Map) obj;
                map.clear();
                map.putAll(newenv);
            }
        }
      } catch (Exception e2) {
        e2.printStackTrace();
      }
    } catch (Exception e1) {
        e1.printStackTrace();
    } 
}
Advertisements

Virtual Wireless Access Point with VPN on DD-WRT

I have received many request for more in-depth information on the post that I had on flashing DD-WRT and setting up a virtual AP that has traffic automatically tunnelled via a VPN service such as OpenVPN. I have recently flashed an Asus RT-68U router and have recorded the exact steps taken to do so (in the likely event I end up forgetting). If you have a different router make and model, the goal would be to research how to flash DDWRT on it and then follow step 2 in the later part of this post.

Step 1 – Flashing DDWRT on Asus RT-68U:

– Asus RT-68U on stock “Merlin” firmware, proceed to clear NVRAM via telnet:
– Enable Telnet by going to Advanced Settings -> Administration -> System -> Enable Telnet
– telnet 192.168.1.1 (or whatever is your router’s IP)

Run commands:

mtd-erase2 nvram;
reboot;

– Flash asus_rt-ac68u-firmware_30709.trx (Brainslayer build)
– Reboot the router
– Navigate to Administration -> Commands tab and run:

erase nvram; reboot;

– Flash dd-wrt.v24-K3_AC_ARM_STD.bin (Kong build) via Administration -> Firmware Upgrade Tab (set to factory)
– Reboot the router

Step 2 – Setting up Virtual Wireless AP with VPN Tunneling on DD-WRT:

On page: Setup -> Basic tab:
– (Optional) Rename router to whatever you want
– Set local IP of router 192.168.1.x (or whatever class A IP address you defined)
– Subnet mask 255.255.255.0 (depends on your network)
– Gateway 192.168.1.1 (or whatever your bridged router’s IP is, if bridging to ISP’s router)
– Local DNS 8.8.8.8
– (Optional) Set start IP address from e.g. 190
– Enable DHCP server
– Set static DNS1 to 8.8.8.8 and static DNS2 to 8.8.4.4
– Enable “Use DNSMasq for DHCP”
– Enable DNSMasq for DNS
– Enable NTP server (e.g. asia.pool.ntp.org or whatever timezone you are in)

Navigate to Wireless -> Basic settings tab:
– Add a virtual AP (with AP name etc)
– Enable optimize multicast traffic option and bridged mode
– Save and reboot the router

Navigate to Setup -> Networking tab:
– Add bridges br0 and br1
– Reboot
– Assign br0 to eth1 interface prio 63
– Assign br1 to wl1.1 interface prio 63
– Save and reboot the router
– bridging table should show:

br0 no vlan1 eth1 eth2
br1 no wl1.1

Navigate to Setup -> Networking tab:
– Scroll down to the br1 interface
– Enable masquerade / NAT, make sure the other options are disabled
– Add a subnet ip address you want this bridge to have e.g. 10.13.37.1 with subnet mask 255.255.255.0
– Save and reboot the router

Navigate to Setup -> Networking tab:
– Scroll down to DHCPD
– enable DHCP0 for br1; e.g (ON, start 100, max 50, leasetime 3600)
– Save and reboot the router

Navigate to Services -> VPN tab:
– Scroll down to OpenVPN client and enable it
– Enter your OpenVPN server details (steps to setup OpenVPN server at bottom of post)
– Set tunnel device to TUN
– Set tunnel protocol to UDP
– Set encryption to Blowfish CBC
– Set hash algorithm to SHA1
– Enable user pass authentication if required and add the OpenVPN username and password
– Enable advanced options
– Set TLS ciphers to none
– Set LZO compression to yes
– Enable NAT
– Set firewall protection to disabled
– Leave IP address and subnet mask fields empty
– Set tunnel MTU setting to 1500
– Leave UDP fragment field empty
– Set UDP MSS-Fix to disabled
– Enable nsCertType vertication
– Export your openvpn.ovpn profile from your OpenVPN server (open .ovpn file in text editor. See Step 3 near the end of this post)
– Put the TLS auth key portion in the TLS Auth Key field
– Fields: add config, policy based routing, pkcs12, static key, all set to blank
– Place your CA cert in the CA Cert field
– Place your public client cert in the Public Cert field
– Place your private client key in the Private Key field
– Save and reboot the router

Navigate to Administration -> Commands tab and add the following as a startup script:

Start up script:

sleep 220; # sleep to allow enough time for NTP to update
tun_name=$(ifconfig | sed -n 's/.*\(tun[^ ]\).*/\1/p');
tun_addr=$(ifconfig $tun_name | sed -nr 's/.*P-t-P:([^ ]+) .*/\1/p');
ip rule add from 10.13.37.0/24 table 200; # IP varies on your br1 subnet
ip route add default via $tun_addr dev $tun_name table 200;
ip route flush cache;

– Reboot and make sure NTP updates the router time, if not the TLS negotiation to the OpenVPN server will fail. A workaround is to reboot the router to let NTP attempt an update again

Step 3 – Setting up OpenVPN Server on VPS (such as RamNode):

– wget http://swupdate.openvpn.org/as/openvpn-as-2.0.11-Ubuntu12.i386.deb
– dpkg -i openvpn-as-2.0.11-Ubuntu12.i386.deb
– passwd openvpn
– Login to the interface and export the .ovpn file to be used for the steps mentioned above

That’s it!

Docker is Awesome

I was playing around with Docker for the past few days and really think that it is an awesome tool. I installed Docker for Mac and created an image based on the original Kali docker base image. I no longer need to spawn a full-fledged virtualbox VM running Kali just to run tools such as msfconsole, wpscan etc.

small_v

I created a Dockerfile project to automatically provision a base image which contains the tools that I require. This file can be tweaked to add/remove tools to your liking, and it is available here: https://github.com/v00d00sec/kali-minimal-dockerfile. If you just want to pull the full image without building it from scratch via the Dockerfile, you can pull the image from: https://hub.docker.com/r/v00d00sec/kali_mini/ by issuing the following commands:

docker pull v00d00sec/kali_mini

After the base image is created, you can view it like this:
screen-shot-2016-09-13-at-3-27-44-pm

After-which, you can run the image like this:

screen-shot-2016-09-13-at-3-28-24-pm

Voila! Instant Kali shell spawned.

Manual Exploitation of Blind HQL Injections

Almost everyone has heard about SQL injections, but what about HQL injections? Hibernate Query Language (HQL) is an object-oriented query language, similar to Structured Query Language (SQL), but instead of operating on tables and columns, HQL works with persistent objects and their properties.

Imagine that you are doing a source-code review on a Java-based web application that uses Direct Web Remoting (DWR) and you come across the following line of code:

String statement = "select x from Rights x, Roles y where x.uId = y.uId " + "and y.roleId = " + rId;
List roles = DataUtils.getHQL().queryList(statement);

The ‘rId’ variable in the statement is taken directly from user input, thus making this line of code vulnerable. After tracing the ‘rId’ variable, it was found that the function that calls the vulnerable statement is also conveniently exposed via DWR. If you have not heard about DWR, it is a library that allows Java methods / functions to be exposed and called directly using JavaScript. (more info available here: http://directwebremoting.org)

Using the JavaScript console in a web browser, it was possible to trigger the function call using the following command:

DWRAction.getUserRightsByRoleID(rId);

Note: The function names depends on how the developer of the web application implements the DWR library. One way to determine this is to observe the traffic through a proxy and look for the main DWR call that pulls the exposed functions’ JavaScript .js file. Take the JavaScript content of the file and manually load it in the browser console, so that you will be able to call the functions.

Calling the following exposed DWR call via JavaScript with the following parameter returns a response of Content-Length: 120:

DWRAction.getUserRightsByRoleID("1 or 1=2");

While calling the following exposed DWR call via JavaScript with the following parameter returns a response of Content-Length: 2649040:

DWRAction.getUserRightsByRoleID("1 or 1=1");

This looks like a confirmed case of a blind HQL injection vulnerability. The goal now would be to dump some important information from the database. Attempts were made to use SQLmap as well as an obscure HQLmap tool from GitHub, but they were not successful. Attempts were then made to exploit this vulnerability manually.

While playing with the injection parameters, it was observed that UNION queries were not allowed. Also, <, >, !, = comparators are filtered by the XSS filters. Single and double quotes were also filtered. You might ask why is the “1 or 1=1” input allowed with quotes? The reason behind this is because the exposed JavaScript DWR method takes in a String, therefore it has be sent to the server enclosed in quotes [DWRAction.getUserRightsByRoleID(String);].

Consulting the official HQL manual shows that the following expressions are supported:

hql_expressions

After a bit of trial and error with the supported expressions, it was observed that the accepted inputs are:

  • ( ) Parentheses
  • Between x and y (this will be the alternative to < > comparators)
  • Sub SELECT statements allowed (alternative to UNION)

With this knowledge, the next step would be to find out valid tables and columns. Searching through the source code for other HQL statements gave some idea of what tables and columns exists. In this case, the table name was ‘TBL_USERS’ with several columns, but what we are interested in is just the “password” and “uId” columns.

The first step in order to extract some meaningful data will require a valid ‘uId’ from the ‘TBL_USERS’ table. The following sub SELECT payload was used:

DWRAction.getUserRightsByRoleID("1 or (select id from TBL_USERS where id between 1 and 2000) = 1");

After tweaking the ‘between’ values manually by elimination and looking at the server responses, it was observed that the ‘uId’ of value 1900 exists. Now, its time to get the password hash for the user who has the ‘uId’ value of 1900. One way to determine the characters of the password is to use ASCII comparison, and we will start with the first character of the password column using the following payload:

DWRAction.getUserRightsByRoleID("1 or (select ascii(substr(password,1,1)) from TBL_USERS where id = 1900) between [ASCII value] and [ASCII value]");

The ASCII values can be obtained from the chart below:

ascii_chart

This part gets repetitive as the trial and error process begins. After many tries using elimination by tweaking the ‘between’ ASCII values, we now know the first character is an ASCII value of 49 which is equivalent to ‘1’. Repeat this tedious process till the n character by altering the substr() parameter in the statement. You can make the process less manual by using Burp Intruder and observing the responses.

Update: After knowing that its a hash value from the first few characters, the statement can be simplified to:

DWRAction.getUserRightsByRoleID("1 or (select ascii(substr(password,1,1)) from TBL_USERS where id = 1900) = [ASCII value]");

Where the ASCII value can range from 48 to 57 and 97 to 122. (Thanks @gifted88 for the tip.)

After all the hard work, the password hash for the user with “uId” 1900 is:

14f61727aed7a8f8139c26714feec7c5

Post-Exploitation with Windows PowerShell

I gave a presentation on the topic of “Post-Exploitation with Windows PowerShell” sometime ago in 2015. This presentation showcases the use of PowerShell scripting exploitation frameworks coupled with various penetration testing tools and AV evasion techniques for post-exploitation on compromised hosts.

The slides can be found here:

Video of the demo from the presentation: