Last active
July 20, 2023 10:57
-
-
Save amlwwalker/f445932d2fdb0f9f9a5e457c1894bf7d to your computer and use it in GitHub Desktop.
check spf, dmarc, dkim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"net" | |
"regexp" | |
"encoding/base64" | |
"crypto/x509" | |
"crypto/rsa" | |
// "math/big" | |
"errors" | |
) | |
func LookupSPF(domain string) (string, error) { | |
txtRecords, err := net.LookupTXT(domain) | |
if err != nil { | |
return "", err | |
} | |
for _, record := range txtRecords { | |
match, _ := regexp.MatchString("^v=spf1.*", record) | |
if match == true { | |
return record, nil | |
} | |
} | |
return "", nil | |
} | |
func LookupDMARC(domain string) (string, error) { | |
dmarcDomain := "_dmarc." + domain | |
txtRecords, err := net.LookupTXT(dmarcDomain) | |
if err != nil { | |
return "", err | |
} | |
for _, record := range txtRecords { | |
match, _ := regexp.MatchString("^v=DMARC1.*", record) | |
if match == true { | |
return record, nil | |
} | |
} | |
return "", nil | |
} | |
func LookupDKIM(domain string) (string, error) { | |
dmarcDomain := "dkim._domainkey." + domain | |
txtRecords, err := net.LookupTXT(dmarcDomain) | |
if err != nil { | |
return "", err | |
} | |
for _, record := range txtRecords { | |
re := regexp.MustCompile(`v=DKIM1; k=rsa; p=(.*)$`) | |
result := re.FindStringSubmatch(record) | |
if len(result) > 1 { | |
return result[1], nil | |
} | |
} | |
return "", errors.New("no dkim cert found") | |
} | |
func testPublicKey(k string) error { | |
key, _ := base64.StdEncoding.DecodeString(k) | |
re, err := x509.ParsePKIXPublicKey(key) | |
pub := re.(*rsa.PublicKey) | |
if err != nil { | |
fmt.Println(err) | |
return err | |
} | |
fmt.Println(pub.E, " ", pub.N) | |
//why is this 32 bit not 2048 etc | |
fmt.Println("bits: ", len(pub.N.Bits())) | |
return nil | |
} | |
func main() { | |
domain := "ryanair.com" | |
result, err := LookupSPF(domain) | |
if err != nil { | |
fmt.Println("err: ", err) | |
} else { | |
fmt.Println("result: ", result) | |
} | |
result, err = LookupDMARC(domain) | |
if err != nil { | |
fmt.Println("err: ", err) | |
} else { | |
fmt.Println("result: ", result) | |
} | |
result, err = LookupDKIM(domain) | |
if err != nil { | |
fmt.Println("err: ", err) | |
} else { | |
fmt.Println("result: ", result) | |
testPublicKey(result) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment