Created
September 23, 2020 16:23
-
-
Save tsumarios/d7ef530577bc6f8160cdee074144c83b to your computer and use it in GitHub Desktop.
A simple TCP honeypot written in Go. Usage: "go run goneypot.go <port>" or build a binary and run it specifying a port.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "bytes" | |
| "log" | |
| "net" | |
| "os" | |
| ) | |
| // Connection handler | |
| func handleConn(conn net.Conn) { | |
| log.Printf("Received connection from %s\n", conn.RemoteAddr()) | |
| // Read buffer from connection | |
| buff := make([]byte, 1024) | |
| nbytes, err := conn.Read(buff) | |
| if err != nil { | |
| log.Println("Error reading from connection. ", err) | |
| } | |
| // Reply with a fake unauthorized message | |
| headers := "HTTP/1.0 401 Unauthorized\nServer: Apache/2.4.44 (Ubuntu) Server\n\n" | |
| fakeMsg := headers + "401 Unauthorized access.\n" | |
| conn.Write([]byte(fakeMsg)) | |
| // Log request | |
| trimmedOutput := bytes.TrimRight(buff, "\x00") | |
| log.Printf("Read %d bytes from %s\n%s\n", | |
| nbytes, conn.RemoteAddr(), trimmedOutput) | |
| conn.Close() | |
| } | |
| func main() { | |
| // Check args | |
| if len(os.Args) != 2 { | |
| log.Fatalf("Usage: ./%s <port>", os.Args[0]) | |
| return | |
| } | |
| // Vars | |
| var ( | |
| ip string = "localhost" | |
| port string = os.Args[1] | |
| endpoint string = ip + ":" + port | |
| ) | |
| // Set log file | |
| logFile, err := os.OpenFile("logs.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| defer logFile.Close() | |
| log.SetOutput(logFile) | |
| // TCP Listener | |
| ln, err := net.Listen("tcp", endpoint) | |
| if err != nil { | |
| log.Fatalf("Error listening on port %s\n%s\n", port, err.Error()) | |
| } | |
| log.Printf("Listening on port %s.\n", port) | |
| for { | |
| conn, err := ln.Accept() | |
| if err != nil { | |
| log.Println("Error accepting connection.", err) | |
| } | |
| go handleConn(conn) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment