public void StoreShapeFileItems(List <ShapefileItem> items)
 {
     lock (this)
     {
         if (_dbcon != null)
         {
             using (var db = new NPoco.Database(_dbcon.Connection, NPoco.DatabaseType.SQLite))
             {
                 db.BeginTransaction();
                 try
                 {
                     db.Execute("delete from ShapeFileItemV2");
                     foreach (var s in items)
                     {
                         db.Insert("ShapeFileItemV2", null, s);
                     }
                     db.CompleteTransaction();
                 }
                 catch
                 {
                     db.AbortTransaction();
                 }
             }
         }
     }
 }
Exemple #2
0
 public void Insert <T>(T obj)
 {
     using (NPoco.IDatabase dbContext = new NPoco.Database(connectionString, NPoco.DatabaseType.SqlServer2012))
     {
         dbContext.Insert <T>(obj);
     }
 }
        public GeocacheCollection GetCollection(string name, bool createIfNotExists = false)
        {
            GeocacheCollection result = null;

            if (!string.IsNullOrEmpty(name))
            {
                lock (this)
                {
                    if (_dbcon != null)
                    {
                        using (var db = new NPoco.Database(_dbcon.Connection, NPoco.DatabaseType.SQLite))
                        {
                            result = db.FirstOrDefault <GeocacheCollection>("where Name like @0", name);
                            if (result == null && createIfNotExists)
                            {
                                //just the lazy way
                                var record = db.FirstOrDefault <GeocacheCollection>("order by CollectionID desc limit 1");
                                result              = new GeocacheCollection();
                                result.Name         = name;
                                result.CollectionID = record == null ? 1 : record.CollectionID + 1;
                                db.Insert(result);
                            }
                        }
                    }
                }
            }
            return(result);
        }
Exemple #4
0
        public DTO.Database.CurrencyExchangeRateDto InsertCurrencyExchangeRate(DTO.Database.CurrencyExchangeRateDto currencyExchangeRate)
        {
            using (NPoco.IDatabase dbContext = new NPoco.Database(DTO.Global.SQLConnectionString, NPoco.DatabaseType.SqlServer2012))
            {
                dbContext.Insert("tbl_CurrencyExchangeRate", "CurrencyExchangeRateID", currencyExchangeRate);
            }

            return(currencyExchangeRate);
        }
Exemple #5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                using (NPoco.Database db = new NPoco.Database(Db.GetOpenMySqlConnection()))
                {
                    Exclude exclude = new Exclude();

                    if (chkRule.Checked == true)
                    {
                        exclude.SigId = _ruleId;
                    }

                    if (chkSourceIp.Checked == true)
                    {
                        byte[] ip = _sourceIp.GetAddressBytes();
                        Array.Reverse(ip);
                        exclude.SourceIp = BitConverter.ToUInt32(ip, 0);
                    }

                    if (chkSrcPort.Checked == true)
                    {
                        exclude.SourcePort = ushort.Parse(txtSourcePort.Text);
                    }

                    if (chkDestinationIp.Checked == true)
                    {
                        byte[] ip = _destinationIp.GetAddressBytes();
                        Array.Reverse(ip);
                        exclude.DestinationIp = BitConverter.ToUInt32(ip, 0);
                    }

                    if (chkDestPort.Checked == true)
                    {
                        exclude.DestinationPort = ushort.Parse(txtDestinationPort.Text);
                    }

                    exclude.IpProto       = _ipProto;
                    exclude.Comment       = txtComment.Text;
                    exclude.FalsePositive = chkFalsePositive.Checked;
                    exclude.Timestamp     = DateTime.Now;

                    db.Insert(exclude);
                }
            }
            catch (Exception ex)
            {
                UserInterface.DisplayErrorMessageBox("An error occurred whilst adding the exclude: " + ex.Message);
                return;
            }

            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }
 public void StoreScriptItem(ScriptItem item)
 {
     lock (this)
     {
         if (_dbcon != null)
         {
             using (var db = new NPoco.Database(_dbcon.Connection, NPoco.DatabaseType.SQLite))
             {
                 if (db.FirstOrDefault <ScriptItem>("select * from Scripts where Name=@0", item.Name) == null)
                 {
                     db.Insert("Scripts", null, item);
                 }
                 else
                 {
                     db.Update("Scripts", "Name", item);
                 }
             }
         }
     }
 }
        /// <summary>
        /// 
        /// </summary>
        private void Process()
        {
            AcknowledgmentClass acknowledgmentClass = (AcknowledgmentClass)cboClassification.Items[cboClassification.SelectedIndex];
            string initials = txtInitials.Text.ToUpper();
            string notes = txtNotes.Text;
            bool successful = chkSuccessful.Checked;

            btnOk.Enabled = false;
            btnCancel.Enabled = false;

            (new Thread(() =>
            {
                try
                {
                    bool acknowledgedPrevious = false;
                    bool errors = false;
                    using (new HourGlass(this))
                    using (NPoco.Database db = new NPoco.Database(Db.GetOpenMySqlConnection()))
                    {
                        db.BeginTransaction();
                        foreach (Event temp in _events)
                        {
                            try
                            {
                                bool insert = true;
                                var ack = db.Fetch<Acknowledgment>("select * from acknowledgment where cid=@0 and sid=@1", new object[] { temp.Cid, temp.Sid });
                                if (ack.Count() > 0)
                                {
                                    if (ack.First().Initials.ToUpper() != initials)
                                    {
                                        acknowledgedPrevious = true;
                                        insert = false;
                                    }
                                    else
                                    {
                                        db.Delete(ack.First());
                                    }
                                }

                                if (insert == true)
                                {
                                    Acknowledgment acknowledgment = new Acknowledgment();
                                    acknowledgment.Cid = temp.Cid;
                                    acknowledgment.Sid = temp.Sid;
                                    acknowledgment.Initials = initials;
                                    acknowledgment.Notes = notes;
                                    acknowledgment.Class = acknowledgmentClass.Id;
                                    acknowledgment.Successful = successful;
                                    acknowledgment.Timestamp = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                                    db.Insert(acknowledgment);
                                }
                            }
                            catch (Exception ex)
                            {
                                db.AbortTransaction();
                                errors = true;
                                IO.WriteTextToFile("Acknowledgement Insert Error (" + DateTime.Now + "): " + ex.ToString() + Environment.NewLine,
                                                   System.IO.Path.Combine(Misc.GetUserDataDirectory(), "Errors.txt"),
                                                   true);
                                break;
                            }
                        }

                        if (errors == false)
                        {
                            db.CompleteTransaction();
                        }
                    }

                    if (acknowledgedPrevious == true)
                    {
                        UserInterface.DisplayMessageBox(this,
                                                        "Some events were not classified due to being already classified",
                                                        MessageBoxIcon.Exclamation);
                    }

                    if (errors == true)
                    {
                        UserInterface.DisplayMessageBox(this,
                                                        "Errors occured, check the Errors.txt file",
                                                        MessageBoxIcon.Exclamation);
                    }
                }
                catch (Exception ex)
                {
                    UserInterface.DisplayMessageBox(this,
                                                    "Errors occured, check the Errors.txt file",
                                                     MessageBoxIcon.Exclamation);
                    IO.WriteTextToFile("Acknowledgement Insert Error (" + DateTime.Now + "): " + ex.ToString() + Environment.NewLine,
                                       System.IO.Path.Combine(Misc.GetUserDataDirectory(), "Errors.txt"),
                                       true);
                }
                finally
                {
                    this.DialogResult = DialogResult.OK;
                }

            })).Start();
        }
Exemple #8
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="acknowledgementClass"></param>
        private void SetAcknowledgement(string ackClass)
        {
            var events = listEvents.SelectedObjects.Cast<Event>().ToList();

            if (cboRule.SelectedIndex == -1)
            {
                return;
            }

            Signature rule = (Signature)cboRule.Items[cboRule.SelectedIndex];

            var acknowledgementClass = (from a in _acknowledgmentClasses where a.Desc.ToLower() == ackClass.ToLower() select a).SingleOrDefault();
            if (acknowledgementClass == null)
            {
                UserInterface.DisplayMessageBox(this, "Cannot locate acknowledgement class", MessageBoxIcon.Exclamation);
                return;
            }

            (new Thread(() =>
            {
                try
                {
                    bool errors = false;
                    bool acknowledgedPrevious = false;
                    using (new HourGlass(this))
                    using (NPoco.Database db = new NPoco.Database(Db.GetOpenMySqlConnection()))
                    {
                        db.BeginTransaction();
                        foreach (Event temp in events)
                        {
                            try
                            {
                                bool insert = true;
                                var ack = db.Fetch<Acknowledgment>("select * from acknowledgment where cid=@0 and sid=@1", new object[] { temp.Cid, temp.Sid });
                                if (ack.Count() > 0)
                                {
                                    if (ack.First().Initials != _initials)
                                    {
                                        acknowledgedPrevious = true;
                                        insert = false;
                                    }
                                    else
                                    {
                                        db.Delete(ack.First());
                                    }
                                }

                                if (insert == true)
                                {
                                    Acknowledgment acknowledgment = new Acknowledgment();
                                    acknowledgment.Cid = temp.Cid;
                                    acknowledgment.Sid = temp.Sid;
                                    acknowledgment.Initials = _initials;
                                    acknowledgment.Notes = string.Empty;
                                    acknowledgment.Class = acknowledgementClass.Id;
                                    acknowledgment.Timestamp = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                                    db.Insert(acknowledgment);
                                }
                            }
                            catch (Exception ex)
                            {
                                db.AbortTransaction();
                                errors = true;
                                IO.WriteTextToFile("Acknowledgement Insert Error: " + ex.ToString() + Environment.NewLine,
                                                   System.IO.Path.Combine(Misc.GetUserDataDirectory(), "Errors.txt"),
                                                   true);
                                break;
                            }
                        }

                        if (errors == false)
                        {
                            db.CompleteTransaction();
                        }
                    }

                    if (acknowledgedPrevious == true)
                    {
                        UserInterface.DisplayMessageBox(this,
                                                        "Some events were not classified due to being already classified",
                                                        MessageBoxIcon.Exclamation);
                    }

                    if (errors == true)
                    {
                        UserInterface.DisplayMessageBox(this,
                                                        "Errors occured, check the Errors.txt file",
                                                        MessageBoxIcon.Exclamation);
                    }

                    LoadRuleEvents(_currentPage);
                }
                catch (Exception ex)
                {
                    UserInterface.DisplayMessageBox(this,
                                                    "Errors occured, check the Errors.txt file",
                                                     MessageBoxIcon.Exclamation);
                    IO.WriteTextToFile("Acknowledgement Insert Error (" + DateTime.Now + "): " + ex.ToString() + Environment.NewLine,
                                       System.IO.Path.Combine(Misc.GetUserDataDirectory(), "Errors.txt"),
                                       true);
                }

            })).Start();
        }
Exemple #9
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listEvents_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                ctxMenuPayload_Click(this, new EventArgs());
            }
            else if (e.KeyCode == Keys.F1)
            {
                if (_initials.Length == 0)
                {
                    UserInterface.DisplayMessageBox(this,
                                                    "The user initials have not been set. Manually classify event to set",
                                                    MessageBoxIcon.Information);
                    return;
                }

                var events = listEvents.Objects.Cast<Event>().ToList();

                (new Thread(() =>
                {
                    SetProcessingStatus(false);

                    bool acknowledgedPrevious = false;
                    using (new HourGlass(this))
                    using (NPoco.Database db = new NPoco.Database(Db.GetOpenMySqlConnection()))
                    {
                        foreach (Event temp in events)
                        {
                            bool insert = true;
                            var ack = db.Fetch<Acknowledgment>("select * from acknowledgment where cid=@0 and sid=@1", new object[] { temp.Cid, temp.Sid });
                            if (ack.Count() > 0)
                            {
                                if (ack.First().Initials != _initials)
                                {
                                    acknowledgedPrevious = true;
                                    insert = false;
                                }
                                else
                                {
                                    db.Delete(ack.First());
                                }
                            }

                            if (insert == true)
                            {
                                Acknowledgment acknowledgment = new Acknowledgment();
                                acknowledgment.Cid = temp.Cid;
                                acknowledgment.Sid = temp.Sid;
                                acknowledgment.Initials = _initials;
                                acknowledgment.Class = 1;

                                db.Insert(acknowledgment);
                            }
                        }
                    }

                    if (acknowledgedPrevious == true)
                    {
                        UserInterface.DisplayMessageBox(this,
                                                        "Some events were not classified due to being already classified",
                                                        MessageBoxIcon.Exclamation);
                    }

                    SetProcessingStatus(true);
                    LoadRuleEvents(_currentPage);

                })).Start();
            }
        }
 //----------------------------------------------------------------------------------------------------------------
 public void SaveBreachedEmailToDatabase()
 {
     db.Insert <Breached_Emails>(this);
 }
Exemple #11
0
 public void Insert <T>([JetBrains.Annotations.NotNull] T obj)
 {
     lock (Dblock) {
         _database.Insert(obj);
     }
 }
Exemple #12
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="packet"></param>
        private void WriteOldSessions(PcapDotNet.Packets.Packet packet)
        {
            try
            {
                OnMessage("Writing session data to database..." + _packetCount + " packets");
                using (DbConnection dbConnection = Db.GetOpenConnection(_outputPath))
                using (var db = new NPoco.Database(dbConnection, NPoco.DatabaseType.SQLCe))
                {
                    var keys = _dictionary.Keys.ToList();
                    foreach (var connection in keys)
                    {
                        var temp = _dictionary[connection];

                        if (temp.TimestampLastPacket == null)
                        {
                            if (temp.DataSize == 0)
                            {
                                _dictionary[connection].Dispose();
                                _dictionary.Remove(connection);
                            }
                            continue;
                        }

                        if (temp.HasFin == false)
                        {
                            if (packet != null)
                            {
                                // Lets ignore sessions that are still within the threshold
                                if (packet.Timestamp < temp.TimestampLastPacket.Value.AddMinutes(SessionInterval))
                                {
                                    continue;
                                }
                            }
                        }
                        else
                        {
                            if (packet != null)
                            {
                                // Only kill sessions that have had a FIN and at least a minute has past
                                if (packet.Timestamp < temp.TimestampLastPacket.Value.AddMinutes(1))
                                {
                                    continue;
                                }
                            }
                        }

                        Session session = CreateNewSession(temp.Guid,
                                                           temp.DataSize,
                                                           connection);

                        _dictionary[connection].Dispose();
                        if (_dictionary.Remove(connection) == false)
                        {
                            Console.WriteLine("Unable to remove connection object: " + connection.GetName());
                        }

                        if (temp.DataSize > 0)
                        {
                            int pk = Convert.ToInt32(db.Insert("Sessions", "Id", session));
                            PerformSessionProcessing(session, pk);
                        }
                    }

                    OnMessage("Commiting database transaction..." + _packetCount + " packets");
                }
            }
            catch (Exception ex)
            {
                this.Log().Error(ex.ToString());
            }
            finally
            {
                OnMessage(string.Empty);
            }
        }
Exemple #13
0
        /// <summary>
        ///
        /// </summary>
        private void Process()
        {
            AcknowledgmentClass acknowledgmentClass = (AcknowledgmentClass)cboClassification.Items[cboClassification.SelectedIndex];
            string initials   = txtInitials.Text.ToUpper();
            string notes      = txtNotes.Text;
            bool   successful = chkSuccessful.Checked;

            btnOk.Enabled     = false;
            btnCancel.Enabled = false;

            (new Thread(() =>
            {
                try
                {
                    bool acknowledgedPrevious = false;
                    bool errors = false;
                    using (new HourGlass(this))
                        using (NPoco.Database db = new NPoco.Database(Db.GetOpenMySqlConnection()))
                        {
                            db.BeginTransaction();
                            foreach (Event temp in _events)
                            {
                                try
                                {
                                    bool insert = true;
                                    var ack = db.Fetch <Acknowledgment>("select * from acknowledgment where cid=@0 and sid=@1", new object[] { temp.Cid, temp.Sid });
                                    if (ack.Count() > 0)
                                    {
                                        if (ack.First().Initials.ToUpper() != initials)
                                        {
                                            acknowledgedPrevious = true;
                                            insert = false;
                                        }
                                        else
                                        {
                                            db.Delete(ack.First());
                                        }
                                    }

                                    if (insert == true)
                                    {
                                        Acknowledgment acknowledgment = new Acknowledgment();
                                        acknowledgment.Cid = temp.Cid;
                                        acknowledgment.Sid = temp.Sid;
                                        acknowledgment.Initials = initials;
                                        acknowledgment.Notes = notes;
                                        acknowledgment.Class = acknowledgmentClass.Id;
                                        acknowledgment.Successful = successful;
                                        acknowledgment.Timestamp = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                                        db.Insert(acknowledgment);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    db.AbortTransaction();
                                    errors = true;
                                    IO.WriteTextToFile("Acknowledgement Insert Error (" + DateTime.Now + "): " + ex.ToString() + Environment.NewLine,
                                                       System.IO.Path.Combine(Misc.GetUserDataDirectory(), "Errors.txt"),
                                                       true);
                                    break;
                                }
                            }

                            if (errors == false)
                            {
                                db.CompleteTransaction();
                            }
                        }

                    if (acknowledgedPrevious == true)
                    {
                        UserInterface.DisplayMessageBox(this,
                                                        "Some events were not classified due to being already classified",
                                                        MessageBoxIcon.Exclamation);
                    }

                    if (errors == true)
                    {
                        UserInterface.DisplayMessageBox(this,
                                                        "Errors occured, check the Errors.txt file",
                                                        MessageBoxIcon.Exclamation);
                    }
                }
                catch (Exception ex)
                {
                    UserInterface.DisplayMessageBox(this,
                                                    "Errors occured, check the Errors.txt file",
                                                    MessageBoxIcon.Exclamation);
                    IO.WriteTextToFile("Acknowledgement Insert Error (" + DateTime.Now + "): " + ex.ToString() + Environment.NewLine,
                                       System.IO.Path.Combine(Misc.GetUserDataDirectory(), "Errors.txt"),
                                       true);
                }
                finally
                {
                    this.DialogResult = DialogResult.OK;
                }
            })).Start();
        }
Exemple #14
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                using (NPoco.Database db = new NPoco.Database(Db.GetOpenMySqlConnection()))
                {
                    Exclude exclude = new Exclude();

                    if (chkRule.Checked == true)
                    {
                        exclude.SigId = _ruleId;
                    }

                    if (chkSourceIp.Checked == true)
                    {
                        byte[] ip = _sourceIp.GetAddressBytes();
                        Array.Reverse(ip);
                        exclude.SourceIp = BitConverter.ToUInt32(ip, 0);
                    }

                    if (chkSrcPort.Checked == true)
                    {
                        exclude.SourcePort = ushort.Parse(txtSourcePort.Text);
                    }

                    if (chkDestinationIp.Checked == true)
                    {
                        byte[] ip = _destinationIp.GetAddressBytes();
                        Array.Reverse(ip);
                        exclude.DestinationIp = BitConverter.ToUInt32(ip, 0);
                    }

                    if (chkDestPort.Checked == true)
                    {
                        exclude.DestinationPort = ushort.Parse(txtDestinationPort.Text);
                    }

                    exclude.IpProto = _ipProto;
                    exclude.Comment = txtComment.Text;
                    exclude.FalsePositive = chkFalsePositive.Checked;
                    exclude.Timestamp = DateTime.Now;

                    db.Insert(exclude);
                }
            }
            catch (Exception ex)
            {
                UserInterface.DisplayErrorMessageBox("An error occurred whilst adding the exclude: " + ex.Message);
                return;
            }

            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }