---
title: "Set Up the Security Lab"
description: "ดูว่า OWASP MCP Top 10 แต่ละข้ออยู่บทไหนของคอร์สนี้ แล้วเตรียมโฟลเดอร์ cafe-mcp จากคอร์สแรกให้พร้อมทดลองโจมตีบนเครื่องตัวเอง ทั้ง Database http_server.py และตัวแปร curl"
canonical_url: "https://iamthana.com/courses/mcp-security/lab"
---

> Index of every page on this site: [llms.txt](https://iamthana.com/llms.txt)

คอร์สนี้เราจะลองโจมตี `cafe-mcp` ของตัวเองตามรายการ OWASP MCP Top 10 แล้วปิดช่องทีละข้อครับ บทนี้ดูรายการนั้นก่อน แล้วเตรียมเครื่องให้พร้อม

## What Is OWASP MCP Top 10

OWASP คือองค์กรที่ทำรายการช่องโหว่ที่เจอบ่อย อย่าง OWASP Top 10 ของเว็บ ส่วนของ MCP มี 10 ข้อตามตารางนี้ (ฉบับ 2025 สถานะ beta) คอลัมน์ขวาบอกว่าคอร์สนี้ลองข้อนั้นในบทไหน

<table>
<thead>
  <tr>
    <th>
      ID
    </th>
    
    <th>
      Item
    </th>
    
    <th>
      Covered In
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      MCP01
    </td>
    
    <td>
      Token Mismanagement & Secret Exposure
    </td>
    
    <td>
      <a href="/courses/mcp-security/auth">
        Require a Token
      </a>
      
       (token อยู่ใน environment variable), <a href="/courses/mcp-security/http-attacks">
        HTTP Attacks
      </a>
      
       กับ <a href="/courses/mcp-security/http-defenses">
        Defend the HTTP Server
      </a>
      
       (token passthrough) และ <a href="/courses/mcp-security/audit-log">
        Audit Tool Calls
      </a>
      
       (token ไม่เข้า log)
    </td>
  </tr>
  
  <tr>
    <td>
      MCP02
    </td>
    
    <td>
      Privilege Escalation via Scope Creep
    </td>
    
    <td>
      <a href="/courses/mcp-security/scopes">
        Limit Tools by Scope
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      MCP03
    </td>
    
    <td>
      Tool Poisoning
    </td>
    
    <td>
      <a href="/courses/mcp-security/tool-attacks">
        Run Tool Attacks
      </a>
      
       และ <a href="/courses/mcp-security/tool-defenses">
        Defend Against Tool Attacks
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      MCP04
    </td>
    
    <td>
      Software Supply Chain Attacks & Dependency Tampering
    </td>
    
    <td>
      <a href="/courses/mcp-security/tool-attacks">
        Run Tool Attacks
      </a>
      
       กับ <a href="/courses/mcp-security/tool-defenses">
        Defend Against Tool Attacks
      </a>
      
       (server ที่เปลี่ยนหลังติดตั้ง), <a href="/courses/mcp-security/supply-chain">
        Lock Dependencies and Pin Servers
      </a>
      
       (lock, hash และเวอร์ชันของคำสั่ง) และ <a href="/courses/mcp-security/shadow-servers">
        Find Shadow Servers
      </a>
      
       (หาคำสั่งที่ไม่ได้ล็อกเวอร์ชัน)
    </td>
  </tr>
  
  <tr>
    <td>
      MCP05
    </td>
    
    <td>
      Command Injection & Execution
    </td>
    
    <td>
      <a href="/courses/mcp-security/command-injection">
        Shell Commands from Tool Input
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      MCP06
    </td>
    
    <td>
      Prompt Injection via Contextual Payloads
    </td>
    
    <td>
      <a href="/courses/mcp-security/resource-injection">
        Untrusted Text in Resources
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      MCP07
    </td>
    
    <td>
      Insufficient Authentication & Authorization
    </td>
    
    <td>
      <a href="/courses/mcp-security/auth">
        Require a Token
      </a>
      
      , <a href="/courses/mcp-security/scopes">
        Limit Tools by Scope
      </a>
      
      , <a href="/courses/mcp-security/http-attacks">
        HTTP Attacks
      </a>
      
       และ <a href="/courses/mcp-security/http-defenses">
        Defend the HTTP Server
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      MCP08
    </td>
    
    <td>
      Lack of Audit and Telemetry
    </td>
    
    <td>
      <a href="/courses/mcp-security/audit-log">
        Audit Tool Calls
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      MCP09
    </td>
    
    <td>
      Shadow MCP Servers
    </td>
    
    <td>
      <a href="/courses/mcp-security/shadow-servers">
        Find Shadow Servers
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      MCP10
    </td>
    
    <td>
      Context Injection & Over-Sharing
    </td>
    
    <td>
      <a href="/courses/mcp-security/resource-injection">
        Untrusted Text in Resources
      </a>
      
       (<code>
        sales_resource
      </code>
      
       ไม่ส่งชื่อลูกค้า)
    </td>
  </tr>
</tbody>
</table>

## Trust Boundary

เวลา AI หรือ server ได้รับข้อความ คอร์สนี้จะดูก่อนเสมอครับว่าใครเป็นคนเขียน เส้นแบ่งระหว่างข้อความที่เราเขียนเองกับข้อความที่คนอื่นเขียนเรียกว่า trust boundary

<table>
<thead>
  <tr>
    <th>
      Text
    </th>
    
    <th>
      Written By
    </th>
    
    <th>
      Reaches
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        description
      </code>
      
       ของ tool
    </td>
    
    <td>
      developer ของ server (เราเอง หรือคนอื่นถ้าเป็น server ที่ติดตั้งมา)
    </td>
    
    <td>
      AI อ่านก่อนเลือกเรียก tool
    </td>
  </tr>
  
  <tr>
    <td>
      argument ของ tool
    </td>
    
    <td>
      AI เลือกเองจากข้อความที่อ่านมา
    </td>
    
    <td>
      server ที่รับ request
    </td>
  </tr>
  
  <tr>
    <td>
      ข้อความใน resource (ชื่อลูกค้า โน้ต)
    </td>
    
    <td>
      ลูกค้าหรือใครก็ตามที่พิมพ์ลง Database ได้
    </td>
    
    <td>
      AI ผ่าน host
    </td>
  </tr>
  
  <tr>
    <td>
      token ใน header
    </td>
    
    <td>
      client ที่ต่อเข้ามา
    </td>
    
    <td>
      server
    </td>
  </tr>
</tbody>
</table>

ในตารางนี้มีแค่ description ของ `cafe` ที่เราเขียนเอง ที่เหลือเราไม่ได้เขียนเอง เลยเชื่อไม่ได้จนกว่าจะตรวจ แต่ละแบบตรวจคนละที่

- description ของ server ที่ติดตั้งมา เราอ่านเองก่อนอนุมัติ แล้วเช็กซ้ำว่ามันไม่เปลี่ยนทีหลัง
- argument ของ tool ให้ server ตรวจก่อนเอาไปใช้
- ข้อความใน resource server บอกไม่ได้ว่า AI จะทำตามคำสั่งที่แฝงมาไหม ทำได้แค่ตรวจรูปแบบตอนเขียนลง Database ส่งเท่าที่จำเป็น และให้คนยืนยันก่อน tool แก้ข้อมูล
- token ให้ server ตรวจว่าออกมาให้ `cafe` จริง และมี scope (สิทธิ์ที่ติดมากับ token) พอสำหรับ tool ที่เรียก

## What You Need

คอร์สนี้ต่อจากคอร์ส [MCP Server with Python](https://iamthana.com/courses/mcp-server) โฟลเดอร์ `cafe-mcp` ที่ได้จากคอร์สนั้นต้องมีของตามตารางนี้ ถ้าขาดอันไหนให้กลับไปทำบทในคอลัมน์ขวา

<table>
<thead>
  <tr>
    <th>
      Needed
    </th>
    
    <th>
      Lesson
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        database.py
      </code>
      
       ที่สร้าง <code>
        cafe.db
      </code>
      
       พร้อมข้อมูลตั้งต้น
    </td>
    
    <td>
      <a href="/courses/mcp-server/first-tool">
        Build Your First Tool
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        http_server.py
      </code>
    </td>
    
    <td>
      <a href="/courses/mcp-server/inspect-traffic">
        Intercept Requests and Responses
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        cafe_server.py
      </code>
      
       ที่มี <code>
        list_menu
      </code>
      
      , <code>
        create_order
      </code>
      
      , <code>
        cancel_order
      </code>
      
      , <code>
        ask_cancel_confirmation
      </code>
      
       และ <code>
        load_order
      </code>
    </td>
    
    <td>
      <a href="/courses/mcp-server/orders">
        Confirm Actions with Elicitation
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      resource ของเมนู ออร์เดอร์ ยอดขาย และสูตร
    </td>
    
    <td>
      <a href="/courses/mcp-server/resources">
        Expose Data with Resources
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      prompt <code>
        daily_summary
      </code>
    </td>
    
    <td>
      <a href="/courses/mcp-server/prompts">
        Build a Prompt
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      completion ของชื่อเมนูกับ <code>
        size
      </code>
    </td>
    
    <td>
      <a href="/courses/mcp-server/completion">
        Autocomplete with Completion
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        stock_server.py
      </code>
      
       กับ <code>
        notify_server.py
      </code>
    </td>
    
    <td>
      <a href="/courses/mcp-server/stock-notify">
        Add Stock and Notify Servers
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        tests/
      </code>
      
       ที่ผ่านทั้ง 14 ตัว
    </td>
    
    <td>
      <a href="/courses/mcp-server/testing">
        Test with pytest
      </a>
    </td>
  </tr>
</tbody>
</table>

ต้องใช้ [Claude Code](https://code.claude.com/docs) กับ `curl` เหมือนคอร์สแรก

## Set Up

ทุกคำสั่งของคอร์สนี้รันในโฟลเดอร์ `cafe-mcp` เปิด terminal แล้วเข้าไปที่โฟลเดอร์นั้นก่อน (เปลี่ยน `/path/to/cafe-mcp` เป็น path จริงบนเครื่อง)

```bash
cd /path/to/cafe-mcp
```

### Database

เลขออร์เดอร์ในตัวอย่างของคอร์สนี้นับต่อจากข้อมูลตั้งต้น เลยต้องลบ `cafe.db` แล้วสร้างใหม่ ออร์เดอร์ที่สร้างไว้ตอนเรียนคอร์สแรกจะหายไป

```bash
rm -f cafe.db
uv run python -c "from database import connect; connect().close()"
```

`cafe.db` ใหม่มีออร์เดอร์ 1 กับ 2

### HTTP Server

`http_server.py` จากคอร์สแรกมีแค่นี้ คอร์สนี้จะแก้ไฟล์นี้ในบท [Defend the HTTP Server](https://iamthana.com/courses/mcp-security/http-defenses)

```python [http_server.py]tangle=expect
from cafe_server import mcp

mcp.run(transport="streamable-http", host="127.0.0.1", port=8765)
```

รันค้างไว้ในหน้าต่างนี้

```bash
uv run python http_server.py
```

```text [$ uv run python http_server.py]
INFO:     Uvicorn running on http://127.0.0.1:8765 (Press CTRL+C to quit)
```

### curl Variables

เปิด terminal อีกหน้าต่างสำหรับ curl แล้ว `cd` เข้า `cafe-mcp` เหมือนเดิม จากนั้นตั้งตัวแปรสองตัวนี้

```bash
URL=http://127.0.0.1:8765/mcp
META='"_meta":{"io.modelcontextprotocol/protocolVersion":"2026-07-28","io.modelcontextprotocol/clientCapabilities":{}}'
```

`META` คือ `_meta` ที่ทุก request ต้องแนบ ตัวแปรอยู่แค่ในหน้าต่างที่ตั้ง ถ้าเปิดหน้าต่างใหม่ต้องตั้งอีกรอบ

## Check the Lab

เช็กสองอย่างในหน้าต่าง curl ครับ อย่างแรกคือ server ตอบ request ที่ใช้ `URL` กับ `META`

```bash
curl -s -o /dev/null -w '%{http_code}\n' $URL \
  -H 'Content-Type: application/json' \
  -H 'Accept: application/json, text/event-stream' \
  -H 'MCP-Protocol-Version: 2026-07-28' \
  -H 'Mcp-Method: tools/list' \
  -d "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"tools/list\",\"params\":{$META}}"
```

```text [$ curl ... tools/list]
200
```

อย่างที่สองคือเทสต์ของคอร์สแรก

```bash
uv run pytest -q
```

```text [$ uv run pytest -q]
..............                                                           [100%]
14 passed in 0.11s
```

ต้องผ่านทั้ง 14 ตัวก่อนเริ่ม เพราะบทต่อ ๆ ไปจะเพิ่มเทสต์เข้าชุดนี้ ถ้าของเดิม Fail อยู่จะแยกไม่ออกว่าพังเพราะโค้ดของบทไหน ถ้าอันไหน Fail ให้กลับไปแก้ที่บทของมันในคอร์สแรก

## Attack Files

ไฟล์โจมตีในคอร์สนี้ (`promo_server.py`, `open_server.py` และอื่น ๆ) เขียนมาเพื่อทดลอง ข้อมูลที่ "ถูกขโมย" ไปลงไฟล์ในโฟลเดอร์เดียวกัน ตัวไฟล์โจมตีไม่ส่งอะไรออกนอกเครื่อง (แต่ตอนลองผ่าน Claude Code ข้อความในบทสนทนาจะถูกส่งไปที่โมเดลตามปกตินะ)

- รันบนเครื่องของเราเองเท่านั้น ห้ามชี้ไปที่ server หรือเครื่องของคนอื่น
- ห้ามคัดโค้ดฝั่งโจมตีไปใช้นอกการทดลอง

## Summary

บทถัดไป [Untrusted Text in Resources](https://iamthana.com/courses/mcp-security/resource-injection) เริ่มจากช่องที่ง่ายที่สุด คือข้อความที่ลูกค้าพิมพ์เข้ามาเอง ให้รัน `http_server.py` ค้างไว้ และใช้หน้าต่าง curl เดิมต่อได้เลย

## References

- [OWASP MCP Top 10](https://owasp.org/projects/mcp-top-10)
- [Security Best Practices](https://modelcontextprotocol.io/docs/2026-07-28/tutorials/security/security_best_practices)

## Sitemap

See the full [sitemap](https://iamthana.com/sitemap.md) for all pages.
