From c9585c36afe0c502c91e5ad527af1c71a80feb0a Mon Sep 17 00:00:00 2001 From: Marin Ivanov Date: Sun, 24 Mar 2019 22:21:08 +0200 Subject: Add Makefile and configurable delay --- .gitignore | 1 + Makefile | 8 ++++++++ handle.go | 7 ++++--- main.go | 14 +++++++++++--- ssh.go | 6 ++---- 5 files changed, 26 insertions(+), 10 deletions(-) create mode 100644 .gitignore create mode 100644 Makefile diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/build diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..906b4bc --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +linux-amd64: build/linux-amd64 +windows-amd64: build/windows-amd64.exe + +build/linux-amd64: $(SRC) + GOOS=linux GOARCH=amd64 go build -o "$@" + +build/windows-amd64.exe: $(SRC) + GOOS=windows GOARCH=amd64 go build -o "$@" diff --git a/handle.go b/handle.go index 622b68e..918f3a8 100644 --- a/handle.go +++ b/handle.go @@ -6,7 +6,8 @@ import ( "time" ) -type protoHandler func(net.Conn) +type empty struct{} +type protoHandler func(net.Conn, time.Duration) func protocolHandler(proto string) (protoHandler, error) { switch proto { @@ -22,10 +23,10 @@ func logConn(conn net.Conn, msg string) { fmt.Printf("%s, %s, %s\n", now.String(), conn.RemoteAddr().String(), msg) } -func connHandler(handler protoHandler, conn net.Conn) { +func connHandler(handler protoHandler, conn net.Conn, delay time.Duration) { defer conn.Close() logConn(conn, "handling") - handler(conn) + handler(conn, delay) logConn(conn, "closing") } diff --git a/main.go b/main.go index 8de0a5c..ac4a25a 100644 --- a/main.go +++ b/main.go @@ -13,9 +13,11 @@ import ( func main() { var protocol string var bindAddr string + var delayParam string var port int - flag.StringVarP(&protocol, "proto", "P", "ssh", "protocol to trap") + flag.StringVarP(&protocol, "proto", "P", "ssh", "protocol to tarpit") + flag.StringVarP(&delayParam, "delay", "d", "10s", "delay between the tarpit keep-alive data packets") flag.StringVarP(&bindAddr, "bind-address", "b", "", "address to bind the socket to") flag.IntVarP(&port, "port", "p", 22, "TCP port") flag.Parse() @@ -25,14 +27,20 @@ func main() { fmt.Fprintln(os.Stderr, "Error: protocol handler;", err.Error()) os.Exit(1) } + delay, err := time.ParseDuration(delayParam) + if err != nil { + fmt.Fprintln(os.Stderr, "Error: parse delay;", err.Error()) + os.Exit(1) + } - rand.Seed(time.Now().UnixNano()) bind := fmt.Sprintf("%s:%d", bindAddr, port) ln, err := net.Listen("tcp", bind) if err != nil { fmt.Fprintln(os.Stderr, "Error: server listen;", err.Error()) os.Exit(1) } + + rand.Seed(time.Now().UnixNano()) fmt.Fprintf(os.Stderr, "** Server listening on %s\n", bind) for { conn, err := ln.Accept() @@ -40,6 +48,6 @@ func main() { // handle error continue } - go connHandler(handler, conn) + go connHandler(handler, conn, delay) } } diff --git a/ssh.go b/ssh.go index 3d43a6c..909dc13 100644 --- a/ssh.go +++ b/ssh.go @@ -9,16 +9,14 @@ import ( "time" ) -type empty struct{} - -func sshHandler(conn net.Conn) { +func sshHandler(conn net.Conn, delay time.Duration) { eof := make(chan empty) go func() { io.Copy(ioutil.Discard, conn) eof <- empty{} }() - tick := time.Tick(10 * time.Second) + tick := time.Tick(delay) for { select { case <-eof: -- cgit v1.2.3