Files
honeydany/app/config.go
T

154 lines
4.5 KiB
Go
Raw Normal View History

2025-09-28 06:48:03 +01:00
package app
import (
2025-09-28 14:47:22 +01:00
"encoding/json"
"fmt"
"os"
"path/filepath"
2025-09-28 06:48:03 +01:00
)
// Config contains runtime configuration for the honeypot
type Config struct {
2025-09-28 14:47:22 +01:00
LogMode string `json:"log_mode"` // "file" | "stdout" | "sqlite"
LogPath string `json:"log_path"`
2025-09-28 06:48:03 +01:00
2025-09-28 14:47:22 +01:00
Web struct {
Enabled bool `json:"enabled"`
Bind string `json:"bind"`
Port int `json:"port"`
} `json:"web"`
2025-09-28 06:48:03 +01:00
2025-09-28 14:47:22 +01:00
Services struct {
HTTP bool `json:"http"`
HTTPS bool `json:"https"`
SSH bool `json:"ssh"`
FTP bool `json:"ftp"`
SMTP bool `json:"smtp"`
IMAP bool `json:"imap"`
Telnet bool `json:"telnet"`
MySQL bool `json:"mysql"`
PostgreSQL bool `json:"postgresql"`
MongoDB bool `json:"mongodb"`
RDP bool `json:"rdp"`
SMB bool `json:"smb"`
SIP bool `json:"sip"`
VNC bool `json:"vnc"`
Generic []int `json:"generic"`
} `json:"services"`
2025-09-28 06:48:03 +01:00
2025-09-28 14:47:22 +01:00
Ports struct {
HTTP int `json:"http"`
HTTPS int `json:"https"`
SSH int `json:"ssh"`
FTP int `json:"ftp"`
SMTP int `json:"smtp"`
IMAP int `json:"imap"`
Telnet int `json:"telnet"`
MySQL int `json:"mysql"`
PostgreSQL int `json:"postgresql"`
MongoDB int `json:"mongodb"`
RDP int `json:"rdp"`
SMB int `json:"smb"`
SIP int `json:"sip"`
VNC int `json:"vnc"`
} `json:"ports"`
2025-09-28 08:06:05 +01:00
2025-09-28 14:47:22 +01:00
// Certificates allows overriding default certificate/key locations.
Certificates struct {
// SSHHostKeyPath points to a PEM-encoded RSA private key to use as SSH host key.
// If empty, a persistent key will be created in the same directory as LogPath.
SSHHostKeyPath string `json:"ssh_host_key_path"`
// TLSCertPath and TLSKeyPath are used by TLS-capable services if provided.
// If empty, a self-signed certificate will be generated and stored next to LogPath.
TLSCertPath string `json:"tls_cert_path"`
TLSKeyPath string `json:"tls_key_path"`
} `json:"certificates"`
2025-09-28 06:48:03 +01:00
}
2025-09-28 14:47:22 +01:00
// LastConfigPath holds the last path used to load/save config.json
var LastConfigPath string
2025-09-28 06:48:03 +01:00
// EnsureConfig writes a default config file if the given path doesn't exist
func EnsureConfig(path string) error {
2025-09-28 14:47:22 +01:00
if _, err := os.Stat(path); os.IsNotExist(err) {
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
return fmt.Errorf("create config dir: %w", err)
}
def := defaultConfig()
b, _ := json.MarshalIndent(def, "", " ")
if err := os.WriteFile(path, b, 0644); err != nil {
return fmt.Errorf("write default config: %w", err)
}
}
return nil
2025-09-28 06:48:03 +01:00
}
// LoadConfig loads JSON config from path
func LoadConfig(path string) (Config, error) {
2025-09-28 14:47:22 +01:00
var cfg Config
b, err := os.ReadFile(path)
if err != nil {
return cfg, err
}
if err := json.Unmarshal(b, &cfg); err != nil {
return cfg, err
}
LastConfigPath = path
return cfg, nil
}
// SaveConfig writes the given config to the provided path
func SaveConfig(path string, cfg Config) error {
b, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
return err
}
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
return err
}
return os.WriteFile(path, b, 0644)
2025-09-28 06:48:03 +01:00
}
func defaultConfig() Config {
2025-09-28 14:47:22 +01:00
var c Config
c.LogMode = "file"
c.LogPath = "honeypot.log"
c.Web.Enabled = true
c.Web.Bind = "127.0.0.1"
c.Web.Port = 6333
2025-09-28 06:48:03 +01:00
2025-09-28 14:47:22 +01:00
// Enable common services by default
c.Services.HTTP = true
c.Services.HTTPS = false
c.Services.SSH = true
c.Services.FTP = true
c.Services.SMTP = true
c.Services.Telnet = true
c.Services.MySQL = false
c.Services.PostgreSQL = false
c.Services.MongoDB = false
c.Services.IMAP = false
c.Services.RDP = false
c.Services.SMB = false
c.Services.SIP = false
c.Services.VNC = false
c.Services.Generic = []int{}
2025-09-28 06:48:03 +01:00
2025-09-28 14:47:22 +01:00
// Standard ports
c.Ports.HTTP = 8080
c.Ports.HTTPS = 8443
c.Ports.SSH = 2222
c.Ports.FTP = 2121
c.Ports.SMTP = 2525
c.Ports.IMAP = 1143
c.Ports.Telnet = 2323
c.Ports.MySQL = 3306
c.Ports.PostgreSQL = 5432
c.Ports.MongoDB = 27017
c.Ports.RDP = 3389
c.Ports.SIP = 5060
c.Ports.VNC = 5900
2025-09-28 06:48:03 +01:00
2025-09-28 14:47:22 +01:00
return c
2025-09-28 06:48:03 +01:00
}