/
type_api.go
380 lines (304 loc) · 9.37 KB
/
type_api.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package cmd
import (
"bufio"
"crypto/tls"
"errors"
"fmt"
"os"
"os/exec"
"strconv"
"strings"
"time"
"github.com/go-resty/resty/v2"
"github.com/theckman/yacspin"
)
var (
basic_username string
basic_password string
token_auth string
)
func gatheringData(value Rule, turbo bool) {
var resp *resty.Response
var err error
if !turbo {
fmt.Println("│ ")
fmt.Println(line)
fmt.Println("│ ")
fmt.Println("├ API Rule #", value.ID)
fmt.Println("│ Rule name : ", value.Name)
fmt.Println("│ Rule description : ", value.Description)
fmt.Println("│ Impacted Env : ", value.Environment)
fmt.Println("│ Tags : ", value.Tags)
fmt.Println("│ ")
fmt.Println("│ API ENDPOINT : ", value.Api_Endpoint)
fmt.Println("│ ")
}
client := resty.New()
if value.Api_Insecure {
client.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
if !turbo {
colorYellowBold.Println("│ API INSECURE - NO TLS CHECK")
fmt.Println("│ ")
}
}
cfg := yacspin.Config{
Frequency: 100 * time.Millisecond,
CharSet: yacspin.CharSets[3],
Suffix: strconv.Itoa(value.ID) + " API Gathering Data ",
SuffixAutoColon: true,
StopCharacter: "│ ✓ ",
StopMessage: "OK",
StopColors: []string{"fgGreen"},
StopFailCharacter: "│ ✗ ",
StopFailColors: []string{"fgRed"},
StopFailMessage: "Response Status NOT OK",
}
spinner, err := yacspin.New(cfg)
if err != nil {
LogError(err)
}
contentType := detectFormat(value.Api_Body)
switch value.Api_Auth {
case "basic":
//fmt.Println("│ API BASIC AUTH")
if (value.Api_Auth_Basic) != nil {
secret, ok := os.LookupEnv("INTERCEPT_" + *value.Api_Auth_Basic)
if ok {
if strings.Contains(secret, ":") {
authParts := strings.Split(secret, ":")
if len(authParts) > 1 {
basic_username = authParts[0]
basic_password = authParts[1]
}
} else {
LogError(errors.New("API Basic Auth Environment variable must be defined as username:password"))
}
} else {
LogError(errors.New("API Auth Environment variables not set for this request"))
}
} else {
LogError(errors.New("API Auth Environment variables not set for this request"))
}
case "token":
//fmt.Println("│ API TOKEN AUTH")
if (value.Api_Auth_Token) != nil {
secret, ok := os.LookupEnv("INTERCEPT_" + *value.Api_Auth_Token)
if ok {
token_auth = secret
} else {
LogError(errors.New("API Auth Environment variables not set for this request"))
}
} else {
LogError(errors.New("API Auth Environment variables not set for this request"))
}
default:
LogError(errors.New("API Auth not defined"))
}
_ = os.Remove("output_" + strconv.Itoa(value.ID))
err = spinner.Start()
if err != nil {
LogError(err)
}
switch value.Api_Request {
case "GET":
if token_auth != "" {
resp, err = client.R().
EnableTrace().
SetAuthToken(token_auth).
SetOutput("output_" + strconv.Itoa(value.ID)).
Get(value.Api_Endpoint)
} else {
resp, err = client.R().
EnableTrace().
SetBasicAuth(basic_username, basic_password).
SetOutput("output_" + strconv.Itoa(value.ID)).
Get(value.Api_Endpoint)
}
case "POST":
if token_auth != "" {
resp, err = client.R().
EnableTrace().
SetAuthToken(token_auth).
SetHeader("Content-Type", contentType).
SetBody(value.Api_Body).
SetOutput("output_" + strconv.Itoa(value.ID)).
Post(value.Api_Endpoint)
} else {
resp, err = client.R().
EnableTrace().
SetBasicAuth(basic_username, basic_password).
SetHeader("Content-Type", contentType).
SetBody(value.Api_Body).
SetOutput("output_" + strconv.Itoa(value.ID)).
Post(value.Api_Endpoint)
}
default:
LogError(errors.New("Invalid request type"))
return
}
if resp.IsSuccess() {
spinner.Stop()
fmt.Println("│ ")
if !turbo {
fmt.Println("│ ")
fmt.Println("│ API Response Status:", resp.Status())
fmt.Println("│ ")
if value.Api_Trace {
fmt.Println("│ API Request Body:", value.Api_Body)
// Explore response object
fmt.Println("│ API Response Info:")
fmt.Println("│ Error :", err)
fmt.Println("│ Status Code:", resp.StatusCode())
fmt.Println("│ Status :", resp.Status())
fmt.Println("│ Proto :", resp.Proto())
fmt.Println("│ Time :", resp.Time())
fmt.Println("│ Received At:", resp.ReceivedAt())
// Explore trace info
fmt.Println("│ API Request Trace Info:")
ti := resp.Request.TraceInfo()
fmt.Println("│ DNSLookup :", ti.DNSLookup)
fmt.Println("│ ConnTime :", ti.ConnTime)
fmt.Println("│ TCPConnTime :", ti.TCPConnTime)
fmt.Println("│ TLSHandshake :", ti.TLSHandshake)
fmt.Println("│ ServerTime :", ti.ServerTime)
fmt.Println("│ ResponseTime :", ti.ResponseTime)
fmt.Println("│ TotalTime :", ti.TotalTime)
fmt.Println("│ IsConnReused :", ti.IsConnReused)
fmt.Println("│ IsConnWasIdle :", ti.IsConnWasIdle)
fmt.Println("│ ConnIdleTime :", ti.ConnIdleTime)
fmt.Println("│ RequestAttempt:", ti.RequestAttempt)
fmt.Println("│ RemoteAddr :", ti.RemoteAddr.String())
}
}
} else {
spinner.StopFail()
LogError(errors.New(resp.Status()))
}
if err != nil {
spinner.StopFail()
LogError(err)
}
}
func processAPIType(value Rule, turbo bool) {
if cfgEnv == "" {
cfgEnv = "先锋"
}
rules := loadUpRules()
pwddir := GetWd()
rgembed, _ := prepareEmbeddedExecutable()
searchPatternFile := strings.Join([]string{pwddir, "/", "search_regex_", strconv.Itoa(value.ID)}, "")
scanPath := "output_" + strconv.Itoa(value.ID)
if !FileExists(scanPath) {
LogError(errors.New("API Output not found"))
}
apiRule = InterceptCompliance{}
apiRule.RuleDescription = value.Description
apiRule.RuleError = value.Error
apiRule.RuleFatal = value.Fatal
apiRule.RuleID = strconv.Itoa(value.ID)
apiRule.RuleName = value.Name
apiRule.RuleSolution = value.Solution
apiRule.RuleType = value.Type
exception := ContainsInt(rules.Exceptions, value.ID)
if exception && !auditNox && !value.Enforcement {
colorRedBold.Println("│")
colorRedBold.Println("│ ", rules.ExceptionMessage)
colorRedBold.Println("│")
} else {
codePatternScan := []string{"--pcre2", "-p", "-o", "-A0", "-B0", "-C0", "-i", "-U", "-f", searchPatternFile, scanPath}
xcmd := exec.Command(rgembed, codePatternScan...)
if !turbo {
xcmd.Stdout = os.Stdout
xcmd.Stderr = os.Stderr
}
errr := xcmd.Run()
if errr != nil {
if xcmd.ProcessState.ExitCode() == 2 {
LogError(errr)
} else {
apiFinding := InterceptComplianceFinding{
FileName: scanPath,
FileHash: sha256hash([]byte(scanPath)),
ParentID: value.ID,
}
envfound := FindMatchingString(cfgEnv, value.Environment, ",")
if (envfound || strings.Contains(value.Environment, "all") || value.Environment == "") && value.Fatal {
if !turbo {
colorRedBold.Println("│")
colorRedBold.Println("│ NON COMPLIANT : ")
colorRedBold.Println("│ ", value.Error)
colorRedBold.Println("│")
} else {
colorRedBold.Print("│ ✗ ", value.ID, " ")
}
apiFinding.Compliant = false
apiFinding.Missing = false
apiFinding.Output = "NON COMPLIANT"
fatal = true
stats.Fatal++
} else {
if !turbo {
colorRedBold.Println("│")
colorRedBold.Println("│ NOT FOUND")
colorRedBold.Println("│ ", value.Error)
colorRedBold.Println("│")
} else {
colorRedBold.Print("│ ✗ ", value.ID, " ")
}
warning = true
apiFinding.Compliant = false
apiFinding.Missing = true
apiFinding.Output = "NOT FOUND"
}
if !turbo {
colorRedBold.Println("│")
colorRedBold.Println("│ API Rule : ", value.Name)
colorRedBold.Println("│ Target Environment : ", value.Environment)
colorRedBold.Println("│ Suggested Solution : ", value.Solution)
colorRedBold.Println("│")
fmt.Println("│ ")
}
stats.Total++
stats.Dirty++
apiRule.RuleFindings = append(apiRule.RuleFindings, apiFinding)
}
} else {
if !turbo {
colorGreenBold.Println("│ ")
colorGreenBold.Println("│ Compliant")
fmt.Println("│ ")
} else {
colorGreenBold.Print("│ ✓ ", value.ID, " ")
}
stats.Clean++
stats.Total++
}
}
apiCompliance = append(apiCompliance, apiRule)
jsonOutputFile := strings.Join([]string{pwddir, "/", strconv.Itoa(value.ID), ".json"}, "")
jsonoutfile, erroutjson := os.Create(jsonOutputFile)
if erroutjson != nil {
LogError(erroutjson)
}
defer jsonoutfile.Close()
writer := bufio.NewWriter(jsonoutfile)
defer writer.Flush()
codePatternScanJSON := []string{"--pcre2", "--no-heading", "-o", "-p", "-i", "-U", "--json", "-f", searchPatternFile, scanPath}
xcmdJSON := exec.Command(rgembed, codePatternScanJSON...)
xcmdJSON.Stdout = jsonoutfile
xcmdJSON.Stderr = os.Stderr
errrJSON := xcmdJSON.Run()
if errrJSON != nil {
if xcmdJSON.ProcessState.ExitCode() == 2 {
LogError(errrJSON)
} else {
colorGreenBold.Println("│")
os.Remove(jsonOutputFile)
}
} else {
ProcessOutput(strings.Join([]string{strconv.Itoa(value.ID), ".json"}, ""), strconv.Itoa(value.ID), value.Type, value.Name, value.Description, value.Error, value.Solution, value.Fatal)
colorRedBold.Println("│ ")
}
_ = os.Remove(scanPath)
_ = os.Remove(searchPatternFile)
}