sql_injection_detection 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b96350f2386f65f82461aeaa07b4e5869e38080ef26c1efeef6020b57594e5dd
4
+ data.tar.gz: 61903b6d203667ad906089fde544c20fe8456be90ae845c18b1d26976fcc80dd
5
+ SHA512:
6
+ metadata.gz: 4b98339bc2daf3ee9fc0da4d8d3316055a3a3d21477c42819cc9ed06de125510f2209ad3239da26d19320b54d37f0d91226cf404e53972a63c6f521ca5dccb1f
7
+ data.tar.gz: 0f84b22512656e042c1610aa3a13c19d45cfd38d5da55a738e1243634bacabebb026e555df41a6666b95c412c56e2e722053d938364059cceb23d0b2b2bcb045
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,8 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.0
3
+
4
+ Style/StringLiterals:
5
+ EnforcedStyle: double_quotes
6
+
7
+ Style/StringLiteralsInInterpolation:
8
+ EnforcedStyle: double_quotes
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,71 @@
1
+ # SQL Injection Detection Gem
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/sql_injection_detection.svg)](https://badge.fury.io/rb/sql_injection_detection)
4
+ [![Build Status](https://travis-ci.com/JakubSchwenkbeck/SQL-Injection-Detector-GEM.svg?branch=main)](https://travis-ci.com/JakubSchwenkbeck/SQL-Injection-Detector-GEM)
5
+
6
+ ## Overview
7
+
8
+ The `sql_injection_detection` gem is a lightweight Ruby library designed to detect potential SQL injection attempts in input strings. It can be used to enhance the security of Ruby on Rails applications or any Ruby-based projects by identifying and flagging suspicious input patterns that are commonly used in SQL injection attacks.
9
+
10
+ ## Features
11
+
12
+ - Detects common SQL injection patterns, such as:
13
+ - Single quotes and encoded characters
14
+ - SQL comments (`--`)
15
+ - SQL keywords like `UNION`, `SELECT`, `DROP`, `INSERT`, and `DELETE`
16
+ - Boolean-based injections using `OR` and `AND`
17
+ - Simple API: one method call to check for SQL injection
18
+ - Lightweight and easy to integrate into existing projects
19
+
20
+ ## Installation
21
+
22
+ Add this line to your application's Gemfile:
23
+
24
+ ```ruby
25
+ gem 'sql_injection_detection'
26
+ ```
27
+ And then execute:
28
+
29
+ ```bash
30
+ bundle install
31
+ ```
32
+ Or install it yourself as:
33
+
34
+ ```bash
35
+ gem install sql_injection_detection
36
+ ```
37
+ ## Usage
38
+ You can use the gem by requiring it in your project and calling the SqlInjectionDetection::Checker.check method with the input string you want to validate:
39
+
40
+ ```ruby
41
+ require 'sql_injection_detection'
42
+
43
+ input = "' OR 1=1 --"
44
+ if SqlInjectionDetection::Checker.check(input)
45
+ puts "Potential SQL injection detected!"
46
+ else
47
+ puts "Input is safe."
48
+ end
49
+ ```
50
+ ### Example Usage
51
+ ```ruby
52
+ input = "SELECT * FROM users WHERE username = 'admin' --"
53
+ if SqlInjectionDetection::Checker.check(input)
54
+ puts "Warning: SQL Injection attempt detected!"
55
+ else
56
+ puts "Input is safe."
57
+ end
58
+ ```
59
+ ## Development
60
+ After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
61
+
62
+ To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the tag, and push the .gem file to rubygems.org.
63
+
64
+ ## Contributing
65
+ Bug reports and pull requests are welcome on GitHub at https://github.com/JakubSchwenkbeck/SQL-Injection-Detector-GEM. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
66
+
67
+ ## License
68
+ The gem is available as open source under the terms of the MIT License.
69
+
70
+ ## Acknowledgements
71
+ This project was created to provide a simple yet effective tool for detecting SQL injection vulnerabilities in Ruby applications. Contributions and feedback are greatly appreciated!
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SqlInjectionDetection
4
+ VERSION = "1.0.0"
5
+ end
@@ -0,0 +1,130 @@
1
+ module SqlInjectionDetection
2
+ class Checker
3
+ # Combined regex patterns to detect a wide range of SQL injection techniques
4
+ PATTERNS = [
5
+ # Detects single quotes or encoded single quotes, often used to escape SQL strings
6
+ /(%27)|(')/i,
7
+
8
+ # Detects SQL comments (inline comments using --) and block comments (/* */)
9
+ /(--|\/\*)/i,
10
+
11
+ # Detects encoded characters for inline comments (e.g., %23 for #)
12
+ /(%23|%2F%2A)/i,
13
+
14
+ # Detects boolean-based SQL injections using OR/AND with numbers, strings, NULL, or boolean values
15
+ /\b(OR|AND)\b\s+(\d+|'[^']*'|NULL|TRUE|FALSE)/i,
16
+
17
+ # Detects UNION and SELECT keywords, often used to combine results from multiple SELECT statements
18
+ /\b(UNION|SELECT)\b/i,
19
+
20
+ # Detects the usage of semicolons, used to terminate SQL statements and inject new ones
21
+ /(;)/,
22
+
23
+ # Detects common SQL keywords for data manipulation and structure changes
24
+ # DROP: Used to drop tables or databases
25
+ # INSERT: Used to inject data into tables
26
+ # DELETE: Used to remove data
27
+ # UPDATE: Used to modify data
28
+ /\b(DROP|INSERT|DELETE|UPDATE)\b/i,
29
+
30
+ # Detects potential SQL keywords with a high risk for code injection
31
+ # EXEC: Execute a stored procedure or dynamic SQL
32
+ # EXECUTE: Same as EXEC
33
+ /\b(EXEC|EXECUTE)\b/i,
34
+
35
+ # Detects SQL wildcards and patterns in LIKE-based attacks
36
+ # LIKE: Used with wildcards to match patterns in SQL
37
+ /\bLIKE\b\s+('|")[^'"]+('|")/i,
38
+
39
+ # Detects null-byte injections, used in various bypass techniques
40
+ /\x00/i,
41
+
42
+ # Detects SQL injections that use concatenation to bypass filters
43
+ # ||: SQL concatenation operator
44
+ /\|\|/,
45
+
46
+ # Detects encoded and decoded SQL keywords using hexadecimal or binary representations
47
+ # e.g., 0x53514c20494e4a454354494f4e represents "SQL INJECTION"
48
+ /0x[0-9A-Fa-f]+/,
49
+
50
+ # Detects encoded comments using the %2D (dash) and %2F (slash) patterns
51
+ /(%2D){2}|(%2F%2A)/i,
52
+
53
+ # Detects SQL keywords that interact with metadata, which could be exploited
54
+ # INFORMATION_SCHEMA: A schema containing information about database objects
55
+ # SYSOBJECTS: A system table containing information about objects within the database
56
+ /\b(INFORMATION_SCHEMA|SYSOBJECTS)\b/i,
57
+
58
+ # Detects blind SQL injection techniques, which rely on timing to infer database information
59
+ # WAITFOR: SQL Server keyword used to delay execution
60
+ # BENCHMARK: MySQL function to run a specified number of loops
61
+ /\b(WAITFOR|BENCHMARK)\b\s+/i,
62
+
63
+ # Detects time-based SQL injection techniques, commonly used to exploit delays
64
+ # SLEEP: MySQL function to delay execution
65
+ # PG_SLEEP: PostgreSQL function to delay execution
66
+ /\b(SLEEP|PG_SLEEP)\b\s*\(\s*\d+\s*\)/i,
67
+
68
+ # Detects SQL Server specific injections for stacked queries
69
+ # XP_CMDSHELL: Executes command shell commands
70
+ /\bXP_CMDSHELL\b/i,
71
+
72
+ # Detects MySQL-specific injections for loading external files
73
+ # LOAD_FILE: Loads a file into a string in MySQL
74
+ /\bLOAD_FILE\b\s*\(/i,
75
+
76
+ # Detects SQL Server injection keywords for accessing system procedures
77
+ # sp_: Stored procedure prefix in SQL Server
78
+ /\bsp_[a-zA-Z0-9_]+\b/i,
79
+
80
+ # Detects SQL injection attempts using the ALTER keyword to change database structure
81
+ /\bALTER\b/i,
82
+
83
+ # Detects usage of subqueries which could be exploited in certain contexts
84
+ /\b(SELECT\s+\*\s+FROM)\b/i,
85
+
86
+ # Detects complex UNION SELECT attempts that combine multiple SELECTs
87
+ /UNION\s+(ALL\s+)?SELECT\s+/i,
88
+
89
+ # Detects SQL keywords that could be used to read from or write to files in the database
90
+ # OUTFILE: Writes query results to a file in MySQL
91
+ # INTO OUTFILE: Writes to a file in SQL
92
+ /\b(INTO\s+OUTFILE|OUTFILE)\b/i,
93
+
94
+ # Detects SQL functions that could be used to bypass typical sanitization checks
95
+ # ASCII: Returns ASCII value of the first character
96
+ # CHAR: Returns the character for each integer passed
97
+ /\b(ASCII|CHAR)\b\s*\(\s*[^\)]*\s*\)/i,
98
+
99
+ # Detects SQL injection techniques that attempt to execute multiple queries
100
+ # Semicolon (;) to chain queries together
101
+ /\;/,
102
+
103
+ # Detects SQL injection techniques that make use of the CONCAT function to merge strings
104
+ /\bCONCAT\b\s*\(/i,
105
+
106
+ # Detects SQL keywords related to database schema manipulation that might be dangerous
107
+ # CREATE: Used to create new tables, databases, or users
108
+ # ALTER: Used to change database structures
109
+ /\b(CREATE|ALTER)\b/i,
110
+
111
+ # Detects SQL injection techniques that rely on the RAND function to bypass unique constraints
112
+ /\bRAND\b\s*\(\s*\)/i,
113
+
114
+ # Detects SQL injection techniques that use mathematical functions to manipulate results
115
+ # SQRT: Returns the square root of a number
116
+ /\bSQRT\b\s*\(\s*\d+\s*\)/i,
117
+
118
+ # Detects SQL injection techniques that use encryption functions to manipulate queries
119
+ # AES_ENCRYPT: Encrypts data using AES
120
+ /\bAES_ENCRYPT\b\s*\(\s*[^\)]*\s*\)/i
121
+ ]
122
+
123
+ # Method to check if the input string contains SQL injection patterns
124
+ def self.check(input)
125
+ return false if input.nil? || input.strip.empty?
126
+
127
+ PATTERNS.any? { |pattern| pattern.match?(input) }
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,4 @@
1
+ module SqlInjectionDetection
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sql_injection_detection
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jakub Schwenkbeck
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-08-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: The sql_injection_detection gem provides a basic yet effective way to
28
+ detect common SQL injection attempts by matching input strings against known malicious
29
+ patterns. It can be used in Ruby on Rails applications or any Ruby-based projects
30
+ to help prevent SQL injection vulnerabilities.
31
+ email:
32
+ - Jakub@Schwenkbeck.de
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - ".rspec"
38
+ - ".rubocop.yml"
39
+ - CODE_OF_CONDUCT.md
40
+ - LICENSE.txt
41
+ - README.md
42
+ - Rakefile
43
+ - lib/sql_injection_detection.rb
44
+ - lib/sql_injection_detection/version.rb
45
+ - sig/sql_injection_detection.rbs
46
+ homepage: https://github.com/JakubSchwenkbeck/SQL-Injection-Detector-GEM
47
+ licenses:
48
+ - MIT
49
+ metadata:
50
+ allowed_push_host: https://rubygems.org
51
+ homepage_uri: https://github.com/JakubSchwenkbeck/SQL-Injection-Detector-GEM
52
+ source_code_uri: https://github.com/JakubSchwenkbeck/SQL-Injection-Detector-GEM
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 3.0.0
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubygems_version: 3.5.11
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: A simple gem to detect potential SQL injection attacks in input strings.
72
+ test_files: []