1

I'm building a C# Console application in VS that asks a user to enter a domain e.g. example.net and returns all the mail records (A, MX, SPF, DMARC, DKIM, CNAME)

I have used the example below which will return a string of the SPF TXT records, but does not return the DMARC and DKIM TXT records.. Strange? - Or not?

I'm after the _domainkey.example.net and k1._domainkey.example.net (DKIM).

I'm after the _dmarc.example.net (DMARC).

private static IList<string> GetTxtRecords(string hostname)
        {
            IList<string> txtRecords = new List<string>();
            string output;
            string pattern = string.Format(@"{0}\s*text =\s*""([\w\-\=]*)""", hostname);

            var startInfo = new ProcessStartInfo("nslookup");
            startInfo.Arguments = string.Format("-type=TXT {0}", hostname);
            startInfo.RedirectStandardOutput = true;
            startInfo.UseShellExecute = false;
            startInfo.WindowStyle = ProcessWindowStyle.Hidden;

            using (var cmd = Process.Start(startInfo))
            {
                output = cmd.StandardOutput.ReadToEnd();
            }

            MatchCollection matches = Regex.Matches(output, pattern, RegexOptions.IgnoreCase);
            foreach (Match match in matches)
            {
                if (match.Success)
                    txtRecords.Add(match.Groups[1].Value);
            }

            return txtRecords;
        }

How do I get the DMARC and DOMAIN KEY (DKIM) Mail Records?

kingcobra
  • 23
  • 7
  • I'm not restricted to this code, but this is the only one I have managed to get to work so far? What do you suggest as an alternative? The DMARC and DKIM records are TXT records too so should they not be returned too? – kingcobra Mar 02 '17 at 19:56
  • Ah sorry, my bad. You're right.... Have a look at this answer: http://stackoverflow.com/a/25167028/75852. I haven't tried it but I would expect it to work by changing Record Type to TXT instead of MX. – squillman Mar 02 '17 at 20:07
  • 1
    I suggest you to use ARSoft.Tools.Net nuget package for these things, not nslookup. – Evk Mar 02 '17 at 20:09
  • dnsclient.net https://github.com/MichaCo/DnsClient.NET or similar tools would probably be easier to use than trying to run windows processes or build it yourself – MichaC Mar 03 '17 at 07:39
  • ARSoft.Tools.Net worked straight away and was super simple! - Thanks guys.. However, I'm struggling to get the DMARC and DKIM records with this. - I'll keep trying and will update. – kingcobra Mar 03 '17 at 11:40

1 Answers1

1

I would use a utility like ARSoft Tools for DNS to specify text records.

Secondly, a message that contains a DKIM signed header also includes the "selector", as it is called, for the sub-domain: mail._domainkey.msn.com. Where the "mail" sub-domain is the selector.

Included in the DKIM signature header is the headers which compromise the signature. You will need those to verify the authenticity of the message.