fix: Telnet 弱口令误报,Cisco MOTD 横幅触发 shell prompt 误判 (Closes #590)

isShellPrompt 使用 Contains 匹配 # $ > 单字符,Cisco IOS MOTD 横幅中
的装饰线(###)和文本内容会误触发,导致未发送凭据就判定认证成功。

重写 isShellPrompt 改为行尾匹配,排除全同字符装饰线;
performTelnetAuth 等待 login prompt 阶段移除 isShellPrompt 检查,
未授权检测由 testUnauthAccess 专门负责。
This commit is contained in:
ZacharyZcR
2026-06-27 16:35:18 +08:00
parent ed45d0ead5
commit a3ccc2827b
2 changed files with 84 additions and 10 deletions
+37 -8
View File
@@ -297,10 +297,6 @@ func (p *TelnetPlugin) performTelnetAuth(conn net.Conn, username, password strin
cleaned := p.cleanResponse(response)
cleanedLower := strings.ToLower(cleaned)
if p.isShellPrompt(cleaned) {
return true
}
if strings.Contains(cleanedLower, "login") ||
strings.Contains(cleanedLower, "username") ||
strings.Contains(cleaned, ":") {
@@ -429,16 +425,49 @@ func (p *TelnetPlugin) isShellPrompt(data string) bool {
return false
}
data = strings.ToLower(strings.TrimSpace(data))
data = strings.TrimSpace(data)
shellPrompts := []string{"$", "#", ">", "~$", "]$", ")#", "bash", "shell", "cmd"}
for _, line := range strings.Split(data, "\n") {
line = strings.TrimSpace(line)
if line == "" {
continue
}
for _, prompt := range shellPrompts {
if strings.Contains(data, prompt) {
lineLower := strings.ToLower(line)
// 关键字匹配(整行包含即可)
for _, kw := range []string{"bash", "shell", "cmd"} {
if strings.Contains(lineLower, kw) {
return true
}
}
// 行尾 prompt 符号匹配:取最后一个非空格字符
trimmed := strings.TrimRight(line, " ")
if len(trimmed) == 0 {
continue
}
tail := trimmed[len(trimmed)-1]
if tail != '#' && tail != '$' && tail != '>' {
continue
}
// 排除装饰线:整行都是同一个字符(如 #### 或 >>>>
allSame := true
for _, c := range trimmed {
if byte(c) != tail {
allSame = false
break
}
}
if allSame {
continue
}
return true
}
return false
}
+45
View File
@@ -37,3 +37,48 @@ func TestClassifyTelnetErrorType(t *testing.T) {
})
}
}
func TestIsShellPrompt(t *testing.T) {
p := NewTelnetPlugin()
positive := []struct {
name, data string
}{
{"linux root", "root@host:~#"},
{"linux user", "user@host:~$"},
{"cisco", "Router>"},
{"cisco enable", "Router#"},
{"bracket prompt", "[admin@host ~]$"},
{"paren prompt", "host(config)#"},
{"bash keyword", "bash-4.2$"},
{"trailing space", "root@host:~# "},
{"multiline last", "Welcome\nroot@host:~#"},
}
negative := []struct {
name, data string
}{
{"empty", ""},
{"decoration hashes", "################"},
{"decoration arrows", ">>>>>>>>"},
{"decoration dollars", "$$$$$$$$"},
{"cisco motd border", "###################################################"},
{"motd with hash mid", "# Welcome to Cisco IOS"},
{"plain text", "Cisco IOS Software, Version 12.2"},
{"login prompt", "Login:"},
{"password prompt", "Password:"},
{"motd multiline", "##########\nWelcome to Router\n##########"},
}
for _, tt := range positive {
if !p.isShellPrompt(tt.data) {
t.Errorf("isShellPrompt(%q) = false, want true [%s]", tt.data, tt.name)
}
}
for _, tt := range negative {
if p.isShellPrompt(tt.data) {
t.Errorf("isShellPrompt(%q) = true, want false [%s]", tt.data, tt.name)
}
}
}