Compare commits

..

3 Commits

Author SHA1 Message Date
Brieuc Dubois a8ce2b9b51 License and readme 2023-12-26 17:30:50 +01:00
Brieuc Dubois 88cf44cd9e Log to custom file 2023-12-26 17:30:41 +01:00
Brieuc Dubois 2a46230b38 Log to custom file and listen custom port 2023-12-26 17:11:51 +01:00
7 changed files with 64 additions and 15 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"]

7
LICENSE.md Normal file
View File

@ -0,0 +1,7 @@
Copyright © 2023 Brieuc Dubois
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

13
README.md Normal file
View File

@ -0,0 +1,13 @@
# SSH-Honeypot
## Overview
SSH-Honeypot is a simple, effective server designed to log SSH connection attempts. It's built using Go and can be easily deployed using Docker.
The server start a ssh server on port 22 and log any connection attempt to a logfile.
## KISS Principles
This project adheres to the [KISS principles](https://en.wikipedia.org/wiki/KISS_principle), utilizing minimalistic and efficient codebase that focus on its core functionality. This approach avoids unnecessary complexities, ensuring the project is easy to maintain and deploy across various architectures.
## License
This project is licensed under the [MIT license](LICENSE.md).

View File

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

2
go.mod
View File

@ -4,4 +4,4 @@ go 1.21.5
require golang.org/x/crypto v0.17.0 require golang.org/x/crypto v0.17.0
require golang.org/x/sys v0.15.0 require golang.org/x/sys v0.15.0 // indirect

44
main.go
View File

@ -1,26 +1,41 @@
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"
"strconv" "os"
"time" "time"
"golang.org/x/crypto/ssh" "golang.org/x/crypto/ssh"
) )
func main() { func main() {
path := os.Getenv("PATH")
if path == "" {
path = "/var/log/ssh-honeypot.log"
}
port := 22 logFile, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
panic(err)
}
defer logFile.Close()
w := bufio.NewWriter(logFile)
port := os.Getenv("PORT")
if port == "" {
port = "22"
}
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{
@ -29,24 +44,30 @@ 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())
}, },
} }
config.AddHostKey(key) config.AddHostKey(key)
// Listen on port 22 listener, err := net.Listen("tcp", "0.0.0.0:"+port)
listener, err := net.Listen("tcp", "0.0.0.0:"+strconv.Itoa(port))
if err != nil { if err != nil {
log.Fatalf("Failed to listen on port %d: %v", port, err) panic(err)
} }
log.Printf("Listening on port %d...", 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
} }
@ -55,19 +76,16 @@ func main() {
} }
func generateKeyPair() (ssh.Signer, error) { func generateKeyPair() (ssh.Signer, error) {
// Generate a new RSA private key
privateKey, err := rsa.GenerateKey(rand.Reader, 2048) privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil { if err != nil {
return nil, err return nil, err
} }
// Encode the private key to PEM format
privateKeyPEM := pem.EncodeToMemory(&pem.Block{ privateKeyPEM := pem.EncodeToMemory(&pem.Block{
Type: "RSA PRIVATE KEY", Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(privateKey), Bytes: x509.MarshalPKCS1PrivateKey(privateKey),
}) })
// Parse the PEM encoded private key to get an ssh.Signer
signer, err := ssh.ParsePrivateKey(privateKeyPEM) signer, err := ssh.ParsePrivateKey(privateKeyPEM)
if err != nil { if err != nil {
return nil, err return nil, err