Log to custom file

This commit is contained in:
Brieuc Dubois 2023-12-26 17:30:41 +01:00
parent 2a46230b38
commit 88cf44cd9e
4 changed files with 29 additions and 10 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
data/

View File

@ -12,4 +12,12 @@ WORKDIR /app
COPY --from=builder /app/ssh-honeypot . COPY --from=builder /app/ssh-honeypot .
RUN mkdir data
ENV PATH data/ssh-honeypot.log
ENV PORT 22
EXPOSE 22
VOLUME /app/data
ENTRYPOINT ["./ssh-honeypot"] ENTRYPOINT ["./ssh-honeypot"]

View File

@ -2,4 +2,6 @@ services:
ssh-honeypot: ssh-honeypot:
build: . build: .
ports: ports:
- "2222:22" - "2222:22"
volumes:
- ./data:/app/data

26
main.go
View File

@ -1,12 +1,12 @@
package main package main
import ( import (
"bufio"
"crypto/rand" "crypto/rand"
"crypto/rsa" "crypto/rsa"
"crypto/x509" "crypto/x509"
"encoding/pem" "encoding/pem"
"fmt" "fmt"
"log"
"net" "net"
"os" "os"
"time" "time"
@ -17,15 +17,16 @@ import (
func main() { func main() {
path := os.Getenv("PATH") path := os.Getenv("PATH")
if path == "" { if path == "" {
path = "./ssh-honeypot.log" path = "/var/log/ssh-honeypot.log"
} }
logFile, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) logFile, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil { if err != nil {
log.Fatalf("Unable to open log file: %v", err) panic(err)
} }
defer logFile.Close() defer logFile.Close()
log.SetOutput(logFile)
w := bufio.NewWriter(logFile)
port := os.Getenv("PORT") port := os.Getenv("PORT")
if port == "" { if port == "" {
@ -34,7 +35,7 @@ func main() {
key, err := generateKeyPair() key, err := generateKeyPair()
if err != nil { if err != nil {
log.Fatalf("Failed to generate key pair: %v", err) panic(err)
} }
config := &ssh.ServerConfig{ config := &ssh.ServerConfig{
@ -43,7 +44,14 @@ func main() {
if err != nil { if err != nil {
ip = c.RemoteAddr().String() ip = c.RemoteAddr().String()
} }
log.Printf("[%s] \"honeypot connection attempt: ssh - %s - %s - %s\"\n", time.Now().Format("2006-01-02 15:04:05.000"), ip, c.User(), string(pass)) _, err = fmt.Fprintf(w, "[%s] \"honeypot connection attempt: ssh - %s - %s - %s\"\n", time.Now().Format("2006-01-02 15:04:05.000"), ip, c.User(), string(pass))
if err != nil {
fmt.Printf("Error writing to log file: %v", err)
}
w.Flush()
return nil, fmt.Errorf("password rejected for %q", c.User()) return nil, fmt.Errorf("password rejected for %q", c.User())
}, },
} }
@ -52,14 +60,14 @@ func main() {
listener, err := net.Listen("tcp", "0.0.0.0:"+port) listener, err := net.Listen("tcp", "0.0.0.0:"+port)
if err != nil { if err != nil {
log.Fatalf("Failed to listen on port %s: %v", port, err) panic(err)
} }
log.Printf("Listening on port %s...", port) fmt.Printf("Listening on port %s...", port)
for { for {
conn, err := listener.Accept() conn, err := listener.Accept()
if err != nil { if err != nil {
log.Printf("Failed to accept incoming connection: %s", err) fmt.Printf("Failed to accept incoming connection: %s", err)
continue continue
} }