Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. SQL Injection Detection

SQL Injection Detection

Scheduled Pinned Locked Moved C#
csharppythondatabasevisual-studiolinq
2 Posts 2 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member 8054321
    wrote on last edited by
    #1

    Please help make this code work. I'm trying to run it in Visual Studio 2022. Thanks.

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using Numpy;
    using Python.Runtime;
    using Keras;
    using Keras.Layers;
    using Keras.Models;
    using Keras.Optimizers;
    using Keras.losses;

    namespace SQLInjectionDetection
    {
    class Program
    {
    static void Main(string[] args)
    {
    // Load CSV file
    var trainData = File.ReadAllLines("tokens.csv")
    .Select(l => l.Split(','))
    .Select(s => new { Token = s[0], Label = int.Parse(s[1]) })
    .ToList();

            // Shuffle trainData
            var random = new Random();
            trainData = trainData.OrderBy(d => random.Next()).ToList();
    
            // Split trainData into training and validation sets
            var splitIndex = (int)(trainData.Count \* 0.8);
            var trainDataSubset = trainData.Take(splitIndex).ToList();
            var testDataSubset = trainData.Skip(splitIndex).ToList();
    
            // Define vocabulary and tokenize trainData
            var vocabulary = new HashSet(trainDataSubset.SelectMany(d => d.Token).Distinct());
            var tokenToIndex = vocabulary.Select((c, i) => new { Token = c, Index = i }).ToDictionary(t => t.Token, t => t.Index);
            var maxSequenceLength = trainDataSubset.Max(d => d.Token.Length);
            var trainTokenized = Tokenize(trainDataSubset, tokenToIndex, maxSequenceLength);
            var testTokenized = Tokenize(testDataSubset, tokenToIndex, maxSequenceLength);
    
            // Build RNN model
            using (Py.GIL())
            {
                dynamic keras = Py.Import("keras");
                dynamic np = Py.Import("numpy");
                var input = new Input(shape: 1000);
                var embedding = new Embedding(vocabulary.Count, 32).Apply(input);
                var lstm = new LSTM(32).Apply(embedding);
                var output = new Dense(1, activation: keras.activations.sigmoid).Apply(lstm);
                var model = new Model(inputs: input, outputs: output);
                model.Compile(optimizer: new Adam(), loss: new BinaryCrossentropy(), metrics: new\[\] { "accuracy" });
    
                // Train model
                var trainX = trainTokenized.Item1;
                var trainY = trainTokenized.Item2;
                var testX = testTokenized.Item1;
    
    OriginalGriffO 1 Reply Last reply
    0
    • M Member 8054321

      Please help make this code work. I'm trying to run it in Visual Studio 2022. Thanks.

      using System;
      using System.Collections.Generic;
      using System.IO;
      using System.Linq;
      using Numpy;
      using Python.Runtime;
      using Keras;
      using Keras.Layers;
      using Keras.Models;
      using Keras.Optimizers;
      using Keras.losses;

      namespace SQLInjectionDetection
      {
      class Program
      {
      static void Main(string[] args)
      {
      // Load CSV file
      var trainData = File.ReadAllLines("tokens.csv")
      .Select(l => l.Split(','))
      .Select(s => new { Token = s[0], Label = int.Parse(s[1]) })
      .ToList();

              // Shuffle trainData
              var random = new Random();
              trainData = trainData.OrderBy(d => random.Next()).ToList();
      
              // Split trainData into training and validation sets
              var splitIndex = (int)(trainData.Count \* 0.8);
              var trainDataSubset = trainData.Take(splitIndex).ToList();
              var testDataSubset = trainData.Skip(splitIndex).ToList();
      
              // Define vocabulary and tokenize trainData
              var vocabulary = new HashSet(trainDataSubset.SelectMany(d => d.Token).Distinct());
              var tokenToIndex = vocabulary.Select((c, i) => new { Token = c, Index = i }).ToDictionary(t => t.Token, t => t.Index);
              var maxSequenceLength = trainDataSubset.Max(d => d.Token.Length);
              var trainTokenized = Tokenize(trainDataSubset, tokenToIndex, maxSequenceLength);
              var testTokenized = Tokenize(testDataSubset, tokenToIndex, maxSequenceLength);
      
              // Build RNN model
              using (Py.GIL())
              {
                  dynamic keras = Py.Import("keras");
                  dynamic np = Py.Import("numpy");
                  var input = new Input(shape: 1000);
                  var embedding = new Embedding(vocabulary.Count, 32).Apply(input);
                  var lstm = new LSTM(32).Apply(embedding);
                  var output = new Dense(1, activation: keras.activations.sigmoid).Apply(lstm);
                  var model = new Model(inputs: input, outputs: output);
                  model.Compile(optimizer: new Adam(), loss: new BinaryCrossentropy(), metrics: new\[\] { "accuracy" });
      
                  // Train model
                  var trainX = trainTokenized.Item1;
                  var trainY = trainTokenized.Item2;
                  var testX = testTokenized.Item1;
      
      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      We have no idea what it is fully supposed to do, or what it does that you didn't expect / doesn't do that you did. And we have no access to your data so we couldn't test it if we wanted to. So, it's going to be up to you. Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need. Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why. Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups