DEV Community

Tossapol Ritcharoenwattu
Tossapol Ritcharoenwattu

Posted on

ทดสอบการใช้งาน trivy โดย ลอง scan nginx version 1 และ 2 กัน

เราจะสร้าง nginx version 1 และ version 2 มา run ใน docker ทดลองใช้ trivy มา scan หาช่องโหว่กันนะครับ

1. สร้าง nginx version 1 and version 2
สร้างไฟล์สำหรับ Nginx v1:

สร้างไดเรกทอรีสำหรับโปรเจกต์:
mkdir nginx-v1
cd nginx-v1

สร้างไฟล์ index.html ให้แสดงผล "Hello, V1":
echo "Hello, V1" > index.html

สร้างไฟล์ Dockerfile เพื่อสร้าง image ของเรา:
Dockerfile
FROM nginx:1.21.6
COPY ./index.html /usr/share/nginx/html/index.html

สร้าง image และ run container:
docker build -t nginx-v1 .
docker run -d -p 8081:80 --name nginx-v1-container nginx-v1
คุณจะเข้าถึงเวอร์ชันนี้ได้ที่ http://:8081
Image description

สร้างไฟล์สำหรับ Nginx v2:
กลับไปยังไดเรกทอรีหลักและสร้างไดเรกทอรีใหม่:
cd ..
mkdir nginx-v2
cd nginx-v2

สร้างไฟล์ index.html ให้แสดงผล "Hello, V2":
echo "Hello, V2" > index.html

สร้าง Dockerfile โดยใช้ Nginx เวอร์ชันใหม่กว่า:
Dockerfile
FROM nginx:1.25.1
COPY ./index.html /usr/share/nginx/html/index.html

สร้าง image และ run container:
docker build -t nginx-v2 .
docker run -d -p 8082:80 --name nginx-v2-container nginx-v2
คุณจะเข้าถึงเวอร์ชันนี้ได้ที่ http://:8082
Image description

2. Scan Container Images ด้วย Trivy
ติดตั้ง Trivy:
curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sudo sh -s -- -b /usr/local/bin
Image description

Scan Images:
ตอนนี้มาสแกน image ทั้งสองตัวที่เราสร้างขึ้น:
Scan Nginx v1:
trivy image nginx-v1
Image description
Image description

Scan Nginx v2:
trivy image nginx-v2
Image description

จะพบว่า รายงานช่องโหว่ (vulnerabilities) ของ nginx version 1 = 479 ซึ่งมากกว่า nginx version 2 = 329

3. List vulnerabilities
เราสามารถแสดง ช่องโหว่ โดยกรองระดับความสำคัญ ได้ด้วย command นี้
กรณีถ้าต้องการเฉพาะบางตัวอาจใส่ตัวใดตัวหนึ่งก็ได้

trivy image --severity LOW,MEDIUM,HIGH,CRITICAL nginx:1.21.6
Enter fullscreen mode Exit fullscreen mode

Image description

Top comments (0)