From cc2cd013be22440e7819f177833940c2b8c30aa0 Mon Sep 17 00:00:00 2001 From: Gregory Rudolph Date: Sat, 15 Feb 2020 09:02:23 -0500 Subject: [PATCH] Initial commit --- .gitignore | 6 ++++ conf.json.example | 6 ++++ default.sig.example | 1 + mailHelper.go | 31 ++++++++++++++++++ main.go | 78 +++++++++++++++++++++++++++++++++++++++++++++ pgpHelper.go | 52 ++++++++++++++++++++++++++++++ priv.key.example | 1 + test.pgp | 2 ++ types.go | 20 ++++++++++++ 9 files changed, 197 insertions(+) create mode 100644 .gitignore create mode 100644 conf.json.example create mode 100644 default.sig.example create mode 100644 mailHelper.go create mode 100644 main.go create mode 100644 pgpHelper.go create mode 100644 priv.key.example create mode 100644 test.pgp create mode 100644 types.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..58efc3f --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +conf.json +priv.key +emailBot +Keybase-Email.log +*.eml +*.sig diff --git a/conf.json.example b/conf.json.example new file mode 100644 index 0000000..727fc71 --- /dev/null +++ b/conf.json.example @@ -0,0 +1,6 @@ +{ + "email": "rudi@nmare.net", + "smtp_server":"oogieboogie.nightmare.haus:587", + "auth_server":"oogieboogie.nightmare.haus", + "private_key":"" +} diff --git a/default.sig.example b/default.sig.example new file mode 100644 index 0000000..eb6ed59 --- /dev/null +++ b/default.sig.example @@ -0,0 +1 @@ +This email message and attachment(s) may contain sensitive and/or proprietary information and is intended only for the person(s) to whom this email message is addressed. If you have received this email message in error, please notify the sender immediately and destroy the original message without making a copy. Please do not transmit any sensitive, proprietary, ITARS or FOUO data via e-mail without using approved encryption techniques. diff --git a/mailHelper.go b/mailHelper.go new file mode 100644 index 0000000..ffa664c --- /dev/null +++ b/mailHelper.go @@ -0,0 +1,31 @@ +package main + +import "fmt" +import "net/smtp" + +func send(e Email) { + e.Body = signMessage(e.Body) + + message := fmt.Sprintf("From: %s\n", conf.MyEmail) + for _, recipient := range e.Recipients { + message += fmt.Sprintf("To: %s\n", recipient) + } + for _, cc := range e.Cc { + message += fmt.Sprintf("Cc: %s\n", cc) + } + for _, bcc := range e.Bcc { + message += fmt.Sprintf("Bcc: %s\n", bcc) + } + message += fmt.Sprintf("Subject: %s\n", e.Subject) + message += e.Body + log.LogInfo("Message created") + log.LogDebug(message) + log.LogInfo("Sending message") + err := smtp.SendMail(conf.SmtpServer, + smtp.PlainAuth("", conf.MyEmail, conf.EmailPass, conf.AuthServer), + conf.MyEmail, e.Recipients, []byte(message)) + if err != nil { + log.LogErrorType(err) + } + log.LogInfo("Email Sent") +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..cbe30a8 --- /dev/null +++ b/main.go @@ -0,0 +1,78 @@ +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "strings" + "syscall" + "time" + + "github.com/rudi9719/loggy" + "golang.org/x/crypto/ssh/terminal" + "samhofi.us/x/keybase" +) + +var ( + k = keybase.NewKeybase() + + logOpts = loggy.LogOpts{ + //OutFile: "Keybase-Email.log", + //KBTeam: "nightmarehaus.logs", + //KBChann: "general", + //ProgName: "KB-Email", + Level: 5, + UseStdout: true, + } + + log = loggy.NewLogger(logOpts) + conf = Config{} +) + +func main() { + if !k.LoggedIn { + log.LogPanic("Keybase not logged in.") + } + log.LogInfo(fmt.Sprintf("Bot started using account %s", k.Username)) + conf = loadConfig() + setupCredentials() + em := Email{ + Recipients: []string{"rudi@nmare.net"}, + Subject: "Test Email", + Body: "Hello, world!", + } + send(em) + time.Sleep(2 * time.Second) + +} +func loadConfig() Config { + var c Config + bytes, err := ioutil.ReadFile("conf.json") + if err != nil { + log.LogErrorType(err) + } + err = json.Unmarshal(bytes, &c) + if err != nil { + log.LogErrorType(err) + } + bytes, err = ioutil.ReadFile("priv.key") + if err != nil { + log.LogErrorType(err) + } + c.PrivateKey = string(bytes) + return c +} +func setupCredentials() { + log.LogCritical("Enter pgp key passphrase:") + bytePass, err := terminal.ReadPassword(int(syscall.Stdin)) + if err != nil { + log.LogCritical(fmt.Sprintf("Error reading pgp password:\n```%+v```", err)) + } + conf.KeyPass = strings.TrimSpace(string(bytePass)) + log.LogCritical("Enter email passphrase:") + bytePass, err = terminal.ReadPassword(int(syscall.Stdin)) + if err != nil { + log.LogCritical(fmt.Sprintf("Error reading email password:\n```%+v```", err)) + } + conf.EmailPass = strings.TrimSpace(string(bytePass)) +} diff --git a/pgpHelper.go b/pgpHelper.go new file mode 100644 index 0000000..92a5b32 --- /dev/null +++ b/pgpHelper.go @@ -0,0 +1,52 @@ +package main + +import ( + "bytes" + // "encoding/base64" + "fmt" + "golang.org/x/crypto/openpgp" + "golang.org/x/crypto/openpgp/clearsign" + "strings" +) + +func getPrivateKey() *openpgp.Entity { + pp := conf.KeyPass + ppb := []byte(pp) + log.LogInfo("Getting entityList") + entitylist, err := openpgp.ReadArmoredKeyRing(strings.NewReader(conf.PrivateKey)) + if err != nil { + log.LogErrorType(err) + } + log.LogInfo(fmt.Sprintf("Getting entity 0 ```%+v```", entitylist)) + entity := entitylist[0] + log.LogInfo("if PrivateKey != nil") + if entity.PrivateKey != nil && entity.PrivateKey.Encrypted { + err := entity.PrivateKey.Decrypt(ppb) + if err != nil { + fmt.Println("Failed to decrypt key") + } + } + + for _, subkey := range entity.Subkeys { + if subkey.PrivateKey != nil && subkey.PrivateKey.Encrypted { + err := subkey.PrivateKey.Decrypt(ppb) + if err != nil { + fmt.Println("Failed to decrypt subkey") + } + } + } + return entity +} + +func signMessage(m string) string { + pk := getPrivateKey() + out := new(bytes.Buffer) + in, err := clearsign.Encode(out, pk.PrivateKey, nil) + //in, err := openpgp.Sign(out, pk, nil, nil) + if err != nil { + log.LogErrorType(err) + } + in.Write([]byte(m)) + in.Close() + return out.String() +} diff --git a/priv.key.example b/priv.key.example new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/priv.key.example @@ -0,0 +1 @@ + diff --git a/test.pgp b/test.pgp new file mode 100644 index 0000000..26fc7be --- /dev/null +++ b/test.pgp @@ -0,0 +1,2 @@ +LS0tLS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TkdSUHRRWHVQeE5nbmtmbmYzNApXYjNuWGJkS3UwOUlIUXlnSUIzTQo9WXdvdgotLS0tLUVORCBQR1AgU0lHTkFUVVJFLS0tLS0= + diff --git a/types.go b/types.go new file mode 100644 index 0000000..9ea15fe --- /dev/null +++ b/types.go @@ -0,0 +1,20 @@ +package main + +// Email for sending +type Email struct { + Recipients []string + Subject string + Cc []string + Bcc []string + Body string +} + +// Config struct +type Config struct { + PrivateKey string `json:"private_key"` + KeyPass string `json:"key_pass,omitempty"` + MyEmail string `json:"email"` + EmailPass string `json:"email_pass,omitempty"` + SmtpServer string `json:"smtp_server"` + AuthServer string `json:"auth_server"` +}