SecurityTrending
Axios ถูกฝังมัลแวร์: บทเรียนด้านความปลอดภัยสำหรับนักพัฒนา
เจาะลึกเหตุการณ์ Axios ถูกแฮกและฝังมัลแวร์ พร้อมวิธีป้องกันและตรวจสอบโปรเจกต์ของคุณ
April 6, 2025
·3 min read
เจาะลึกเหตุการณ์ Axios ถูกแฮกและฝังมัลแวร์ พร้อมวิธีป้องกันและตรวจสอบโปรเจกต์ของคุณ
พอดีเมื่อไม่กี่วันนี้เกิดเรื่องใหญ่ในวงการ dev ที่ทำให้หลายคนตกใจกันไม่น้อย คือ Axios ที่เราใช้กันแทบทุกโปรเจกต์สำหรับยิง API ถูกแฮกและมีคนแอบฝังมัลแวร์เข้าไป! ซึ่งเรื่องนี้มันเตือนใจเราได้ดีว่า ความปลอดภัยของ dependencies ที่เราใช้นี่สำคัญจริงๆ
เรื่องมันเป็นแบบนี้ครับ มีแฮกเกอร์เข้าถึงบัญชี npm ของคนดูแล Axios ได้ แล้วก็อัปโหลดเวอร์ชันที่มีมัลแวร์แอบแฝงเข้าไป ซึ่งมัลแวร์ตัวนี้มันทำอะไรได้บ้าง:
# เวอร์ชันที่มีมัลแวร์ [email protected] - 1.7.2 # เวอร์ชันที่ปลอดภัย [email protected] (เวอร์ชันก่อนหน้า) [email protected]+ (เวอร์ชันที่แก้ไขแล้ว)
# ดูว่าเราใช้เวอร์ชันไหนอยู่ npm list axios # ถ้าใช้ yarn yarn why axios
# สแกนหาช่องโหว่ npm audit # ให้มันแก้ให้เลย (ถ้าทำได้) npm audit fix # บังคับอัปเดตเลย ไม่สนว่าจะ breaking หรือเปล่า npm audit fix --force
{ "dependencies": { "axios": { "version": "1.7.1", // ⚠️ ถ้าเจอเวอร์ชันนี้ ระวังนะ! "resolved": "https://registry.npmjs.org/axios/-/axios-1.7.1.tgz" } } }
# ลบของเก่าทิ้งก่อน rm -rf node_modules package-lock.json # ติดตั้งเวอร์ชันใหม่ npm install axios@latest # หรือจะระบุเวอร์ชันเลยก็ได้ npm install [email protected]
// package.json { "dependencies": { "axios": "^1.7.3" }, "overrides": { "axios": "1.7.3" // บังคับให้ใช้เวอร์ชันนี้เท่านั้น } }
# .github/workflows/security.yml name: Security Audit on: [push, pull_request] jobs: audit: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Run npm audit run: npm audit --audit-level=high - name: Check for malicious packages run: npx socket security
# ลองใช้ Socket Security ดู npm install -g @socketsecurity/cli # สแกนโปรเจกต์ npx socket security
// อย่าใช้ ^ หรือ ~ ถ้าไม่จำเป็น { "dependencies": { "axios": "1.7.3" // ระบุเวอร์ชันตายตัว } }
// ตรวจสอบว่า package ถูกแก้ไขหรือเปล่า import crypto from 'crypto'; import fs from 'fs'; function verifyPackageIntegrity(packagePath: string, expectedHash: string) { const fileBuffer = fs.readFileSync(packagePath); const hashSum = crypto.createHash('sha256'); hashSum.update(fileBuffer); const hex = hashSum.digest('hex'); if (hex !== expectedHash) { throw new Error('เอ๊ะ! Package นี้ถูกแก้ไข!'); } }
# ดูว่ามี package ไหนเก่าไปแล้วบ้าง npx npm-check-updates # หรือใช้คำสั่งนี้ npm outdated # ลองใช้ Snyk ดูก็ได้ npx snyk test
# ติดตั้ง npm install -g @socketsecurity/cli # รันเลย socket security
# ติดตั้ง npm install -g snyk # Login ก่อน snyk auth # เช็คโปรเจกต์ snyk test # ให้มันคอยเช็คให้เรื่อยๆ snyk monitor
# ใช้ผ่าน Docker สะดวกดี docker run --rm -v $(pwd):/src owasp/dependency-check \ --scan /src --format "ALL"
เรื่องนี้สอนเราได้หลายอย่างเลย:
เรื่อง Axios ครั้งนี้เป็นบทเรียนที่ดีสำหรับพวกเราทุกคน การดูแลความปลอดภัยของ dependencies ไม่ใช่แค่กด update แล้วจบ แต่ต้องมีการเช็ค ทดสอบ และใช้เครื่องมือช่วยด้วย
สิ่งที่ควรทำตอนนี้เลย:
จำไว้นะครับว่า ความปลอดภัยมันต้องทำต่อเนื่อง ไม่ใช่ทำครั้งเดียวแล้วจบ!