abusix / xarf

XARF - eXtended Abuse Reporting Format
https://abusix.com/xarf/
MIT License
110 stars 18 forks source link

.net library #36

Open jaycollett opened 2 years ago

jaycollett commented 2 years ago

I searched around on the internet looking for a .net compatible NuGet/library for this and came up empty. I'd be interested in building one if you were not already aware of someone doing so?

NXTwoThou commented 2 months ago

FYI, I put in XARF into my in-house security software to reduce my daily workload of filling out DigitalOcean reports. It was pretty simple. Tossed some of the json samples into https://json2csharp.com/, filled out the structure, serialized it out into a MemoryStream.

The only frustrating thing is I couldn't set up feedback-report with System.Net.Mail. Had to use MailKit. Putting this here in case anyone else kept fighting with System.Net.Mail.

` using MailKit; using MimeKit;

public class Xarf {
string userAgent = "MySoftware/1.0";
string myMailServer="mail.MySoftware.duh"; string myMailUserName="MyUser"; string myMailPassword="MyEasyToGuessPassword";

public void SendEmailXARF(string toEmail, string fromEmail, string subject, string body, MemoryStream memoryStreamXARF)
{
    var message = new MimeMessage();

    #region Primary headers
    message.From.Add(new MailboxAddress("", fromEmail));
    message.To.Add(new MailboxAddress("", toEmail));
    message.Subject = subject;
    #endregion

    #region Build multiparts
    var multipartBody = new TextPart("plain") { Text = body };
    var memoryStreamFeedbackType = new MemoryStream(ASCIIEncoding.ASCII.GetBytes("Feedback-Type: xarf\nUser-Agent: " + userAgent + "\nVersion: 1"));
    var multipartFeedbackType = new MimePart("message", "feedback-report")
    {
        Content = new MimeContent(memoryStreamFeedbackType),
        ContentDisposition = new ContentDisposition(ContentDisposition.Inline)
    };
    var multipartAttachmentXARF = new MimePart("application", "json")
    {
        Content = new MimeContent(memoryStreamXARF),
        ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
        ContentTransferEncoding = ContentEncoding.Base64,
        FileName = "xarf.json"
    };
    #endregion

    #region Add multiparts
    var multipart = new Multipart("report; report-type=feedback-report");
    multipart.Add(multipartBody);
    multipart.Add(multipartFeedbackType);
    multipart.Add(multipartAttachmentXARF);
    message.Body = multipart;
    #endregion

    #region Send email
    using (var smtpClient = new MailKit.Net.Smtp.SmtpClient())
    {
        smtpClient.Connect(myMailServer);
        smtpClient.Authenticate(myMailUserName, myMailPassword);
        smtpClient.Send(message);
        smtpClient.Disconnect(true);
    }
    #endregion
    memoryStreamFeedbackType.Dispose();
}

`