Skip to content

Commit

Permalink
feat: add flag to disable ssl verify
Browse files Browse the repository at this point in the history
  • Loading branch information
rgmz authored and Richard Gomez committed Jul 1, 2024
1 parent 980d783 commit ea06b39
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 18 deletions.
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ var (
includeDetectors = cli.Flag("include-detectors", "Comma separated list of detector types to include. Protobuf name or IDs may be used, as well as ranges.").Default("all").String()
excludeDetectors = cli.Flag("exclude-detectors", "Comma separated list of detector types to exclude. Protobuf name or IDs may be used, as well as ranges. IDs defined here take precedence over the include list.").String()
jobReportFile = cli.Flag("output-report", "Write a scan report to the provided path.").Hidden().OpenFile(os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
SslVerify = cli.Flag("ssl-verify", "Whether to verify the SSL certificates when making requests.").Default("true").Bool()

gitScan = cli.Command("git", "Find credentials in git repositories.")
gitScanURI = gitScan.Arg("uri", "Git repository URL. https://, file://, or ssh:// schema expected.").Required().String()
Expand Down Expand Up @@ -250,12 +251,18 @@ func init() {

cmd = kingpin.MustParse(cli.Parse(os.Args[1:]))

// Configure log level.
switch {
case *trace:
log.SetLevel(5)
case *debug:
log.SetLevel(2)
}

// Disable certificate validation, if specified.
if !*SslVerify {
common.VerifySsl = false
}
}

func main() {
Expand Down
38 changes: 24 additions & 14 deletions pkg/common/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,23 +196,33 @@ func RetryableHTTPClientTimeout(timeOutSeconds int64, opts ...ClientOption) *htt

const DefaultResponseTimeout = 5 * time.Second

var saneTransport = &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 2 * time.Second,
KeepAlive: 5 * time.Second,
}).DialContext,
MaxIdleConns: 5,
IdleConnTimeout: 5 * time.Second,
TLSHandshakeTimeout: 3 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
var VerifySsl = true

func saneTransport() *http.Transport {
t := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 2 * time.Second,
KeepAlive: 5 * time.Second,
}).DialContext,
MaxIdleConns: 5,
IdleConnTimeout: 5 * time.Second,
TLSHandshakeTimeout: 3 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}

// Disable TLS certificate validation.
if !VerifySsl {
t.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
}
return t
}

func SaneHttpClient() *http.Client {
httpClient := &http.Client{}
httpClient.Timeout = DefaultResponseTimeout
httpClient.Transport = NewCustomTransport(saneTransport)
return httpClient
client := &http.Client{}
client.Timeout = DefaultResponseTimeout
client.Transport = NewCustomTransport(saneTransport())
return client
}

// SaneHttpClientTimeOut adds a custom timeout for some scanners
Expand Down
13 changes: 11 additions & 2 deletions pkg/detectors/jdbc/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"strings"

"github.com/go-sql-driver/mysql"

"github.com/trufflesecurity/trufflehog/v3/pkg/common"
)

type mysqlJDBC struct {
Expand Down Expand Up @@ -49,20 +51,27 @@ func isMySQLErrorDeterminate(err error) bool {
return false
}

const defaultParams = "timeout=5s"

func parseMySQL(subname string) (jdbc, error) {
// expected form: [subprotocol:]//[user:password@]HOST[/DB][?key=val[&key=val]]
if !strings.HasPrefix(subname, "//") {
return nil, errors.New("expected host to start with //")
}

params := defaultParams
if !common.VerifySsl {
params = defaultParams + "&tls=skip-verify"
}

// need for hostnames that have tcp(host:port) format required by this database driver
cfg, err := mysql.ParseDSN(strings.TrimPrefix(subname, "//"))
if err == nil {
return &mysqlJDBC{
conn: subname[2:],
userPass: cfg.User + ":" + cfg.Passwd,
host: fmt.Sprintf("tcp(%s)", cfg.Addr),
params: "timeout=5s",
params: params,
}, nil
}

Expand Down Expand Up @@ -95,7 +104,7 @@ func parseMySQL(subname string) (jdbc, error) {
conn: subname[2:],
userPass: userAndPass,
host: fmt.Sprintf("tcp(%s)", u.Host),
params: "timeout=5s",
params: params,
}, nil

}
6 changes: 5 additions & 1 deletion pkg/detectors/jdbc/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"strings"

"github.com/lib/pq"

"github.com/trufflesecurity/trufflehog/v3/pkg/common"
)

type postgresJDBC struct {
Expand Down Expand Up @@ -88,7 +90,9 @@ func parsePostgres(subname string) (jdbc, error) {
}
}

if v := u.Query()["sslmode"]; len(v) > 0 {
if !common.VerifySsl {
params["sslmode"] = "disable"
} else if v := u.Query()["sslmode"]; len(v) > 0 {
switch v[0] {
// https://www.postgresql.org/docs/current/libpq-ssl.html#LIBPQ-SSL-PROTECTION
case "disable", "allow", "prefer",
Expand Down
4 changes: 3 additions & 1 deletion pkg/detectors/jdbc/sqlserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"strings"

mssql "github.com/microsoft/go-mssqldb"

"github.com/trufflesecurity/trufflehog/v3/pkg/common"
)

type sqlServerJDBC struct {
Expand Down Expand Up @@ -66,6 +68,6 @@ func parseSqlServer(subname string) (jdbc, error) {
}
}
return &sqlServerJDBC{
connStr: fmt.Sprintf("sqlserver://sa:%s@%s:%s?database=master&connection+timeout=5", password, host, port),
connStr: fmt.Sprintf("sqlserver://sa:%s@%s:%s?database=master&connection+timeout=5&TrustServerCertificate=%t", password, host, port, common.VerifySsl),
}, nil
}

0 comments on commit ea06b39

Please sign in to comment.