Skip to content

Commit

Permalink
Refactor (#28)
Browse files Browse the repository at this point in the history
* Bump github.com/labstack/echo/v4 from 4.8.0 to 4.9.0

Bumps [github.com/labstack/echo/v4](https://github.com/labstack/echo) from 4.8.0 to 4.9.0.
- [Release notes](https://github.com/labstack/echo/releases)
- [Changelog](https://github.com/labstack/echo/blob/master/CHANGELOG.md)
- [Commits](labstack/echo@v4.8.0...v4.9.0)

---
updated-dependencies:
- dependency-name: github.com/labstack/echo/v4
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <[email protected]>

* Refactor (#27)

* wip commit

* wip commit

* refactor wip

* more wip

* removing test file

* fixing docker and other configs

* reademe

---------

* adding missing files

---------
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
  • Loading branch information
Zeerg and dependabot[bot] committed Jul 23, 2023
1 parent 38a4d4c commit 70db900
Show file tree
Hide file tree
Showing 91 changed files with 857,138 additions and 70,278 deletions.
3 changes: 2 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
images*
Dockerfile
docker-compose.yaml
.git
.git
config.toml
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
.DS_Store
bin/
bin/
defaultConfig.json
kubeSystemConfig.json
config.toml
7 changes: 2 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@ FROM golang:alpine as build
RUN mkdir /app
WORKDIR /app
COPY . ./
RUN go mod download && go build -o /helix-honeypot
WORKDIR /app/cmd
RUN go get ./... && go build -o /helix-honeypot

#Final Stage
FROM alpine:latest
WORKDIR /
COPY --from=build /helix-honeypot /helix-honeypot
RUN addgroup -S helix && adduser -S helix -G helix
USER helix

EXPOSE 80

ENTRYPOINT ["/helix-honeypot"]
17 changes: 17 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import (
"fmt"
"os"

"helix-honeypot/server"
)

func main() {
// Start the server based on the run mode
err := server.StartHoneypot()
if err != nil {
fmt.Println("Failed To Start Server:", err)
os.Exit(1)
}
}
180 changes: 168 additions & 12 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,180 @@
package config

import (
"fmt"
"os"
"io/ioutil"
"strings"

"helix-honeypot/model"
"github.com/BurntSushi/toml"
)

type Config struct {
HTTP HTTPConfig
// GetEnvAsBool gets environment variables as boolean values. It returns false if the value is not "true" (case-insensitive), otherwise it returns true
func GetEnvAsBool(key string) bool {
value := os.Getenv(key)
return strings.ToLower(value) == "true"
}

// If env vars are empty set a default
func GetEnv(key, defaultValue string) string {
value := os.Getenv(key) // try to get the env var
if len(value) == 0 {
return defaultValue // if empty set default
}
return value // if not empty return env var
func LoadK8SConfig(cfg *model.K8SConfig) model.K8SConfig {
apiVersion := os.Getenv("K8SAPI_VERSION")
ipBase := os.Getenv("IP_BASE")
host := os.Getenv("K8S_HOST")
port := os.Getenv("K8S_PORT")

if apiVersion != "" {
cfg.APIVersion = apiVersion
}
if ipBase != "" {
cfg.IPBase = ipBase
}
if host != "" {
cfg.Host = host
}
if port != "" {
cfg.Port = port
}

// Since the zero value for a boolean in Go is `false`,
// we only need to check if the environment variable is "true".
if strings.ToLower(os.Getenv("GENERATE_KUBE_SYSTEM")) == "true" {
cfg.GenerateKubeSys = true
}
if strings.ToLower(os.Getenv("GENERATE_RANDOMNESS")) == "true" {
cfg.GenerateRand = true
}

return *cfg
}

func LoadHTTPConfig(cfg *model.HTTPConfig) model.HTTPConfig {
host := os.Getenv("HELIX_HTTP_HOST")
port := os.Getenv("HELIX_HTTP_PORT")

if host != "" {
cfg.Host = host
}
if port != "" {
cfg.Port = port
}

return *cfg
}

func LoadUDPConfig(cfg *model.UDPConfig) model.UDPConfig {
host := os.Getenv("HELIX_UDP_HOST")
port := os.Getenv("HELIX_UDP_PORT")

if host != "" {
cfg.Host = host
}
if port != "" {
cfg.Port = port
}

return *cfg
}

func LoadTCPConfig(cfg *model.TCPConfig) model.TCPConfig {
host := os.Getenv("HELIX_TCP_HOST")
port := os.Getenv("HELIX_TCP_PORT")

if host != "" {
cfg.Host = host
}
if port != "" {
cfg.Port = port
}

return *cfg
}

func LoadDEFConfig(cfg *model.DEFConfig) model.DEFConfig {
host := os.Getenv("HELIX_DEF_HOST")
port := os.Getenv("HELIX_DEF_PORT")

if host != "" {
cfg.Host = host
}
if port != "" {
cfg.Port = port
}

return *cfg
}

func NewConfig() *Config {
return &Config{
HTTP: LoadHTTPConfig(),
func LoadMongoDBConfig(cfg *model.MongoDBConfig) model.MongoDBConfig {
username := os.Getenv("MONGODB_USERNAME")
password := os.Getenv("MONGODB_PASSWORD")
host := os.Getenv("MONGODB_HOST")
database := os.Getenv("MONGODB_DATABASE")
collection := os.Getenv("MONGODB_COLLECTION")

if username != "" {
cfg.Username = username
}
if password != "" {
cfg.Password = password
}
if host != "" {
cfg.Host = host
}
if database != "" {
cfg.Database = database
}
if collection != "" {
cfg.Collection = collection
}

// Format the MongoDB URI
uri := fmt.Sprintf("mongodb+srv://%s:%s@%s/", cfg.Username, cfg.Password, cfg.Host)
cfg.URI = uri

if strings.ToLower(os.Getenv("LOG_TO_MONGODB")) == "true" {
cfg.LogToMongoDB = true
}

return *cfg
}

func LoadRunModeConfig(cfg *model.RunModeConfig) model.RunModeConfig {
runMode := os.Getenv("RUN_MODE")
location := os.Getenv("HELIX_LOCATION")

if runMode != "" {
cfg.RunMode = runMode
}
if location != "" {
cfg.Location = location
}

return *cfg
}

func NewConfig(configFile string) (*model.Config, error) {
var cfg model.Config

// Check if the config file exists
if _, err := os.Stat(configFile); err == nil {
// If the config file exists, load default values from TOML file
data, err := ioutil.ReadFile(configFile)
if err != nil {
return nil, err
}
_, err = toml.Decode(string(data), &cfg)
if err != nil {
return nil, err
}
}

// Override with environment variables
cfg.HTTP = LoadHTTPConfig(&cfg.HTTP)
cfg.UDP = LoadUDPConfig(&cfg.UDP)
cfg.TCP = LoadTCPConfig(&cfg.TCP)
cfg.K8S = LoadK8SConfig(&cfg.K8S)
cfg.DEF = LoadDEFConfig(&cfg.DEF)
cfg.MongoDB = LoadMongoDBConfig(&cfg.MongoDB)
cfg.RunMode = LoadRunModeConfig(&cfg.RunMode)

return &cfg, nil
}
13 changes: 0 additions & 13 deletions config/http.go

This file was deleted.

73 changes: 73 additions & 0 deletions config/id.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package config

import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"net"
"os"
"strings"

"github.com/denisbrodbeck/machineid"
. "github.com/klauspost/cpuid/v2"

"helix-honeypot/model"
)

func getMacAddress() ([]string, error) {
ifas, err := net.Interfaces()
if err != nil {
return nil, fmt.Errorf("error getting network interfaces: %v", err)
}
var as []string
for _, ifa := range ifas {
a := ifa.HardwareAddr.String()
if a != "" {
as = append(as, a)
}
}
return as, nil
}

func MakeMachineId() (string, error) {
// Generate a machine id from multiple values
var hostid model.HostIDStruct
id, err := machineid.ID()
if err != nil {
fmt.Errorf("error generating machine id: %v", err)
}
hostname, err := os.Hostname()
if err != nil {
fmt.Errorf("error getting hostname: %v", err)
}
macSlice, err := getMacAddress()
if err != nil {
fmt.Errorf("error getting mac address: %v", err)
}

hostid.MachineID = id
hostid.ProcessorHash = CPU.BrandName
hostid.ProcessorFeatures = strings.Join(CPU.FeatureSet(), ",")
hostid.CacheLine = fmt.Sprint(CPU.CacheLine)
hostid.CacheL1D = fmt.Sprint(CPU.Cache.L1D)
hostid.CacheL1I = fmt.Sprint(CPU.Cache.L1I)
hostid.CacheL2 = fmt.Sprint(CPU.Cache.L2)
hostid.CacheL3 = fmt.Sprint(CPU.Cache.L3)
hostid.CPUFrequency = fmt.Sprint(CPU.Hz)
hostid.PhysicalCores = fmt.Sprint(CPU.PhysicalCores)
hostid.LogicalCores = fmt.Sprint(CPU.LogicalCores)
hostid.ThreadsPerCore = fmt.Sprint(CPU.ThreadsPerCore)
hostid.VendorID = CPU.VendorID.String() // convert VendorID to string
hostid.MacAddress = macSlice
hostid.Hostname = hostname

hostIDBytes, err := json.Marshal(hostid)
if err != nil {
return "", fmt.Errorf("error marshalling hostid: %v", err)
}
hash := sha256.Sum256(hostIDBytes)

return hex.EncodeToString(hash[:]), nil
}

57 changes: 48 additions & 9 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,53 @@
version: '3.7'

services:
helix-honeypot-ad:
build: ./
ports:
- "8000:80"
entrypoint: [/helix-honeypot, -mode=ad]
volumes:
- /dev/urandom:/dev/urandom
helix-honeypot:
helix-honeypot-k8s:
build: ./
ports:
- "80:80"
- "8111:8111"
environment:
- RUN_MODE=k8s
- HELIX_LOCATION=testing
- K8SAPI_VERSION=v1.21
- IP_BASE=192.168
- GENERATE_KUBE_SYSTEM=true
- GENERATE_RANDOMNESS=true
- K8S_HOST=0.0.0.0
- K8S_PORT=8111

helix-honeypot-http:
build: ./
ports:
- "8000:8000"
environment:
- RUN_MODE=http
- HELIX_HTTP_HOST=0.0.0.0
- HELIX_HTTP_PORT=8000

helix-honeypot-tcp:
build: ./
ports:
- "3000:3000"
environment:
- RUN_MODE=tcp
- HELIX_TCP_HOST=0.0.0.0
- HELIX_TCP_PORT=3000

helix-honeypot-udp:
build: ./
ports:
- "53:53/udp"
environment:
- RUN_MODE=udp
- HELIX_UDP_HOST=0.0.0.0
- HELIX_UDP_PORT=53

helix-honeypot-def:
build: ./
ports:
- "8001:8001"
environment:
- RUN_MODE=def
- HELIX_DEF_HOST=0.0.0.0
- HELIX_DEF_PORT=8001

Loading

0 comments on commit 70db900

Please sign in to comment.