|
| 1 | +// (c) Copyright 2016 Hewlett Packard Enterprise Development LP |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package main |
| 16 | + |
| 17 | +import ( |
| 18 | + "go/ast" |
| 19 | + |
| 20 | + gas "github.com/GoASTScanner/gas/core" |
| 21 | + "github.com/GoASTScanner/gas/rules" |
| 22 | +) |
| 23 | + |
| 24 | +type RuleInfo struct { |
| 25 | + id string |
| 26 | + description string |
| 27 | + build func(string, map[string]interface{}) (gas.Rule, []ast.Node) |
| 28 | +} |
| 29 | + |
| 30 | +// GetFullRuleList get the full list of all rules available to GAS |
| 31 | +func GetFullRuleList() map[string]RuleInfo { |
| 32 | + rules := []RuleInfo{ |
| 33 | + // misc |
| 34 | + RuleInfo{"G101", "Look for hardcoded credentials", rules.NewHardcodedCredentials}, |
| 35 | + RuleInfo{"G102", "Bind to all interfaces", rules.NewBindsToAllNetworkInterfaces}, |
| 36 | + RuleInfo{"G103", "Audit the use of unsafe block", rules.NewUsingUnsafe}, |
| 37 | + RuleInfo{"G104", "Audit errors not checked", rules.NewNoErrorCheck}, |
| 38 | + RuleInfo{"G105", "Audit the use of big.Exp function", rules.NewUsingBigExp}, |
| 39 | + |
| 40 | + // injection |
| 41 | + RuleInfo{"G201", "SQL query construction using format string", rules.NewSqlStrFormat}, |
| 42 | + RuleInfo{"G202", "SQL query construction using string concatenation", rules.NewSqlStrConcat}, |
| 43 | + RuleInfo{"G203", "Use of unescaped data in HTML templates", rules.NewTemplateCheck}, |
| 44 | + RuleInfo{"G204", "Audit use of command execution", rules.NewSubproc}, |
| 45 | + |
| 46 | + // filesystem |
| 47 | + RuleInfo{"G301", "Poor file permissions used when creating a directory", rules.NewMkdirPerms}, |
| 48 | + RuleInfo{"G302", "Poor file permisions used when creation file or using chmod", rules.NewFilePerms}, |
| 49 | + RuleInfo{"G303", "Creating tempfile using a predictable path", rules.NewBadTempFile}, |
| 50 | + |
| 51 | + // crypto |
| 52 | + RuleInfo{"G401", "Detect the usage of DES, RC4, or MD5", rules.NewUsesWeakCryptography}, |
| 53 | + RuleInfo{"G402", "Look for bad TLS connection settings", rules.NewIntermediateTlsCheck}, |
| 54 | + RuleInfo{"G403", "Ensure minimum RSA key length of 2048 bits", rules.NewWeakKeyStrength}, |
| 55 | + RuleInfo{"G404", "Insecure random number source (rand)", rules.NewWeakRandCheck}, |
| 56 | + |
| 57 | + // blacklist |
| 58 | + RuleInfo{"G501", "Import blacklist: crypto/md5", rules.NewBlacklist_crypto_md5}, |
| 59 | + RuleInfo{"G502", "Import blacklist: crypto/des", rules.NewBlacklist_crypto_des}, |
| 60 | + RuleInfo{"G503", "Import blacklist: crypto/rc4", rules.NewBlacklist_crypto_rc4}, |
| 61 | + RuleInfo{"G504", "Import blacklist: net/http/cgi", rules.NewBlacklist_net_http_cgi}, |
| 62 | + } |
| 63 | + ruleMap := make(map[string]RuleInfo) |
| 64 | + for _, v := range rules { |
| 65 | + ruleMap[v.id] = v |
| 66 | + } |
| 67 | + return ruleMap |
| 68 | +} |
| 69 | + |
| 70 | +func AddRules(analyzer *gas.Analyzer, conf map[string]interface{}) { |
| 71 | + var all map[string]RuleInfo |
| 72 | + |
| 73 | + inc := conf["include"].([]string) |
| 74 | + exc := conf["exclude"].([]string) |
| 75 | + |
| 76 | + // add included rules |
| 77 | + if len(inc) == 0 { |
| 78 | + all = GetFullRuleList() |
| 79 | + } else { |
| 80 | + all = map[string]RuleInfo{} |
| 81 | + tmp := GetFullRuleList() |
| 82 | + for _, v := range inc { |
| 83 | + if val, ok := tmp[v]; ok { |
| 84 | + all[v] = val |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // remove excluded rules |
| 90 | + for _, v := range exc { |
| 91 | + delete(all, v) |
| 92 | + } |
| 93 | + |
| 94 | + for k, v := range all { |
| 95 | + analyzer.AddRule(v.build(k, conf)) |
| 96 | + } |
| 97 | +} |
0 commit comments