Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect windows SSH from server header #177

Merged
merged 1 commit into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions protocol/ssh/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ func (c *Connection) Disconnect() {
c.client.Close()
}

func boolptr(b bool) *bool {
return &b
}

// IsWindows is true when the host is running windows.
func (c *Connection) IsWindows() bool {
if c.isWindows != nil {
Expand All @@ -225,21 +229,23 @@ func (c *Connection) IsWindows() bool {
return false
}

var isWin bool
if strings.Contains(string(c.client.ServerVersion()), "Windows") {
isWin = true
c.isWindows = &isWin
return true
}
serverVersion := strings.ToLower(string(c.client.ServerVersion()))
log.Trace(context.Background(), "checking if host is windows", "server_version", serverVersion)

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
switch {
case strings.Contains(serverVersion, "windows"):
c.isWindows = boolptr(true)
case isKnownPosix(serverVersion):
c.isWindows = boolptr(false)
default:
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

isWinProc, err := c.StartProcess(ctx, "cmd.exe /c exit 0", nil, nil, nil)
isWin = err == nil && isWinProc.Wait() == nil
isWinProc, err := c.StartProcess(ctx, "ver.exe", nil, nil, nil)
c.isWindows = boolptr(err == nil && isWinProc.Wait() == nil)
}

c.isWindows = &isWin
log.Trace(context.Background(), fmt.Sprintf("host is windows: %t", *c.isWindows), log.KeyHost, c)
log.Trace(context.Background(), fmt.Sprintf("host is windows: %t", *c.isWindows))

return *c.isWindows
}
Expand Down
45 changes: 45 additions & 0 deletions protocol/ssh/knownposix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package ssh

import "strings"

// A list of known ssh server version substrings that are used to aid in
// identifying if a server is windows or not.

var knownPosix = []string{
"linux",
"darwin",
"bsd",
"unix",
"alpine",
"ubuntu",
"debian",
"suse",
"oracle",
"rhel",
"rocky",
"sles",
"fedora",
"amzn",
"arch",
"centos",
"bodhi",
"elementary",
"gentoo",
"kali",
"mageia",
"manjaro",
"slackware",
"solaris",
"illumos",
"aix",
"dragonfly",
}

func isKnownPosix(s string) bool {
for _, v := range knownPosix {
if strings.Contains(s, v) {
return true
}
}
return false
}