提前捕获 .NET 漏洞:使用 Puma Scan 的实战指南

发布: (2025年12月5日 GMT+8 02:50)
3 min read
原文: Dev.to

Source: Dev.to

为什么选择 Puma Scan?

根据官方的 OWASP Source Code Analysis Tools 列表,虽然有很多 SAST 工具,但本次作业排除了 Sonar、Snyk、Semgrep 和 Veracode。在剩余的 C#/.NET 选项中,Puma Scan 脱颖而出,原因如下:

  • 专为 .NET 和 C# 设计
  • 可与 Visual StudioVS Code 以及 命令行 集成
  • 能检测常见的 OWASP Top 10 问题,如 SQL 注入跨站脚本 (XSS)
  • 提供 Community Edition,免费用于学习和测试

完整的演示——包括代码、自动化脚本和扫描结果——已在 GitHub 上公开。

漏洞代码示例

为了保持演示的简洁和可复现性,使用了一个带有经典 SQL 注入漏洞 的最小 C# 类:

// TestVuln.cs
using System;
using System.Data.SqlClient;

public class VulnerableClass
{
    public void UnsafeQuery(string userInput)
    {
        // ⚠️ SQL Injection: user input concatenated directly into query
        string query = "SELECT * FROM Users WHERE Id = " + userInput;
        SqlCommand cmd = new SqlCommand(query);
    }
}

如果攻击者传入 1 OR 1=1,查询将变为:

SELECT * FROM Users WHERE Id = 1 OR 1=1

→ 所有用户记录被泄露。

这对应 CWE‑89,属于 OWASP Top 10:A03:2021 – 注入

使用 Puma Scan 自动检测

步骤 1:将 Puma Scan 添加到项目中

dotnet add package Puma.Security.Rules

步骤 2:PowerShell 脚本 (scan.ps1)

# scan.ps1 - PumaScan SAST Analysis Script
Write-Host "======================================" -ForegroundColor Cyan
Write-Host " PumaScan - SAST Security Analysis" -ForegroundColor Cyan
Write-Host "======================================" -ForegroundColor Cyan
Write-Host ""

# Build the project with PumaScan analysis
Set-Location PumaScanner
dotnet clean | Out-Null
dotnet build

Write-Host ""
Write-Host "======================================" -ForegroundColor Green
Write-Host " Analysis Complete!" -ForegroundColor Green
Write-Host "======================================" -ForegroundColor Green
Write-Host ""
Write-Host "Look for security warnings above:" -ForegroundColor Yellow
Write-Host "  - SEC0107: SQL Injection vulnerability" -ForegroundColor Yellow
Write-Host ""
Set-Location ..

步骤 3:查看结果

使用以下命令运行脚本:

powershell -ExecutionPolicy Bypass -File .\scan.ps1

输出中会出现类似以下的明确警告:

SEC0107: SQL Injection vulnerability

这表明 Puma Scan 已成功识别出示例代码中的注入查询。

Back to Blog

相关文章

阅读更多 »