Skip to content

Commit

Permalink
fix anti honeypot detector
Browse files Browse the repository at this point in the history
  • Loading branch information
hugefiver committed Dec 7, 2021
1 parent 3a2e504 commit dd156ba
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
39 changes: 25 additions & 14 deletions third/crypto/ssh/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,29 +249,40 @@ func (s *connection) serverHandshake(config *ServerConfig) (*Permissions, error)
} else {
s.clientVersion, err = exchangeVersions(s.sshConn.conn, s.serverVersion)
}
if err != nil {
return nil, err
}

// precheck version string
var major, minor int
n, err := fmt.Sscanf(string(s.clientVersion), "SSH-%d.%d", &major, &minor)
if err != nil || n != 2 {
// if unexpect character in version string
if err == errInvalidChar {
s.sshConn.conn.Write([]byte("Invalid SSH identification string.\r\n"))
err := s.sshConn.Close()
if err == nil {
err = errors.New("client version format invalid")
}
return nil, err
} else if major != 2 && minor != 0 {
s.sshConn.conn.Write([]byte("Protocol major versions differ.\r\n"))
err := s.sshConn.Close()
if err == nil {
err = errors.New("client major version don't match")
}
}
if err != nil {
return nil, err
}

// openssh: precheck version string
if config.AsOpenSSH {
var major, minor int
n, err := fmt.Sscanf(string(s.clientVersion), "SSH-%d.%d", &major, &minor)
if err != nil || n != 2 {
s.sshConn.conn.Write([]byte("Invalid SSH identification string.\r\n"))
err := s.sshConn.Close()
if err == nil {
err = errors.New("client version format invalid")
}
return nil, err
} else if major != 2 && minor != 0 {
s.sshConn.conn.Write([]byte("Protocol major versions differ.\r\n"))
err := s.sshConn.Close()
if err == nil {
err = errors.New("client major version don't match")
}
return nil, err
}
}

if config.CheckClientVersion != nil && !config.CheckClientVersion(s.clientVersion) {
// s.sshConn.conn.Write([]byte("Invalid SSH identification string.\r\n"))
s.sshConn.conn.Write([]byte("Protocol mismatch.\r\n"))
Expand Down
6 changes: 6 additions & 0 deletions third/crypto/ssh/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,8 @@ func readVersion(r io.Reader) ([]byte, error) {
return versionString, nil
}

var errInvalidChar = errors.New("invalid character in version string")

func readVersionOpenSSH(rw io.ReadWriter) ([]byte, error) {
versionString := make([]byte, 0, 64)
var ok bool
Expand Down Expand Up @@ -402,6 +404,10 @@ func readVersionOpenSSH(rw io.ReadWriter) ([]byte, error) {
break loop
}

if buf[0] < 32 {
return nil, errInvalidChar
}

// if last char is '\r', it's not allowed
if lastCR {
rw.Write([]byte("Protocol mismatch.\r\n"))
Expand Down

0 comments on commit dd156ba

Please sign in to comment.