Пример #1
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="session"></param>
        /// <param name="message"></param>
        /// <param name="outputDir"></param>
        private void HashFiles(Session session,
                               MimeKit.MimeMessage message,
                               string parentDir,
                               string outputDir)
        {
            if (System.IO.Directory.Exists(outputDir) == false)
            {
                return;
            }

            string mailFrom = GetSmtpMailFrom(System.IO.Path.Combine(this.dataDirectory,
                                                                     session.Guid.Substring(0, 2),
                                                                     session.Guid + ".bin"));
            string from = string.Empty;

            if (message.From != null)
            {
                List<string> temp = new List<string>();
                Regex regex = new Regex("<(.*?)>", RegexOptions.IgnoreCase);
                for(int index = 0; index < message.From.Count; index++)
                {
                    Match match = regex.Match(message.From[index].ToString());
                    if (match.Success == true)
                    {
                        temp.Add(match.Groups[1].Value);
                    }
                    else
                    {
                        temp.Add(message.From[index].ToString());
                    }

                }
                from = string.Join(",", temp);
            }

            string to = string.Empty;
            if (message.To != null)
            {
                List<string> temp = new List<string>();
                Regex regex = new Regex("<(.*?)>", RegexOptions.IgnoreCase);
                for (int index = 0; index < message.From.Count; index++)
                {
                    Match match = regex.Match(message.To[index].ToString());
                    if (match.Success == true)
                    {
                        temp.Add(match.Groups[1].Value);
                    }
                    else
                    {
                        temp.Add(message.To[index].ToString());
                    }

                }
                to = string.Join(",", temp);
            }

            string sender = string.Empty;
            if (message.Sender != null)
            {
                sender = message.Sender.Address;
            }

            MessageDetails messageDetails = new MessageDetails();
            messageDetails.SrcIp = session.SrcIpText;
            messageDetails.SrcPort = session.SourcePort;
            messageDetails.DstIp = session.DstIpText;
            messageDetails.DstPort = session.DestinationPort;
            messageDetails.From = from;
            messageDetails.To = to;
            messageDetails.MailFrom = mailFrom;
            messageDetails.Sender = sender;
            messageDetails.Subject = message.Subject;
            messageDetails.Date = message.Date.ToString("o");

            // Now MD5 the files
            foreach (string file in System.IO.Directory.EnumerateFiles(outputDir,
                                                                        "*.*",
                                                                        SearchOption.AllDirectories))
            {
                if (System.IO.Path.GetFileName(file) == "Message.Info.txt")
                {
                    continue;
                }

                try
                {
                    // Not sure if BufferedStream should be wrapped in using block
                    using (var stream = new BufferedStream(File.OpenRead(file), 1200000))
                    {
                        MD5 md5 = new MD5CryptoServiceProvider();
                        byte[] hashMd5 = md5.ComputeHash(stream);

                        AttachmentDetails attachmentDetails = new AttachmentDetails();
                        attachmentDetails.File = file;
                        attachmentDetails.Md5 = woanware.Text.ConvertByteArrayToHexString(hashMd5);
                        messageDetails.Attachments.Add(attachmentDetails);
                    }
                }
                catch (Exception) { }
            }

             messageDetails.Save(System.IO.Path.Combine(outputDir, "Message.Details." + message.MessageId + ".xml"));
        }
Пример #2
0
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public string Load(string path)
        {
            try
            {
                if (System.IO.File.Exists(path) == false)
                {
                    return string.Empty;
                }

                XmlSerializer serializer = new XmlSerializer(typeof(MessageDetails));

                FileInfo info = new FileInfo(path);
                using (FileStream stream = info.OpenRead())
                {
                    MessageDetails messageDetails = (MessageDetails)serializer.Deserialize(stream);
                    this.SrcIp = messageDetails.SrcIp;
                    this.SrcPort = messageDetails.SrcPort;
                    this.DstIp = messageDetails.DstIp;
                    this.DstPort = messageDetails.DstPort;
                    this.From = messageDetails.From;
                    this.To = messageDetails.To;
                    this.MailFrom = messageDetails.MailFrom;
                    this.Sender = messageDetails.Sender;
                    this.Subject = messageDetails.Subject;
                    this.Date = messageDetails.Date;

                    foreach (AttachmentDetails temp in messageDetails.Attachments)
                    {
                        AttachmentDetails attachmentDetails = new AttachmentDetails();
                        attachmentDetails.File = temp.File;
                        attachmentDetails.Md5 = temp.Md5;
                        this.Attachments.Add(attachmentDetails);
                    }

                    return string.Empty;
                }
            }
            catch (FileNotFoundException fileNotFoundEx)
            {
                return fileNotFoundEx.Message;
            }
            catch (UnauthorizedAccessException unauthAccessEx)
            {
                return unauthAccessEx.Message;
            }
            catch (IOException ioEx)
            {
                return ioEx.Message;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }