Skip to content

Commit d3c3cd6

Browse files
author
Cosmin Cojocar
committed
Add a rule to detect the usage of ssh InsecureIgnoreHostKey function
1 parent 8b87505 commit d3c3cd6

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed

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: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 rules
16+
17+
import (
18+
"go/ast"
19+
20+
"github.com/GoASTScanner/gas"
21+
)
22+
23+
type sshHostKey struct {
24+
gas.MetaData
25+
pkg string
26+
calls []string
27+
}
28+
29+
func (r *sshHostKey) Match(n ast.Node, c *gas.Context) (gi *gas.Issue, err error) {
30+
if _, matches := gas.MatchCallByPackage(n, c, r.pkg, r.calls...); matches {
31+
return gas.NewIssue(c, n, r.What, r.Severity, r.Confidence), nil
32+
}
33+
return nil, nil
34+
}
35+
36+
// NewSSHHostKey rule detects the use of insecure ssh HostKeyCallback.
37+
func NewSSHHostKey(conf gas.Config) (gas.Rule, []ast.Node) {
38+
return &sshHostKey{
39+
pkg: "golang.org/x/crypto/ssh",
40+
calls: []string{"InsecureIgnoreHostKey"},
41+
MetaData: gas.MetaData{
42+
What: "Use of ssh InsecureIgnoreHostKey should be audited",
43+
Severity: gas.Medium,
44+
Confidence: gas.High,
45+
},
46+
}, []ast.Node{(*ast.CallExpr)(nil)}
47+
}

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)