zgrab2/modules/mysql.go

111 lines
2.6 KiB
Go
Raw Normal View History

2017-11-27 18:00:31 +00:00
package modules
import (
log "github.com/sirupsen/logrus"
2017-11-27 18:00:31 +00:00
"github.com/zmap/zgrab2"
"github.com/zmap/zgrab2/lib/mysql"
)
// HandshakeLog contains detailed information about each step of the
// MySQL handshake, and can be encoded to JSON.
type MySQLScanResults struct {
mysql.ConnectionLog
TLSLog *zgrab2.TLSLog `json:"tls,omitempty"`
}
2017-11-27 18:00:31 +00:00
type MySQLFlags struct {
zgrab2.BaseFlags
zgrab2.TLSFlags
2017-12-15 15:19:25 +00:00
Verbose bool `long:"verbose" description:"More verbose logging, include debug fields in the scan results"`
2017-11-27 18:00:31 +00:00
}
type MySQLModule struct {
}
type MySQLScanner struct {
config *MySQLFlags
}
func init() {
var module MySQLModule
_, err := zgrab2.AddCommand("mysql", "MySQL", "Grab a MySQL handshake", 3306, &module)
if err != nil {
log.Fatal(err)
2017-11-27 18:00:31 +00:00
}
}
func (m *MySQLModule) NewFlags() interface{} {
return new(MySQLFlags)
}
func (m *MySQLModule) NewScanner() zgrab2.Scanner {
return new(MySQLScanner)
}
func (f *MySQLFlags) Validate(args []string) error {
return nil
}
func (f *MySQLFlags) Help() string {
return ""
}
func (s *MySQLScanner) Init(flags zgrab2.ScanFlags) error {
f, _ := flags.(*MySQLFlags)
s.config = f
return nil
}
func (s *MySQLScanner) InitPerSender(senderID int) error {
return nil
}
func (s *MySQLScanner) GetName() string {
return s.config.Name
}
func (s *MySQLScanner) GetPort() uint {
return s.config.Port
}
func (s *MySQLScanner) Scan(t zgrab2.ScanTarget) (status zgrab2.ScanStatus, result interface{}, thrown error) {
Implements postgres zgrab2 module (#30) * remove unnecessary indirection on net.Conn * Ignore *.pyc * fix NPE on nil handshake * refactoring -- move status to status.go; add Open() methods for ScanTarget * cherry-pick .gitignore fix * pull in TLS fix * status.go comments * trim over-generalizations * use /usr/bin/env bash instead of absolute path * remove debug tcpwrap * add integration tests for postgres * hack for cleanup.sh to work on mingw -- use //var/lib instead of /var/lib * cleanup should actually stop the process though * comments / rearrange * Bump up timeout in postgres tests; only pass user if explicitly requested to do so * add schema stubs to new.sh * Integration test fixes -- use /usr/bin/env bash; log all validation failures * add postgres schemas * fill out zcrypto.client_hello schema * handle early get of TLSLog * postgres: return SCAN_SUCCESS on success * cleanup * fix new.sh * fix typo * postgres container cleanup * build.sh docs * standardize container/image names * add not to check for success * shift mysql's connection management to ScanTarget.Open(); wrap Read/Write methods returned by ScanTarget.Open() to enforce timeouts * catch schematically-valid but non-successful scans * postgres: clean up output format; more scanning * cleanup; better error handling; get detailed protocol version error * refactor modules * clean up dangling connections * split gigantic postgres.go * remove unused * ServerParams gets its own type * refactor integration tests: run zgrab2 in its own container, which is linked to the service containers, so that we don't need to keep track of unique ports on the host any more * rename entrypoint; remove duplicate postgres tests * comments for postgres schema * Use param expansion to check for env variable [minor] This is a *very* minor change to `docker-runner/docker-run.sh` checks to see if the environment variable required to run the script has been set to a non-empty string. If not, the script exits with a non-zero status code and displays a default message: ``` ❯ docker-runner/docker-run.sh docker-runner/docker-run.sh: line 7: CONTAINER_NAME: parameter null or not set ``` This was the behavior before, but just uses a one-liner declarative bash idiom. For further reading on parameter expansion, see https://stackoverflow.com/a/307735. @justinbastress can tell me if I did something wrong and broke the intent of the script :-) * Add integration_test targets to makefile; use makefile instead of directly calling go build everywhere; run postgres schema through PEP8 linter * use make in docker-runner entrypoint * add .integration_test_setup to .gitignore * more .gitignore items * Makefile updates: Windows support; add docker-runner target; better cleanup. * docker-runner Dockerfile: start from zgrab2_runner_base image * cleanup postgres setup * make travis use make * add .gitattributes, try to prevent it from overriding lfs with crlfs in shell scripts at least * fix folder name in Makefile * update go (one of our dependencies now works only with >= 1.9) * From travis: `I don't have any idea what to do with '1.9.0'.` * explicit clean make * fix dep order * fix build.sh location * popd * use make to ensure zgrab2_runner exists * Make docker-runner an order-dependency for integration-test-cleanup; don't do a cleanup after each integration test * use explicit tag name for zgrab2_runner * Add container-clean target to Makefile, to remove cyclic dependency on docker; use .id files to track docker images; add servce-base image; use Make to build / track images * use LF in Makefiles; update .gitignore; use zgrab_service_base image in ssh container; fix line endings (?) * remove overzealous cleanup * let setup continue even if some containers are already running * zgrab depends on *.go * docker-runner depends on zgrab2 binary * clean output before running integration tests
2018-01-15 19:24:57 +00:00
sql := mysql.NewConnection(&mysql.Config{})
result = &MySQLScanResults{}
defer func() {
recovered := recover()
if recovered != nil {
thrown = recovered.(error)
2017-12-15 16:03:06 +00:00
status = zgrab2.TryGetScanStatus(thrown)
// TODO FIXME: do more to distinguish errors
}
result.(*MySQLScanResults).ConnectionLog = sql.ConnectionLog
}()
2017-11-27 18:00:31 +00:00
defer sql.Disconnect()
2017-12-15 16:03:06 +00:00
var err error
Implements postgres zgrab2 module (#30) * remove unnecessary indirection on net.Conn * Ignore *.pyc * fix NPE on nil handshake * refactoring -- move status to status.go; add Open() methods for ScanTarget * cherry-pick .gitignore fix * pull in TLS fix * status.go comments * trim over-generalizations * use /usr/bin/env bash instead of absolute path * remove debug tcpwrap * add integration tests for postgres * hack for cleanup.sh to work on mingw -- use //var/lib instead of /var/lib * cleanup should actually stop the process though * comments / rearrange * Bump up timeout in postgres tests; only pass user if explicitly requested to do so * add schema stubs to new.sh * Integration test fixes -- use /usr/bin/env bash; log all validation failures * add postgres schemas * fill out zcrypto.client_hello schema * handle early get of TLSLog * postgres: return SCAN_SUCCESS on success * cleanup * fix new.sh * fix typo * postgres container cleanup * build.sh docs * standardize container/image names * add not to check for success * shift mysql's connection management to ScanTarget.Open(); wrap Read/Write methods returned by ScanTarget.Open() to enforce timeouts * catch schematically-valid but non-successful scans * postgres: clean up output format; more scanning * cleanup; better error handling; get detailed protocol version error * refactor modules * clean up dangling connections * split gigantic postgres.go * remove unused * ServerParams gets its own type * refactor integration tests: run zgrab2 in its own container, which is linked to the service containers, so that we don't need to keep track of unique ports on the host any more * rename entrypoint; remove duplicate postgres tests * comments for postgres schema * Use param expansion to check for env variable [minor] This is a *very* minor change to `docker-runner/docker-run.sh` checks to see if the environment variable required to run the script has been set to a non-empty string. If not, the script exits with a non-zero status code and displays a default message: ``` ❯ docker-runner/docker-run.sh docker-runner/docker-run.sh: line 7: CONTAINER_NAME: parameter null or not set ``` This was the behavior before, but just uses a one-liner declarative bash idiom. For further reading on parameter expansion, see https://stackoverflow.com/a/307735. @justinbastress can tell me if I did something wrong and broke the intent of the script :-) * Add integration_test targets to makefile; use makefile instead of directly calling go build everywhere; run postgres schema through PEP8 linter * use make in docker-runner entrypoint * add .integration_test_setup to .gitignore * more .gitignore items * Makefile updates: Windows support; add docker-runner target; better cleanup. * docker-runner Dockerfile: start from zgrab2_runner_base image * cleanup postgres setup * make travis use make * add .gitattributes, try to prevent it from overriding lfs with crlfs in shell scripts at least * fix folder name in Makefile * update go (one of our dependencies now works only with >= 1.9) * From travis: `I don't have any idea what to do with '1.9.0'.` * explicit clean make * fix dep order * fix build.sh location * popd * use make to ensure zgrab2_runner exists * Make docker-runner an order-dependency for integration-test-cleanup; don't do a cleanup after each integration test * use explicit tag name for zgrab2_runner * Add container-clean target to Makefile, to remove cyclic dependency on docker; use .id files to track docker images; add servce-base image; use Make to build / track images * use LF in Makefiles; update .gitignore; use zgrab_service_base image in ssh container; fix line endings (?) * remove overzealous cleanup * let setup continue even if some containers are already running * zgrab depends on *.go * docker-runner depends on zgrab2 binary * clean output before running integration tests
2018-01-15 19:24:57 +00:00
conn, err := t.Open(&s.config.BaseFlags)
if err != nil {
panic(err)
}
if err = sql.Connect(conn); err != nil {
panic(err)
2017-11-27 18:00:31 +00:00
}
if sql.SupportsTLS() {
2017-12-15 16:03:06 +00:00
if err = sql.NegotiateTLS(); err != nil {
panic(err)
}
var conn *zgrab2.TLSConnection
2017-12-15 16:03:06 +00:00
if conn, err = s.config.TLSFlags.GetTLSConnection(sql.Connection); err != nil {
panic(err)
}
2017-12-15 16:03:06 +00:00
// Following the example of the SSH module, allow the possibility of failing while still returning a (perhaps incomplete) log
result.(*MySQLScanResults).TLSLog = conn.GetLog()
2017-12-15 16:03:06 +00:00
if err = conn.Handshake(); err != nil {
panic(err)
}
// Replace sql.Connection to allow hypothetical future calls to go over the secure connection
sql.Connection = conn
}
2017-12-15 16:03:06 +00:00
// If we made it this far, the scan was a success.
return zgrab2.SCAN_SUCCESS, result, nil
2017-11-27 18:00:31 +00:00
}