Make connection throttle configurable

This commit is contained in:
Torjus Håkestad 2021-04-11 02:24:39 +02:00
parent 7ec29d0846
commit 44f611aa6b
4 changed files with 27 additions and 16 deletions

View File

@ -22,6 +22,10 @@ LogLevel = "INFO"
# Address and port to listen to # Address and port to listen to
# Default: ":2222" # Default: ":2222"
ListenAddr = ":2222" ListenAddr = ":2222"
# Throttle incoming and outgoing data per connection
# Values are in bytes per second. Empty means no unlimited
# Default: ""
ThrottleSpeed = 10240
[Frontend] [Frontend]
# Log level for SSH Honeypot # Log level for SSH Honeypot

View File

@ -24,9 +24,10 @@ type PostgresStoreConfig struct {
} }
type HoneypotConfig struct { type HoneypotConfig struct {
ListenAddr string `toml:"ListenAddr"` ListenAddr string `toml:"ListenAddr"`
LogLevel string `toml:"LogLevel"` LogLevel string `toml:"LogLevel"`
HostKeyPath string `toml:"HostKeyPath"` HostKeyPath string `toml:"HostKeyPath"`
ThrottleSpeed float64 `toml:"ThrottleSpeed"`
} }
type FrontendConfig struct { type FrontendConfig struct {

View File

@ -11,29 +11,32 @@ import (
type throttledConn struct { type throttledConn struct {
ID uuid.UUID ID uuid.UUID
conn net.Conn conn net.Conn
speed float64 writer *shapeio.Writer
reader *shapeio.Reader
CloseCallback func(c *throttledConn) CloseCallback func(c *throttledConn)
} }
func newThrottledConn(conn net.Conn) *throttledConn { func newThrottledConn(conn net.Conn) *throttledConn {
id := uuid.Must(uuid.NewRandom()) id := uuid.Must(uuid.NewRandom())
return &throttledConn{ID: id, conn: conn, speed: 1024 * 10} return &throttledConn{
ID: id,
conn: conn,
writer: shapeio.NewWriter(conn),
reader: shapeio.NewReader(conn),
}
} }
func (sc *throttledConn) SetSpeed(bytesPerSec float64) { func (sc *throttledConn) SetSpeed(bytesPerSec float64) {
sc.speed = bytesPerSec sc.writer.SetRateLimit(bytesPerSec)
sc.reader.SetRateLimit(bytesPerSec)
} }
func (sc *throttledConn) Read(b []byte) (n int, err error) { func (sc *throttledConn) Read(b []byte) (n int, err error) {
slowReader := shapeio.NewReader(sc.conn) return sc.reader.Read(b)
slowReader.SetRateLimit(sc.speed)
return slowReader.Read(b)
} }
func (sc *throttledConn) Write(b []byte) (n int, err error) { func (sc *throttledConn) Write(b []byte) (n int, err error) {
slowWriter := shapeio.NewWriter(sc.conn) return sc.writer.Write(b)
slowWriter.SetRateLimit(sc.speed)
return slowWriter.Write(b)
} }
func (sc *throttledConn) Close() error { func (sc *throttledConn) Close() error {

View File

@ -19,12 +19,14 @@ import (
) )
type HoneypotServer struct { type HoneypotServer struct {
attemptStore store.LoginAttemptStore Logger *zap.SugaredLogger
attemptsCallbacks []func(l models.LoginAttempt)
sshServer *ssh.Server sshServer *ssh.Server
Logger *zap.SugaredLogger attemptStore store.LoginAttemptStore
attemptsCallbacks []func(l models.LoginAttempt)
throttleSpeed float64
} }
func NewHoneypotServer(cfg config.HoneypotConfig, store store.LoginAttemptStore) (*HoneypotServer, error) { func NewHoneypotServer(cfg config.HoneypotConfig, store store.LoginAttemptStore) (*HoneypotServer, error) {
@ -107,8 +109,9 @@ func (hs *HoneypotServer) passwordHandler(ctx ssh.Context, password string) bool
func (s *HoneypotServer) connCallback(ctx ssh.Context, conn net.Conn) net.Conn { func (s *HoneypotServer) connCallback(ctx ssh.Context, conn net.Conn) net.Conn {
throttledConn := newThrottledConn(conn) throttledConn := newThrottledConn(conn)
throttledConn.SetSpeed(s.throttleSpeed)
ctx.SetValue("uuid", throttledConn.ID) ctx.SetValue("uuid", throttledConn.ID)
throttledConn.SetSpeed(2048) throttledConn.SetSpeed(s.throttleSpeed)
return throttledConn return throttledConn
} }