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-09-03 13:36:57 +00:00
|
|
|
func (m *FTPMatcher) Key() string {
|
2018-08-13 07:48:32 +00:00
|
|
|
return "FTP"
|
|
|
|
}
|
|
|
|
|
2018-09-12 04:26:27 +00:00
|
|
|
func (m *FTPMatcher) Type() string {
|
|
|
|
return "NETWORK"
|
|
|
|
}
|
|
|
|
|
2018-09-03 13:36:57 +00:00
|
|
|
func (m *FTPMatcher) Name(matchCtx *osm.MatchCtx) string {
|
2018-08-13 07:48:32 +00:00
|
|
|
return "FTP"
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|