Ejemplo n.º 1
0
        public void ActionSendMail(EmailEntity emailEntity)
        {
            // Lay danh sach CVE
            ModCVEEntity cve = ModCVEService.Instance.CreateQuery().Where(o => o.ID == emailEntity.RecordID).ToSingle();

            // Lay template
            ModEmailTemplateEntity emailTemp = ModEmailTemplateService.Instance.CreateQuery().Where(o => o.Activity == true && o.Code == "Type1").ToSingle();

            // Lay he thong thong tin theo CVE tuong ung
            string s     = cve.VulnerabilityType;
            var    query = ModHeThongThongTinService.Instance.CreateQuery()
                           .Where(o => o.Activity == true)
                           .Where(!string.IsNullOrEmpty(s), o => o.Name.Contains(s));
            List <ModHeThongThongTinEntity> lstHttt = query.ToList();

            string emailTos = "";

            if (lstHttt != null)
            {
                string dauMoiIds = string.Join(",", lstHttt.Select(o => o.DauMoiUCSCID).ToArray());
                string donDkIds  = string.Join(",", lstHttt.Select(o => o.DonDangKyUCSCID).ToArray());

                // Lay dau moi UCSC
                List <ModDauMoiUCSCEntity> lstDauMoi = ModDauMoiUCSCService.Instance.CreateQuery()
                                                       .Where(o => o.Activity == true)
                                                       .WhereIn(dauMoiIds != "", o => o.ID, dauMoiIds)
                                                       .ToList();
                emailTos = string.Join(";", lstDauMoi.Select(o => o.Email).ToArray());

                // Lay don DK
                List <ModDonDangKyUCSCEntity> lstDangKy = ModDonDangKyUCSCService.Instance.CreateQuery()
                                                          .Where(o => o.Activity == true)
                                                          .WhereIn(donDkIds != "", o => o.ID, donDkIds)
                                                          .ToList();
                emailTos += ";" + string.Join(";", lstDangKy.Select(o => o.ToChuc_Email).ToArray());
                emailTos.Replace(";;", ";").Replace(";;", ";").Replace(";;", ";").Trim(';');
            }

            if (emailTemp != null)
            {
                if (emailTos != "")
                {
                    emailEntity.To = emailTos;
                }
                emailEntity.Cc      = "*****@*****.**";
                emailEntity.Subject = string.Format(emailTemp.Subject, "");
                emailEntity.Body    = cve.VulnerabilityType;
            }

            ViewBag.Data = emailEntity;
        }
Ejemplo n.º 2
0
        public void ActionAdd(ModEmailTemplateModel model)
        {
            if (model.RecordID > 0)
            {
                entity = ModEmailTemplateService.Instance.GetByID(model.RecordID);

                // khoi tao gia tri mac dinh khi update
            }
            else
            {
                entity = new ModEmailTemplateEntity();

                // khoi tao gia tri mac dinh khi insert
                entity.MenuID = model.MenuID;
                entity.Activity = CPViewPage.UserPermissions.Approve;
                entity.Order = GetMaxOrder(model);
            }

            ViewBag.Data = entity;
            ViewBag.Model = model;
        }
Ejemplo n.º 3
0
        //public void ExportToPDF(ModIncidentModel model)
        //{
        //    RenderView("Index");

        //    //lấy danh sách thuật ngữ
        //    var listEntity = ModIncidentService.Instance.CreateQuery()
        //                //.Where(o => o.Activity == true)
        //                .ToList();

        //    //khai báo tập hợp bản ghi excel
        //    List<List<object>> list = new List<List<object>>();
        //    //khai báo 1 dòng excel
        //    List<object> _list = null;
        //    string cityname = "";
        //    for (int i = 0; listEntity != null && listEntity.Count > 0 && i < listEntity.Count; i++)
        //    {
        //        _list = new List<object>();
        //        //add 1 dòng excel
        //        // _list.Add(i + 1);
        //        _list.Add(listEntity[i].Name);
        //        _list.Add(cityname);
        //        _list.Add(listEntity[i].Published);
        //        list.Add(_list);
        //    }

        //    //ghi exel
        //    string temp_file = CPViewPage.Server.MapPath("~/Data/upload/files/Excel/DanhSachSuCo_" +
        //    string.Format("{0:dd_MM_yyyy}", DateTime.Now) + ".xls");
        //    string filePath = CPViewPage.Server.MapPath("~/TTPortal/Templates/DanhSachSuCo.xlsx");



        //    CPViewPage.Response.ContentType = "application/pdf";
        //    CPViewPage.Response.AddHeader("content-disposition", "attachment;filename=DataTable.pdf");
        //    CPViewPage.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        //    StringWriter sw = new StringWriter();
        //    HtmlTextWriter hw = new HtmlTextWriter(sw);
        //    GridView1.RenderControl(hw);
        //    StringReader sr = new StringReader(sw.ToString());
        //    Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
        //    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        //    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        //    pdfDoc.Open();
        //    htmlparser.Parse(sr);
        //    pdfDoc.Close();
        //    CPViewPage.Response.Write(pdfDoc);
        //    CPViewPage.Response.End();
        //}

        public void ActionSendMail(EmailEntity emailEntity)
        {
            // Lay danh sach su co
            ModIncidentEntity        suCoParent = ModIncidentService.Instance.CreateQuery().Where(o => o.ID == emailEntity.RecordID).ToSingle();
            List <ModIncidentEntity> suCos = null;
            WebMenuEntity            menuIncident = null;
            string sSuCo = string.Empty, isp = string.Empty, ip = string.Empty;
            string s2 = string.Empty, s3 = string.Empty, s4 = string.Empty, sName = string.Empty;

            //{2}: (giả mạo (phishing), nhiễm mã độc  (malware), (thay đổi giao diện) Deface )
            //{3}: , và các cố {2}
            // {4}: {2} + {3}
            if (suCoParent != null)
            {
                suCos  = ModIncidentService.Instance.CreateQuery().Where(o => o.Resolve == false && o.ParentID == suCoParent.ID).ToList();
                sSuCo += "<b><span>" + suCoParent.Path + "</span></b>";
                isp    = suCoParent.ISP;
                ip     = suCoParent.IP;
            }
            if (suCos != null && suCos.Count > 0)
            {
                int countSuCo = suCos.Count;
                for (int i = 0; i < countSuCo; i++)
                {
                    menuIncident = WebMenuService.Instance.CreateQuery().Where(o => o.Activity == true && o.Type == "Incident" && o.ID == suCos[i].MenuID).ToSingle();
                    if (menuIncident != null)
                    {
                        sName = menuIncident.Code == "Phishing" ? "giả mạo (Phishing)" : menuIncident.Code == "Malware" ? "nhiễm mã độc(Malware)" : "thay đổi giao diện (Deface)";
                        if (i == 0)
                        {
                            s2 = sName;
                        }
                        else
                        {
                            s3 += sName;
                            if (i < countSuCo - 1)
                            {
                                s3 += ", ";
                            }
                        }
                    }
                    sSuCo += "<br />&nbsp;&nbsp;&nbsp;&nbsp;" + suCos[i].Path + "<br />";
                }
            }
            sSuCo += ip;

            // Lay template
            ModEmailTemplateEntity emailTemp = ModEmailTemplateService.Instance.CreateQuery().Where(o => o.Activity == true && o.Code == "Type1").ToSingle();

            // Lay email gui di theo ISP
            ModISPEntity ispEntity = ModISPService.Instance.CreateQuery().Where(o => o.Activity == true && o.Name == isp).ToSingle();

            if (emailTemp != null)
            {
                if (ispEntity != null)
                {
                    emailEntity.To = ispEntity.Email;
                }
                emailEntity.Cc      = "*****@*****.**";
                emailEntity.Subject = string.Format(emailTemp.Subject, isp);
                emailEntity.Body    = string.Format(emailTemp.Content, isp, sSuCo, s2, (!string.IsNullOrEmpty(s3) ? ", và các sự cố " + s3 : ""), s2 + (!string.IsNullOrEmpty(s3) ? ", " + s3 : ""));
            }

            ViewBag.Data = emailEntity;
        }
Ejemplo n.º 4
0
        private static void SendMail()
        {
            // Khai bao bien
            List <ModIncidentEntity> incidents;
            EmailEntity   emailEntity;
            DateTime      dateNow = DateTime.Now;
            DateTime      ngayDauTuan;
            TimeSpan      subtract;
            WebMenuEntity menuIncident = null;
            WebMenuEntity chuKyGuiMail = null;
            bool          allowSend = false;
            string        sSuCo = string.Empty, isp = string.Empty, ip = string.Empty;
            string        s2 = string.Empty, s3 = string.Empty, s4 = string.Empty, sName = string.Empty;
            string        tempFolderConfig = ConfigurationManager.AppSettings.Get("TempFolder");
            string        exportTemp       = ConfigurationManager.AppSettings.Get("ExportTemp");
            string        exportConfig     = ConfigurationManager.AppSettings.Get("Export");

            if (string.IsNullOrEmpty(tempFolderConfig))
            {
                Console.WriteLine("ERROR: Ban chua cau hinh cho key TempFolder.");
                Console.WriteLine("===== Ket thuc chuong trinh =====");
                Logger.Error($"Ban chua cau hinh cho key TempFolder.");
                return;
            }
            if (string.IsNullOrEmpty(exportConfig))
            {
                Console.WriteLine("ERROR: Ban chua cau hinh cho key Export.");
                Console.WriteLine("===== Ket thuc chuong trinh =====");
                Logger.Error($"Ban chua cau hinh cho key Export.");
                return;
            }
            if (string.IsNullOrEmpty(exportTemp))
            {
                Console.WriteLine("ERROR: Ban chua cau hinh cho key ExportTemp.");
                Console.WriteLine("===== Ket thuc chuong trinh =====");
                Logger.Error($"Ban chua cau hinh cho key ExportTemp.");
                return;
            }

            // Lay ds Dich vu canh bao
            List <ModDichVuCanhBaoEntity> dvs = ModDichVuCanhBaoService.Instance.CreateQuery()
                                                .Where(o => o.Activity == true)
                                                .ToList();

            if (dvs == null)
            {
                Console.WriteLine("INFO: Khong co dich vu canh bao nao.");
                Console.WriteLine("===== Ket thuc chuong trinh =====");
                Logger.Info($"Khong co dich vu canh bao nao.");
                return;
            }

            // Lay template
            ModEmailTemplateEntity emailTemp = ModEmailTemplateService.Instance.CreateQuery().Where(o => o.Activity == true && o.Code == "Type2").ToSingle();

            if (emailTemp == null)
            {
                Console.WriteLine("FAILED: Khong co Email template.");
                Console.WriteLine("===== Ket thuc chuong trinh =====");
                Logger.Error($"Khong co Email template.");
                return;
            }

            // Duyet qua cac ban ghi dv canh bao va thuc hien gui mail tuong ung
            foreach (ModDichVuCanhBaoEntity dv in dvs)
            {
                // Lay log gui mail
                string incidentIds = string.Empty;
                ModSendMailLogsEntity sendMailLog = ModSendMailLogsService.Instance.CreateQuery().Where(o => o.Activity == true && o.DichVuCanhBaoID == dv.ID).OrderByDesc(o => o.Publish).ToSingle();
                if (sendMailLog != null)
                {
                    // Neu dich vu canh bao da gui mail trong ngay hom nay roi thi khong thuc hien gui mail nua
                    if (sendMailLog.Publish.Date == dateNow.Date)
                    {
                        continue;
                    }
                    incidentIds = sendMailLog.IncidentIDs.Trim(',');
                }

                incidents = ModIncidentService.Instance.CreateQuery()
                            .Where(o => o.Activity == true && o.Resolve == false)
                            .WhereIn(incidentIds != "", o => o.ID, incidentIds)
                            .ToList();

                if (incidents == null)
                {
                    continue;
                }

                // Lay chu ky gui mail
                chuKyGuiMail = WebMenuService.Instance.CreateQuery()
                               .Where(o => o.Activity == true && o.ID == dv.MenuID)
                               .ToSingle();
                if (chuKyGuiMail == null)
                {
                    Console.WriteLine("WARN: Dich vu canh bao co ID = " + dv.ID + " khong co chu ky gui mail.");
                    Logger.Warn($"Dich vu canh bao co ID = {dv.ID} khong co chu ky gui mail.");
                    continue;
                }

                allowSend = false;
                subtract  = dv.Time.Subtract(dateNow.TimeOfDay);
                switch (chuKyGuiMail.Code)
                {
                case "Nam":
                    if (dateNow.Day.ToString() == "1" && dateNow.Month.ToString() == "1")
                    {
                        if (subtract.Hours == 0 && subtract.Minutes <= 5)
                        {
                            allowSend = true;
                        }
                    }
                    break;

                case "Quy":
                    if (dateNow.Day.ToString() == "1" && dateNow.Month.ToString() == "4" ||
                        dateNow.Day.ToString() == "1" && dateNow.Month.ToString() == "7" ||
                        dateNow.Day.ToString() == "1" && dateNow.Month.ToString() == "10" ||
                        dateNow.Day.ToString() == "1" && dateNow.Month.ToString() == "1")
                    {
                        if (subtract.Hours == 0 && subtract.Minutes <= 5)
                        {
                            allowSend = true;
                        }
                    }
                    break;

                case "Thang":
                    if (dateNow.Day.ToString() == "1")
                    {
                        if (subtract.Hours == 0 && subtract.Minutes <= 5)
                        {
                            allowSend = true;
                        }
                    }
                    break;

                case "Tuan":
                    ngayDauTuan = GetFirstDayOfWeek(dateNow);
                    if (ngayDauTuan == dateNow)
                    {
                        if (subtract.Hours == 0 && subtract.Minutes <= 5)
                        {
                            allowSend = true;
                        }
                    }
                    break;

                case "Ngay":
                    if (subtract.Hours == 0 && subtract.Minutes <= 5)
                    {
                        allowSend = true;
                    }
                    break;

                default:
                    allowSend = false;
                    break;
                }

                if (allowSend == false)
                {
                    continue;
                }

                try
                {
                    if (incidents != null && incidents.Count > 0)
                    {
                        int countSuCo = incidents.Count;
                        for (int i = 0; i < countSuCo; i++)
                        {
                            menuIncident = WebMenuService.Instance.CreateQuery().Where(o => o.Activity == true && o.Type == "Incident" && o.ID == incidents[i].MenuID).ToSingle();
                            if (menuIncident != null)
                            {
                                sName = menuIncident.Code == "Phishing" ? "giả mạo (Phishing)" : menuIncident.Code == "Malware" ? "nhiễm mã độc(Malware)" : "thay đổi giao diện (Deface)";
                                if (i == 0)
                                {
                                    s2 = sName;
                                }
                                else
                                {
                                    s3 += sName;
                                    if (i < countSuCo - 1)
                                    {
                                        s3 += ", ";
                                    }
                                }
                            }
                            //sSuCo += "<br />&nbsp;&nbsp;&nbsp;&nbsp;" + incidents[i].Path + "<br />";
                        }
                    }

                    emailEntity = new EmailEntity
                    {
                        To      = dv.ToEmails,
                        Cc      = string.IsNullOrEmpty(dv.CcEmails) ? "*****@*****.**" : dv.CcEmails,
                        Subject = string.Format(emailTemp.Subject, dv.Name),
                        Body    = string.Format(emailTemp.Content, dv.Name, s2 + (!string.IsNullOrEmpty(s3) ? ", " + s3 : "")),
                        Attach  = Export(dv, incidents, "Word")
                    };

                    // Goi ham send mail
                    Console.WriteLine("INFO: Thuc hien gui mail...");
                    string sendResult = Mail.SendMailUseSMTP(emailEntity.To, emailEntity.Cc, emailEntity.Subject, emailEntity.Body, emailEntity.Attach);
                    Console.Write("INFO: Ket qua gui mail:\n" + sendResult);
                    Logger.Info($"Ket qua gui mail: {sendResult}");

                    // Ghi lai log gui mail
                    ModSendMailLogsEntity logEntity = new ModSendMailLogsEntity()
                    {
                        DichVuCanhBaoID = dv.ID,
                        EmailTo         = emailEntity.To,
                        EmailCc         = emailEntity.Cc,
                        Subject         = emailEntity.Subject,
                        Body            = emailEntity.Body,
                        IncidentIDs     = incidentIds + "," + string.Join(",", incidents.Select(o => o.ID).ToArray()),
                        SendSuccess     = string.IsNullOrEmpty(sendResult) ? true : false,
                        Publish         = DateTime.Now,
                        Activity        = true
                    };
                    ModSendMailLogsService.Instance.Save(logEntity);
                }
                catch (Exception ex)
                {
                    Logger.Error($"Failed: {ex.Message}\n {ex.StackTrace}");
                    Console.WriteLine("Exception: " + ex.Message);
                }
            }
        }