service_matcher-go/ftp/ftp.go

121 lines
2.5 KiB
Go
Raw Permalink Normal View History

2018-08-13 07:48:32 +00:00
package ftp
import (
"strings"
2018-08-15 07:17:18 +00:00
osm "git.loafle.net/overflow/service_matcher-go"
2018-08-13 07:48:32 +00:00
)
// FTP Status codes, defined in RFC 959
const (
statusReadyServer = "120"
statusOK = "200"
statusNewConnectOK = "220"
statusSystemNameOK = "215"
statusCloseConnect = "221"
statusUnkownCMD = "202"
statusTlsUseOK = "234"
statusCloseControlConnect = "421"
statusSyntaxErr = "500"
statusParamSyntaxErr = "501"
statusNotUseCMD = "502"
statusIncorrectCMD = "503"
statusNotLoggedIn = "530"
statusTlsNotUse = "534"
statusNeedUserId = "332"
)
type FTPMatcher struct {
2018-08-15 07:17:18 +00:00
osm.Matchers
2018-08-13 07:48:32 +00:00
}
2018-10-23 04:31:25 +00:00
func (m *FTPMatcher) Key(matchCtx *osm.MatchCtx) string {
2018-08-13 07:48:32 +00:00
return "FTP"
}
2018-10-23 04:31:25 +00:00
func (m *FTPMatcher) Type(matchCtx *osm.MatchCtx) string {
2018-09-12 04:26:27 +00:00
return "NETWORK"
}
func (m *FTPMatcher) Vendor(matchCtx *osm.MatchCtx) string {
2018-10-23 05:11:16 +00:00
v := "UNKNOWN"
if _v, ok := matchCtx.GetAttribute("comment"); ok {
if strings.Contains(_v, "Pure-FTPd") {
v = "Pure-FTPd"
}
}
return v
}
func (m *FTPMatcher) Version(matchCtx *osm.MatchCtx) string {
return "UNKNOWN"
}
func (m *FTPMatcher) OsType(matchCtx *osm.MatchCtx) string {
return "UNKNOWN"
}
func (m *FTPMatcher) OsVersion(matchCtx *osm.MatchCtx) string {
return "UNKNOWN"
}
2018-09-12 04:26:27 +00:00
2018-09-03 13:36:57 +00:00
func (m *FTPMatcher) Name(matchCtx *osm.MatchCtx) string {
2018-10-23 05:07:35 +00:00
name := "FTP"
if v, ok := matchCtx.GetAttribute("comment"); ok {
if strings.Contains(v, "Pure-FTPd") {
name = "Pure-FTPd"
}
}
return name
2018-08-13 07:48:32 +00:00
}
2018-09-03 13:41:28 +00:00
func (m *FTPMatcher) IsPrePacket() bool {
2018-08-13 07:48:32 +00:00
return true
}
2018-09-03 13:36:57 +00:00
func (m *FTPMatcher) HasResponse(matchCtx *osm.MatchCtx, index int) bool {
2018-08-13 07:48:32 +00:00
return true
}
2018-09-03 13:36:57 +00:00
func (m *FTPMatcher) IsError(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) bool {
2018-08-13 07:48:32 +00:00
return false
}
2018-09-03 13:36:57 +00:00
func (m *FTPMatcher) Match(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) error {
2018-08-13 07:48:32 +00:00
2018-09-03 06:42:56 +00:00
if packet == nil || !packet.Valid() {
2018-08-15 07:17:18 +00:00
return osm.NoPacketReceivedError()
2018-08-13 07:48:32 +00:00
}
2018-09-03 06:42:56 +00:00
str := strings.Split(string(packet.Bytes()), "\r\n")[0]
2018-08-13 07:48:32 +00:00
if len(str) < 4 {
2018-08-15 07:17:18 +00:00
return osm.NotMatchedError()
2018-08-13 07:48:32 +00:00
}
code := str[:3]
if index == 0 {
switch code {
case statusNewConnectOK, statusReadyServer:
2018-09-03 13:36:57 +00:00
matchCtx.SetAttribute("comment", str[4:])
2018-08-13 07:48:32 +00:00
return nil
}
} else if index == 1 {
switch code {
case statusCloseConnect, statusSyntaxErr:
return nil
}
}
2018-08-15 07:17:18 +00:00
return osm.NotMatchedError()
2018-08-13 07:48:32 +00:00
}
2018-08-15 07:17:18 +00:00
func NewMatcher() osm.Matcher {
2018-08-13 07:48:32 +00:00
m := &FTPMatcher{}
quitStr := "QUIT\r\n"
quitByte := make([]byte, len(quitStr))
copy(quitByte[:], quitStr)
2018-08-15 07:17:18 +00:00
m.AddPacket(osm.NewPacket(quitByte, len(quitStr)))
2018-08-13 07:48:32 +00:00
return m
}