Skip to content

Commit a72a21b

Browse files
authored
Merge pull request #164 from cosmincojocar/ssh_rule
Add a rule to audit the usage of ssh.InsecureIgnoreHostKey
2 parents 8b87505 + a7cdd9c commit a72a21b

File tree

6 files changed

+49
-0
lines changed

6 files changed

+49
-0
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ go:
77
install:
88
- go get -v github.com/onsi/ginkgo/ginkgo
99
- go get -v github.com/onsi/gomega
10+
- go get -v golang.org/x/crypto/ssh
1011
- go get -v -t ./...
1112
- export PATH=$PATH:$HOME/gopath/bin
1213

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ or to specify a set of rules to explicitly exclude using the '-exclude=' flag.
4141
- G103: Audit the use of unsafe block
4242
- G104: Audit errors not checked
4343
- G105: Audit the use of math/big.Int.Exp
44+
- G106: Audit the use of ssh.InsecureIgnoreHostKey
4445
- G201: SQL query construction using format string
4546
- G202: SQL query construction using string concatenation
4647
- G203: Use of unescaped data in HTML templates

rules/rulelist.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ func Generate(filters ...RuleFilter) RuleList {
6565
"G103": RuleDefinition{"Audit the use of unsafe block", NewUsingUnsafe},
6666
"G104": RuleDefinition{"Audit errors not checked", NewNoErrorCheck},
6767
"G105": RuleDefinition{"Audit the use of big.Exp function", NewUsingBigExp},
68+
"G106": RuleDefinition{"Audit the use of ssh.InsecureIgnoreHostKey function", NewSSHHostKey},
6869

6970
// injection
7071
"G201": RuleDefinition{"SQL query construction using format string", NewSQLStrFormat},

rules/rules_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ var _ = Describe("gas rules", func() {
6565
runner("G105", testutils.SampleCodeG105)
6666
})
6767

68+
It("should detect of ssh.InsecureIgnoreHostKey function", func() {
69+
runner("G106", testutils.SampleCodeG106)
70+
})
71+
6872
It("should detect sql injection via format strings", func() {
6973
runner("G201", testutils.SampleCodeG201)
7074
})

rules/ssh.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package rules
2+
3+
import (
4+
"go/ast"
5+
6+
"github.com/GoASTScanner/gas"
7+
)
8+
9+
type sshHostKey struct {
10+
gas.MetaData
11+
pkg string
12+
calls []string
13+
}
14+
15+
func (r *sshHostKey) Match(n ast.Node, c *gas.Context) (gi *gas.Issue, err error) {
16+
if _, matches := gas.MatchCallByPackage(n, c, r.pkg, r.calls...); matches {
17+
return gas.NewIssue(c, n, r.What, r.Severity, r.Confidence), nil
18+
}
19+
return nil, nil
20+
}
21+
22+
// NewSSHHostKey rule detects the use of insecure ssh HostKeyCallback.
23+
func NewSSHHostKey(conf gas.Config) (gas.Rule, []ast.Node) {
24+
return &sshHostKey{
25+
pkg: "golang.org/x/crypto/ssh",
26+
calls: []string{"InsecureIgnoreHostKey"},
27+
MetaData: gas.MetaData{
28+
What: "Use of ssh InsecureIgnoreHostKey should be audited",
29+
Severity: gas.Medium,
30+
Confidence: gas.High,
31+
},
32+
}, []ast.Node{(*ast.CallExpr)(nil)}
33+
}

testutils/source.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,15 @@ func main() {
183183
z = z.Exp(x, y, m)
184184
}`, 1}}
185185

186+
// SampleCodeG106 - ssh InsecureIgnoreHostKey
187+
SampleCodeG106 = []CodeSample{{`
188+
package main
189+
import (
190+
"golang.org/x/crypto/ssh"
191+
)
192+
func main() {
193+
_ = ssh.InsecureIgnoreHostKey()
194+
}`, 1}}
186195
// SampleCodeG201 - SQL injection via format string
187196
SampleCodeG201 = []CodeSample{
188197
{`

0 commit comments

Comments
 (0)