-
Notifications
You must be signed in to change notification settings - Fork 0
/
nuclei_test.go
160 lines (114 loc) · 4.15 KB
/
nuclei_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"net/http"
"strings"
"testing"
"github.com/projectdiscovery/nuclei/v3/pkg/templates"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"
)
const (
templateString = `
id: my-template
info:
name: "My Test Template"
author: mikerott
description: |
This is a test template.
tags: mike
classification:
cvss-metrics: CVSS:3.0/AV:N/AC:L/PR:H/UI:N/S:C/C:H/I:H/A:H
cvss-score: 5.0
cve-id: CVE-2019-0000
cwe-id: CWE-22
http:
- method: GET
path:
- "{{BaseURL}}"
matchers-condition: and
matchers:
- type: status
status:
- 200
`
)
func TestTemplateHit(t *testing.T) {
addresses := []string{"http://localhost"} // template hit = resultEvent
nuclei := Nuclei{}
template := templates.Template{}
if err := yaml.Unmarshal([]byte(templateString), &template); err != nil {
assert.FailNow(t, err.Error())
}
resultEvents, failureEvents, err := nuclei.RunScan(addresses, template)
assert.Nil(t, err)
assert.Equal(t, 1, len(resultEvents))
assert.Equal(t, 0, len(failureEvents))
}
func TestTemplateMiss(t *testing.T) {
address := "http://localhost/this-is-a-501" // template miss = failureEvent
nuclei := Nuclei{}
template := templates.Template{}
if err := yaml.Unmarshal([]byte(templateString), &template); err != nil {
assert.FailNow(t, err.Error())
}
resultEvents, failureEvents, err := nuclei.RunScan([]string{address}, template)
assert.Nil(t, err)
assert.Equal(t, 0, len(resultEvents))
assert.Equal(t, 1, len(failureEvents))
_, ok := (failureEvents[0].InternalEvent)["error"]
assert.False(t, ok) // no "error" in the map
assert.Equal(t, (failureEvents[0].InternalEvent)["status_code"], 501)
}
func TestUnreachableConnectionRefused(t *testing.T) {
address := "http://localhost:9999" // unreachable = failureEvent
nuclei := Nuclei{}
template := templates.Template{}
if err := yaml.Unmarshal([]byte(templateString), &template); err != nil {
assert.FailNow(t, err.Error())
}
resultEvents, failureEvents, err := nuclei.RunScan([]string{address}, template)
assert.Nil(t, err)
assert.Equal(t, 0, len(resultEvents))
assert.Equal(t, 1, len(failureEvents))
assert.True(t, len((failureEvents[0].InternalEvent)["error"].(string)) > 0)
assert.Equal(t, (failureEvents[0].InternalEvent)["status_code"], 0)
assert.True(t, strings.Contains((failureEvents[0].InternalEvent)["error"].(string), "connection refused"))
}
func TestUnreachableTimedOut(t *testing.T) {
httpClient := http.Client{}
request, err := http.NewRequest("POST", "http://localhost/instruct", strings.NewReader(`{"matchers":{"path":"/latency5000","method":"get"},"behaviors":{"latency":5000},"response":{"code":200}}`))
request.Header.Add("Content-Type", "application/json")
assert.Nil(t, err)
response, err := httpClient.Do(request)
assert.Nil(t, err, "Did you run from `make test`, because you should have!")
assert.Equal(t, 201, response.StatusCode)
address := "http://localhost/latency5000" // will time out
nuclei := Nuclei{}
template := templates.Template{}
if err := yaml.Unmarshal([]byte(templateString), &template); err != nil {
assert.FailNow(t, err.Error())
}
resultEvents, failureEvents, err := nuclei.RunScan([]string{address}, template)
assert.Nil(t, err)
assert.Equal(t, 0, len(resultEvents))
assert.Equal(t, 1, len(failureEvents))
assert.True(t, len((failureEvents[0].InternalEvent)["error"].(string)) > 0)
assert.Equal(t, (failureEvents[0].InternalEvent)["status_code"], 0)
assert.True(t, strings.Contains((failureEvents[0].InternalEvent)["error"].(string), "context deadline exceeded"))
}
func TestMultipleAddresses(t *testing.T) {
addresses := []string{
"http://localhost", // hit = resultEvent
"http://localhost/this-is-a-501", // template miss = failureEvent
"http://localhost:9999", // unreachable = failureEvent
}
nuclei := Nuclei{}
template := templates.Template{}
if err := yaml.Unmarshal([]byte(templateString), &template); err != nil {
assert.FailNow(t, err.Error())
}
resultEvents, failureEvents, err := nuclei.RunScan(addresses, template)
assert.Nil(t, err)
assert.Equal(t, 1, len(resultEvents))
assert.Equal(t, 2, len(failureEvents))
}