Release-Candidate with Dynamic TCP and UDP-Ports
This commit is contained in:
92
main.go
92
main.go
@@ -3,12 +3,12 @@ package main
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
@@ -66,6 +66,18 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
/* TCP- and UDP-Honeypods */
|
||||
|
||||
portsTCP := []string{"135", "139", "445", "389", "636", "3268", "3269", "88", "3389", "3306", "27017", "5432", "25", "123", "5900"}
|
||||
for _, port := range portsTCP {
|
||||
go startTCPListener(port)
|
||||
}
|
||||
|
||||
portsUDP := []string{"135", "137", "138", "389", "3389", "88"}
|
||||
for _, port := range portsUDP {
|
||||
go startTCPListener(port)
|
||||
}
|
||||
|
||||
// Endpunkte registrieren
|
||||
http.HandleFunc("/", handleRoot)
|
||||
http.HandleFunc("/add", handleAdd)
|
||||
@@ -103,6 +115,63 @@ func handleAdd(w http.ResponseWriter, r *http.Request) {
|
||||
processIP(ip, w)
|
||||
}
|
||||
|
||||
func startTCPListener(port string) {
|
||||
ln, err := net.Listen("tcp", ":"+port)
|
||||
if err != nil {
|
||||
log.Fatalf("❌ Could not listen on TCP port %s: %v", port, err)
|
||||
}
|
||||
log.Printf("🚀 TCP Honeypod listening on port %s", port)
|
||||
for {
|
||||
conn, err := ln.Accept()
|
||||
if err != nil {
|
||||
log.Printf("⚠️ Error accepting TCP connection: %v", err)
|
||||
continue
|
||||
}
|
||||
go handleTCPConn(conn)
|
||||
}
|
||||
}
|
||||
|
||||
func handleTCPConn(conn net.Conn) {
|
||||
defer conn.Close()
|
||||
ip, _, err := net.SplitHostPort(conn.RemoteAddr().String())
|
||||
if err != nil {
|
||||
log.Printf("⚠️ Could not parse remote address: %v", err)
|
||||
return
|
||||
}
|
||||
log.Printf("👀 TCP connection from %s", ip)
|
||||
processIP(ip, nil) // Anpassung nötig: processIP muss nil w als Option haben
|
||||
}
|
||||
|
||||
func startUDPListener(port string) {
|
||||
addr := net.UDPAddr{
|
||||
Port: portInt(port),
|
||||
IP: net.ParseIP("0.0.0.0"),
|
||||
}
|
||||
conn, err := net.ListenUDP("udp", &addr)
|
||||
if err != nil {
|
||||
log.Fatalf("❌ Could not listen on UDP port %s: %v", port, err)
|
||||
}
|
||||
log.Printf("🚀 UDP Honeypod listening on port %s", port)
|
||||
buf := make([]byte, 1024)
|
||||
for {
|
||||
n, remoteAddr, err := conn.ReadFromUDP(buf)
|
||||
if err != nil {
|
||||
log.Printf("⚠️ Error reading UDP packet: %v", err)
|
||||
continue
|
||||
}
|
||||
if n > 0 {
|
||||
ip := remoteAddr.IP.String()
|
||||
log.Printf("👀 UDP packet from %s", ip)
|
||||
processIP(ip, nil) // ebenfalls anpassen
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func portInt(port string) int {
|
||||
p, _ := strconv.Atoi(port)
|
||||
return p
|
||||
}
|
||||
|
||||
func processIP(ip string, w http.ResponseWriter) {
|
||||
ipKey := ip + "/32"
|
||||
|
||||
@@ -110,24 +179,31 @@ func processIP(ip string, w http.ResponseWriter) {
|
||||
err := redisClient.HSet(ctx, hashName, ipKey, 1).Err()
|
||||
if err != nil {
|
||||
log.Printf("❌ Error writing to redis: %v", err)
|
||||
http.Error(w, "Interner Fehler", http.StatusInternalServerError)
|
||||
if w != nil {
|
||||
http.Error(w, "Interner Fehler", http.StatusInternalServerError)
|
||||
}
|
||||
return
|
||||
}
|
||||
log.Printf("✅ IP %s in saved to redis", ipKey)
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
w.Write([]byte("Temporary unavailable"))
|
||||
if w != nil {
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
w.Write([]byte("Temporary unavailable"))
|
||||
}
|
||||
} else {
|
||||
// Slave: an Master weiterleiten
|
||||
resp, err := http.Post(masterURL+"/add", "text/plain", bytes.NewBuffer([]byte(ip)))
|
||||
if err != nil {
|
||||
log.Printf("❌ Error relaying to Master: %v", err)
|
||||
http.Error(w, "Error relaying", http.StatusBadGateway)
|
||||
if w != nil {
|
||||
http.Error(w, "Error relaying", http.StatusBadGateway)
|
||||
}
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
w.WriteHeader(resp.StatusCode)
|
||||
fmt.Println(resp.Body)
|
||||
io.Copy(w, resp.Body)
|
||||
if w != nil {
|
||||
w.WriteHeader(resp.StatusCode)
|
||||
io.Copy(w, resp.Body)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user