forked from Tylous/Limelighter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Limelighter.go
257 lines (228 loc) · 7.1 KB
/
Limelighter.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package main
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"flag"
"fmt"
"io"
"log"
crand "math/rand"
"os"
"os/exec"
"strings"
"time"
"github.com/fatih/color"
)
type FlagOptions struct {
outFile string
inputFile string
domain string
password string
real string
verify string
}
var (
debugging bool
debugWriter io.Writer
)
func printDebug(format string, v ...interface{}) {
if debugging {
output := fmt.Sprintf("[DEBUG] ")
output += format
fmt.Fprintf(debugWriter, output, v...)
}
}
const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
func VarNumberLength(min, max int) string {
var r string
crand.Seed(time.Now().UnixNano())
num := crand.Intn(max-min) + min
n := num
r = RandStringBytes(n)
return r
}
func RandStringBytes(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = letters[crand.Intn(len(letters))]
}
return string(b)
}
func GenerateCert(domain string, inputFile string) {
var err error
rootKey, err := rsa.GenerateKey(rand.Reader, 4096)
if err != nil {
panic(err)
}
certs, err := GetCertificatesPEM(domain + ":443")
if err != nil {
os.Chdir("..")
foldername := strings.Split(inputFile, ".")
os.RemoveAll(foldername[0])
log.Fatal("Error: The domain: " + domain + " does not exist or is not accessible from the host you are compiling on")
}
block, _ := pem.Decode([]byte(certs))
cert, _ := x509.ParseCertificate(block.Bytes)
keyToFile(domain+".key", rootKey)
SubjectTemplate := x509.Certificate{
SerialNumber: cert.SerialNumber,
Subject: pkix.Name{
CommonName: cert.Subject.CommonName,
},
NotBefore: cert.NotBefore,
NotAfter: cert.NotAfter,
BasicConstraintsValid: true,
IsCA: true,
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth},
}
IssuerTemplate := x509.Certificate{
SerialNumber: cert.SerialNumber,
Subject: pkix.Name{
CommonName: cert.Issuer.CommonName,
},
NotBefore: cert.NotBefore,
NotAfter: cert.NotAfter,
}
derBytes, err := x509.CreateCertificate(rand.Reader, &SubjectTemplate, &IssuerTemplate, &rootKey.PublicKey, rootKey)
if err != nil {
panic(err)
}
certToFile(domain+".pem", derBytes)
}
func keyToFile(filename string, key *rsa.PrivateKey) {
file, err := os.Create(filename)
if err != nil {
panic(err)
}
defer file.Close()
b, err := x509.MarshalPKCS8PrivateKey(key)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to marshal RSA private key: %v", err)
os.Exit(2)
}
if err := pem.Encode(file, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: b}); err != nil {
panic(err)
}
}
func certToFile(filename string, derBytes []byte) {
certOut, err := os.Create(filename)
if err != nil {
log.Fatalf("[-] Failed to Open cert.pem for Writing: %s", err)
}
if err := pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}); err != nil {
log.Fatalf("[-] Failed to Write Data to cert.pem: %s", err)
}
if err := certOut.Close(); err != nil {
log.Fatalf("[-] Error Closing cert.pem: %s", err)
}
}
func GetCertificatesPEM(address string) (string, error) {
conn, err := tls.Dial("tcp", address, &tls.Config{
InsecureSkipVerify: true,
})
if err != nil {
return "", err
}
defer conn.Close()
var b bytes.Buffer
for _, cert := range conn.ConnectionState().PeerCertificates {
err := pem.Encode(&b, &pem.Block{
Type: "CERTIFICATE",
Bytes: cert.Raw,
})
if err != nil {
return "", err
}
}
return b.String(), nil
}
func GeneratePFK(password string, domain string) {
cmd := exec.Command("openssl", "pkcs12", "-export", "-out", domain+".pfx", "-inkey", domain+".key", "-in", domain+".pem", "-passin", "pass:"+password+"", "-passout", "pass:"+password+"")
err := cmd.Run()
if err != nil {
log.Fatalf("cmd.Run() failed with %s\n", err)
}
}
func SignExecutable(password string, pfx string, filein string, fileout string) {
cmd := exec.Command("osslsigncode", "sign", "-pkcs12", pfx, "-in", ""+filein+"", "-out", ""+fileout+"", "-pass", ""+password+"")
err := cmd.Run()
if err != nil {
log.Fatalf("cmd.Run() failed with %s\n", err)
}
}
func Check(check string) {
cmd := exec.Command("osslsigncode", "verify", ""+check+"")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
log.Fatalf("cmd.Run() failed with %s\n", err)
}
}
func options() *FlagOptions {
outFile := flag.String("O", "", "Signed file name")
inputFile := flag.String("I", "", "Unsiged file name to be signed")
domain := flag.String("Domain", "", "Domain you want to create a fake code sign for")
password := flag.String("Password", "", "Password for real certificate")
real := flag.String("Real", "", "Path to a valid .pfx certificate file")
verify := flag.String("Verify", "", "Verifies a file's code sign certificate")
debug := flag.Bool("debug", false, "Print debug statements")
flag.Parse()
debugging = *debug
debugWriter = os.Stdout
return &FlagOptions{outFile: *outFile, inputFile: *inputFile, domain: *domain, password: *password, real: *real, verify: *verify}
}
func main() {
fmt.Println(`
.____ .__ .____ .__ .__ __
| | |__| _____ ____ | | |__| ____ | |___/ |_ ___________
| | | |/ \_/ __ \| | | |/ ___\| | \ __\/ __ \_ __ \
| |___| | Y Y \ ___/| |___| / /_/ > Y \ | \ ___/| | \/
|_______ \__|__|_| /\___ >_______ \__\___ /|___| /__| \___ >__|
\/ \/ \/ \/ /_____/ \/ \/
@Tyl0us
[*] A Tool for Code Signing... Real and fake`)
opt := options()
if opt.verify == "" && opt.inputFile == "" && opt.outFile == "" {
log.Fatal("Error: Please provide a file to sign or a file check")
}
if opt.verify == "" && opt.inputFile == "" {
log.Fatal("Error: Please provide a file to sign")
}
if opt.verify == "" && opt.outFile == "" {
log.Fatal("Error: Please provide a name for the signed file")
}
if opt.real == "" && opt.domain == "" && opt.verify == "" {
log.Fatal("Error: Please specify a valid path to a .pfx file or specify the domain to spoof")
}
if opt.verify != "" {
fmt.Println("[*] Checking code signed on file: " + opt.verify)
Check(opt.verify)
os.Exit(3)
}
if opt.real != "" {
fmt.Println("[*] Signing " + opt.inputFile + " with a valid cert " + opt.real)
SignExecutable(opt.password, opt.real, opt.inputFile, opt.outFile)
} else {
password := VarNumberLength(8, 12)
pfx := opt.domain + ".pfx"
fmt.Println("[*] Signing " + opt.inputFile + " with a fake cert")
GenerateCert(opt.domain, opt.inputFile)
GeneratePFK(password, opt.domain)
SignExecutable(password, pfx, opt.inputFile, opt.outFile)
}
fmt.Println("[*] Cleaning up....")
printDebug("[!] Deleting " + opt.domain + ".pem\n")
os.Remove(opt.domain + ".pem")
printDebug("[!] Deleting " + opt.domain + ".key\n")
os.Remove(opt.domain + ".key")
printDebug("[!] Deleting " + opt.domain + ".pfx\n")
os.Remove(opt.domain + ".pfx")
fmt.Println(color.GreenString("[+] ") + "Signed File Created.")
}