2017-09-27 13:44:02 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
2017-09-28 09:35:05 +00:00
|
|
|
"time"
|
2017-09-27 21:56:07 +00:00
|
|
|
"github.com/mkideal/cli"
|
2017-09-27 13:44:02 +00:00
|
|
|
)
|
|
|
|
|
2017-09-27 21:56:07 +00:00
|
|
|
type knockArguments struct {
|
|
|
|
cli.Helper
|
|
|
|
WhitelistPort int `cli:'wp' usage:'The port to launch the whitelist server on'`
|
|
|
|
GatewayPort int `cli:'gp' usage:'The port to protect'`
|
2017-09-28 12:41:07 +00:00
|
|
|
BlacklistPort int `cli:'wp' usage:'If set: The port to blacklist the connected host'`
|
2017-09-27 21:56:07 +00:00
|
|
|
Destination string `cli:'d' usage:'The destination to relay traffic to'`
|
2017-09-28 09:35:05 +00:00
|
|
|
Timeout int64 `cli:'t' usage:'Time in seconds after which a whitelist entry will be removed'`
|
2017-09-28 13:09:29 +00:00
|
|
|
Verbose bool `cli:'v' usage:'Verbosity'`
|
2017-09-27 21:56:07 +00:00
|
|
|
}
|
|
|
|
|
2017-09-28 12:41:07 +00:00
|
|
|
var (
|
|
|
|
whitelist = make(map[string]int64)
|
|
|
|
blacklist []string
|
|
|
|
arguments *knockArguments
|
2017-09-28 12:58:24 +00:00
|
|
|
traffic_in int64
|
|
|
|
traffic_out int64
|
2017-09-28 12:41:07 +00:00
|
|
|
)
|
2017-09-27 13:44:02 +00:00
|
|
|
|
|
|
|
func main() {
|
2017-09-28 10:11:54 +00:00
|
|
|
// Parse command line arguments
|
2017-09-27 21:56:07 +00:00
|
|
|
cli.Run(new(knockArguments), func(ctx *cli.Context) error {
|
|
|
|
arguments = ctx.Argv() . (*knockArguments)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2017-09-28 10:11:54 +00:00
|
|
|
// Launch listeners
|
2017-09-27 21:56:07 +00:00
|
|
|
go listener(arguments.WhitelistPort, whitelist_handler)
|
2017-09-28 12:41:07 +00:00
|
|
|
go listener(arguments.BlacklistPort, blacklist_handler)
|
2017-09-28 12:58:24 +00:00
|
|
|
go listener(arguments.GatewayPort, gateway_handler)
|
|
|
|
stats()
|
|
|
|
}
|
|
|
|
|
|
|
|
func stats() {
|
|
|
|
for {
|
|
|
|
time.Sleep(60*time.Second)
|
|
|
|
fmt.Println("[STS] In ", traffic_in/1024, "KB, Out ", traffic_out/1024, "KB");
|
|
|
|
}
|
2017-09-27 14:24:38 +00:00
|
|
|
}
|
|
|
|
|
2017-09-28 21:41:47 +00:00
|
|
|
func get_address_from_conn(c net.Conn) string {
|
|
|
|
host, _, _ := net.SplitHostPort(c.RemoteAddr().String())
|
|
|
|
return host
|
|
|
|
}
|
|
|
|
|
2017-09-27 14:24:38 +00:00
|
|
|
func listener(port int, listen_func func(c net.Conn)) {
|
2017-09-28 10:11:54 +00:00
|
|
|
// Set up listening sockets on specified port and hand over to specified listen_func
|
2017-09-27 14:24:38 +00:00
|
|
|
ln, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
|
2017-09-27 13:44:02 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2017-09-27 21:08:23 +00:00
|
|
|
fmt.Println("[ERR] Creating listener for Port ", port)
|
2017-09-28 08:56:36 +00:00
|
|
|
fmt.Println(" Error is ", err)
|
2017-09-27 13:44:02 +00:00
|
|
|
} else {
|
2017-09-27 21:08:23 +00:00
|
|
|
fmt.Println("[OK ] Creating listener for Port ", port)
|
2017-09-27 13:44:02 +00:00
|
|
|
for {
|
|
|
|
conn, err := ln.Accept()
|
|
|
|
if err != nil {
|
2017-09-27 21:08:23 +00:00
|
|
|
fmt.Println("[ERR] Accepting on Port ", port)
|
2017-09-27 13:44:02 +00:00
|
|
|
} else {
|
2017-09-27 14:24:38 +00:00
|
|
|
go listen_func(conn)
|
2017-09-27 13:44:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-27 14:24:38 +00:00
|
|
|
func whitelist_handler(c net.Conn) {
|
2017-09-28 10:11:54 +00:00
|
|
|
// Handler function for whitelist socket connections, whitelisting the connecting host
|
2017-09-28 21:41:47 +00:00
|
|
|
host := get_address_from_conn(c)
|
2017-09-27 13:44:02 +00:00
|
|
|
|
2017-09-28 12:41:07 +00:00
|
|
|
if is_blacklisted(host) {
|
2017-09-28 13:09:29 +00:00
|
|
|
if arguments.Verbose {
|
|
|
|
fmt.Println("[BLK] Denying blacklisted host ", host)
|
|
|
|
}
|
2017-09-28 12:41:07 +00:00
|
|
|
} else {
|
|
|
|
io.WriteString(c, fmt.Sprintf("Knock Knock, %s.", host))
|
|
|
|
add_to_whitelist(host)
|
|
|
|
}
|
|
|
|
c.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func blacklist_handler(c net.Conn) {
|
|
|
|
// Handler which blocks every host connecting to it.
|
|
|
|
// Useful to place it on port (whitelistPort-1) to crash port scanners.
|
2017-09-28 21:41:47 +00:00
|
|
|
host := get_address_from_conn(c)
|
2017-09-28 12:41:07 +00:00
|
|
|
|
|
|
|
if ! is_whitelisted(host) {
|
2017-09-28 13:09:29 +00:00
|
|
|
if arguments.Verbose {
|
|
|
|
fmt.Println("[BLK] Blacklisting ", host)
|
|
|
|
}
|
2017-09-28 12:41:07 +00:00
|
|
|
add_to_blacklist(host)
|
|
|
|
} else {
|
2017-09-28 13:09:29 +00:00
|
|
|
if arguments.Verbose {
|
|
|
|
fmt.Println("[ERR] Whitelisted host ", host, " connected to blacklist port. Ignoring.")
|
|
|
|
}
|
2017-09-28 12:41:07 +00:00
|
|
|
}
|
2017-09-27 14:24:38 +00:00
|
|
|
c.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func gateway_handler(c net.Conn) {
|
2017-09-28 10:11:54 +00:00
|
|
|
// Filter connections whether or not the connecting host is whitelisted
|
2017-09-28 21:41:47 +00:00
|
|
|
host := get_address_from_conn(c)
|
2017-09-27 14:24:38 +00:00
|
|
|
|
2017-09-28 12:41:07 +00:00
|
|
|
if is_blacklisted(host) {
|
2017-09-28 13:09:29 +00:00
|
|
|
if arguments.Verbose {
|
|
|
|
fmt.Println("[BLK] Blacklisted host ", host, ", ignoring")
|
|
|
|
}
|
2017-09-28 12:41:07 +00:00
|
|
|
} else if is_whitelisted(host) {
|
2017-09-28 13:09:29 +00:00
|
|
|
if arguments.Verbose {
|
|
|
|
fmt.Println("[OK ] Whitelisted host ", host, " connected")
|
|
|
|
}
|
2017-09-28 09:54:10 +00:00
|
|
|
update_whitelist_time(host)
|
2017-09-27 14:40:29 +00:00
|
|
|
proxy(c)
|
2017-09-28 10:13:03 +00:00
|
|
|
update_whitelist_time(host)
|
|
|
|
// yes, we're updating this before and after.
|
|
|
|
// why? Consider long TCP connections, e.g. in games
|
|
|
|
// Then the specified Timeout may be reached before the connection is even closed
|
|
|
|
// This won't affect this connection (it'll stay open even if the timeout is reached)
|
|
|
|
// but another connection won't be possible, even if it's right after the closing of
|
|
|
|
// the first connection. ¯\_(ツ)_/¯
|
2017-09-28 13:09:29 +00:00
|
|
|
} else if arguments.Verbose {
|
2017-09-27 21:08:23 +00:00
|
|
|
fmt.Println("[BLK] Blocking host ", host)
|
2017-09-27 13:44:02 +00:00
|
|
|
}
|
|
|
|
c.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func add_to_whitelist(addr string) {
|
2017-09-28 10:11:54 +00:00
|
|
|
// Add the specified address to the whitelist
|
2017-09-27 13:44:02 +00:00
|
|
|
if ! is_whitelisted(addr) {
|
2017-09-28 21:30:21 +00:00
|
|
|
fmt.Println("[OK ] Add ", addr, " to whitelist")
|
2017-09-28 09:35:05 +00:00
|
|
|
update_whitelist_time(addr)
|
2017-09-27 13:44:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-28 09:35:05 +00:00
|
|
|
func remove_from_whitelist(addr string) {
|
2017-09-28 10:11:54 +00:00
|
|
|
// Remove specified address from whitelist
|
2017-09-28 09:35:05 +00:00
|
|
|
delete(whitelist, addr)
|
|
|
|
}
|
|
|
|
|
2017-09-27 13:44:02 +00:00
|
|
|
func is_whitelisted(addr string) bool {
|
2017-09-28 10:11:54 +00:00
|
|
|
// Check whether or not the specified address is whitelisted and inside the timing window
|
2017-09-28 09:35:05 +00:00
|
|
|
if _, present := whitelist[addr]; present {
|
|
|
|
// Key is present in whitelist map
|
|
|
|
if (whitelist[addr] + arguments.Timeout) >= time.Now().Unix() {
|
|
|
|
// AND we are still in the timing window
|
|
|
|
update_whitelist_time(addr)
|
2017-09-27 13:44:02 +00:00
|
|
|
return true
|
2017-09-28 09:35:05 +00:00
|
|
|
} else {
|
|
|
|
// But we're outside of the timing window
|
|
|
|
remove_from_whitelist(addr)
|
|
|
|
return false
|
2017-09-27 13:44:02 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-28 09:35:05 +00:00
|
|
|
// Entry is not present.
|
2017-09-27 13:44:02 +00:00
|
|
|
return false
|
|
|
|
}
|
2017-09-27 14:40:29 +00:00
|
|
|
|
2017-09-28 12:41:07 +00:00
|
|
|
func add_to_blacklist(addr string) {
|
|
|
|
// Add specified address to blacklist
|
2017-09-28 21:30:21 +00:00
|
|
|
if ! is_blacklisted(addr) {
|
|
|
|
fmt.Println("[OK ] Add ", addr, " to blacklist")
|
|
|
|
blacklist = append(blacklist, addr)
|
|
|
|
}
|
2017-09-28 12:41:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func is_blacklisted(addr string) bool {
|
|
|
|
// Check whether or not the specified address is blacklisted
|
|
|
|
for i:=0; i<len(blacklist); i++ {
|
|
|
|
if blacklist[i] == addr {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-09-28 09:35:05 +00:00
|
|
|
func update_whitelist_time(addr string) {
|
2017-09-28 10:11:54 +00:00
|
|
|
// Update whitelist - prevent timeout of connection
|
2017-09-28 09:35:05 +00:00
|
|
|
whitelist[addr] = time.Now().Unix()
|
|
|
|
}
|
|
|
|
|
2017-09-27 14:40:29 +00:00
|
|
|
func proxy(c net.Conn) {
|
2017-09-28 10:11:54 +00:00
|
|
|
// Proxy connection between the destination server and our connecting client
|
2017-09-27 21:56:07 +00:00
|
|
|
ln, err := net.Dial("tcp", arguments.Destination)
|
2017-09-27 14:40:29 +00:00
|
|
|
if err != nil {
|
2017-09-27 21:08:23 +00:00
|
|
|
fmt.Println("[ERR] Proxy connection to server failed")
|
2017-09-28 08:56:36 +00:00
|
|
|
fmt.Println(" Error is ", err)
|
2017-09-27 14:40:29 +00:00
|
|
|
} else {
|
2017-09-28 21:41:47 +00:00
|
|
|
host := get_address_from_conn(c)
|
|
|
|
go proxy_writefunc(c, ln, &traffic_in, host)
|
|
|
|
proxy_writefunc(ln, c, &traffic_out, host)
|
2017-09-28 21:31:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-28 21:41:47 +00:00
|
|
|
func proxy_writefunc(a net.Conn, b net.Conn, written_bytes *int64, addr string) {
|
2017-09-28 21:31:10 +00:00
|
|
|
var delta int64 = 0
|
|
|
|
var err error
|
|
|
|
|
|
|
|
for err == nil {
|
|
|
|
delta, err = io.CopyN(a, b, 1024)
|
|
|
|
*written_bytes += delta
|
2017-09-28 21:41:47 +00:00
|
|
|
update_whitelist_time(addr)
|
2017-09-27 14:40:29 +00:00
|
|
|
}
|
|
|
|
}
|