2018-08-31 10:53:33 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2018-08-31 12:56:36 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
2018-08-31 10:53:33 +00:00
|
|
|
omd "git.loafle.net/overflow/model/discovery"
|
2018-09-01 13:09:33 +00:00
|
|
|
omu "git.loafle.net/overflow/model/util"
|
2018-08-31 12:56:36 +00:00
|
|
|
osm "git.loafle.net/overflow/service_matcher-go"
|
|
|
|
ouej "git.loafle.net/overflow/util-go/encoding/json"
|
2018-08-31 10:53:33 +00:00
|
|
|
"git.loafle.net/overflow_scanner/probe/discovery/session"
|
2018-08-31 12:56:36 +00:00
|
|
|
"git.loafle.net/overflow_scanner/probe/internal/matcher"
|
2018-08-31 10:53:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func scanTCP(discoverySession session.DiscoverySession, targetPort *omd.Port) error {
|
2018-08-31 12:56:36 +00:00
|
|
|
hostAddress := targetPort.Host.Address
|
|
|
|
portNumber, err := ouej.NumberToInt(targetPort.PortNumber)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Service scan on %s:%s error has occurred %v ", hostAddress, targetPort.PortNumber, err)
|
|
|
|
}
|
2018-09-03 10:33:20 +00:00
|
|
|
if 9100 == portNumber {
|
|
|
|
return nil
|
|
|
|
}
|
2018-08-31 12:56:36 +00:00
|
|
|
|
|
|
|
info := osm.NewMatchInfo(hostAddress, portNumber)
|
|
|
|
connectors := newConnectors()
|
|
|
|
buf := make([]byte, 1024)
|
|
|
|
|
|
|
|
var discoveredMatcher osm.Matcher
|
2018-09-01 13:09:33 +00:00
|
|
|
var discoveredConnector connector
|
2018-08-31 12:56:36 +00:00
|
|
|
|
|
|
|
LOOP:
|
|
|
|
for _, _connector := range connectors {
|
|
|
|
conn, err := _connector.dial(targetPort)
|
|
|
|
if nil != err {
|
|
|
|
continue LOOP
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := conn.SetReadDeadline(time.Now().Add(1 * time.Second)); nil != err {
|
|
|
|
continue LOOP
|
|
|
|
}
|
|
|
|
n, err := conn.Read(buf)
|
|
|
|
if nil != err {
|
|
|
|
n = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
if 0 < n {
|
|
|
|
discoveredMatcher = hadlePrePacket(info, _connector, conn, osm.NewPacket(buf, n))
|
|
|
|
} else {
|
|
|
|
conn.Close()
|
|
|
|
discoveredMatcher = hadlePostPacket(info, _connector, targetPort, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
if nil != discoveredMatcher {
|
|
|
|
if "HTTP" == discoveredMatcher.Key() {
|
|
|
|
hsm := matcher.GetHTTPSubMatchers()
|
|
|
|
if _discoveredMatcher := hadlePostPacket(info, _connector, targetPort, hsm); _discoveredMatcher != nil {
|
|
|
|
discoveredMatcher = _discoveredMatcher
|
|
|
|
}
|
|
|
|
}
|
2018-09-01 13:09:33 +00:00
|
|
|
discoveredConnector = _connector
|
2018-08-31 12:56:36 +00:00
|
|
|
|
|
|
|
break LOOP
|
|
|
|
}
|
2018-09-03 10:33:20 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-discoverySession.StopChan():
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
}
|
2018-08-31 12:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if nil != discoveredMatcher {
|
2018-09-01 13:09:33 +00:00
|
|
|
// log.Printf("discovered matcher: %s(%s) %v", discoveredMatcher.Name(), discoveredMatcher.Key(), discoveredMatcher)
|
2018-09-03 10:33:20 +00:00
|
|
|
discoverySession.AddService(&omd.Service{
|
2018-09-01 13:09:33 +00:00
|
|
|
MetaCryptoType: discoveredConnector.metaCryptoType(),
|
|
|
|
Key: discoveredMatcher.Key(),
|
|
|
|
Name: discoveredMatcher.Name(),
|
|
|
|
Meta: discoveredMatcher.Meta(),
|
|
|
|
DiscoveredDate: omu.NowPtr(),
|
|
|
|
Port: targetPort,
|
|
|
|
})
|
2018-08-31 12:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func hadlePrePacket(info osm.MatchInfo, _connector connector, conn net.Conn, packet *osm.Packet) osm.Matcher {
|
|
|
|
defer func() {
|
|
|
|
conn.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
matchers := matcher.GetTCPMatchers(true)
|
|
|
|
buf := make([]byte, 1024)
|
|
|
|
var discoveredMatcher osm.Matcher
|
|
|
|
|
|
|
|
LOOP:
|
|
|
|
for _, matcher := range matchers {
|
|
|
|
if err := matcher.Match(info, 0, packet); err != nil {
|
|
|
|
continue LOOP
|
|
|
|
}
|
|
|
|
|
|
|
|
packetCount := matcher.PacketCount()
|
|
|
|
|
|
|
|
if 0 == packetCount {
|
|
|
|
return matcher
|
|
|
|
}
|
|
|
|
|
|
|
|
INNER_LOOP:
|
|
|
|
for j := 0; j < packetCount; j++ {
|
|
|
|
_packet := matcher.Packet(j)
|
|
|
|
|
|
|
|
if err := conn.SetWriteDeadline(time.Now().Add(1 * time.Second)); nil != err {
|
|
|
|
return nil
|
|
|
|
}
|
2018-09-03 07:59:17 +00:00
|
|
|
_, err := conn.Write(_packet.Buffer)
|
2018-08-31 12:56:36 +00:00
|
|
|
if nil != err {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := conn.SetReadDeadline(time.Now().Add(1 * time.Second)); nil != err {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
n, err := conn.Read(buf)
|
|
|
|
if nil != err {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := matcher.Match(info, j+1, osm.NewPacket(buf, n)); nil == err {
|
|
|
|
discoveredMatcher = matcher
|
|
|
|
} else {
|
|
|
|
discoveredMatcher = nil
|
|
|
|
break INNER_LOOP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if nil != discoveredMatcher {
|
|
|
|
return discoveredMatcher
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func hadlePostPacket(info osm.MatchInfo, _connector connector, targetPort *omd.Port, limitedMatchers []osm.Matcher) osm.Matcher {
|
|
|
|
matchers := matcher.GetTCPMatchers(false)
|
|
|
|
if nil != limitedMatchers {
|
|
|
|
matchers = limitedMatchers
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, 1024)
|
|
|
|
var discoveredMatcher osm.Matcher
|
|
|
|
|
|
|
|
LOOP:
|
|
|
|
for _, matcher := range matchers {
|
|
|
|
packetCount := matcher.PacketCount()
|
|
|
|
if 0 == packetCount {
|
|
|
|
continue LOOP
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := _connector.dial(targetPort)
|
|
|
|
if nil != err {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
INNER_LOOP:
|
|
|
|
for j := 0; j < packetCount; j++ {
|
|
|
|
_packet := matcher.Packet(j)
|
|
|
|
|
|
|
|
if err := conn.SetWriteDeadline(time.Now().Add(1 * time.Second)); nil != err {
|
|
|
|
break INNER_LOOP
|
|
|
|
}
|
2018-09-03 07:59:17 +00:00
|
|
|
_, err := conn.Write(_packet.Buffer)
|
2018-08-31 12:56:36 +00:00
|
|
|
if nil != err {
|
|
|
|
break INNER_LOOP
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := conn.SetReadDeadline(time.Now().Add(1 * time.Second)); nil != err {
|
|
|
|
break INNER_LOOP
|
|
|
|
}
|
|
|
|
n, err := conn.Read(buf)
|
|
|
|
if nil != err {
|
|
|
|
if !matcher.HasResponse(j) {
|
|
|
|
discoveredMatcher = matcher
|
|
|
|
}
|
|
|
|
|
|
|
|
break INNER_LOOP
|
|
|
|
}
|
|
|
|
|
2018-09-03 10:33:20 +00:00
|
|
|
// log.Printf("res: %s", string(buf[:n]))
|
2018-08-31 12:56:36 +00:00
|
|
|
|
|
|
|
if err := matcher.Match(info, j+1, osm.NewPacket(buf, n)); err == nil {
|
|
|
|
if packetCount-1 == j {
|
|
|
|
discoveredMatcher = matcher
|
|
|
|
|
|
|
|
break INNER_LOOP
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break INNER_LOOP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
conn.Close()
|
|
|
|
if nil != discoveredMatcher {
|
|
|
|
return discoveredMatcher
|
|
|
|
}
|
|
|
|
}
|
2018-08-31 10:53:33 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|