简体   繁体   English

用于在WinForms中检测SQL注入的正则表达式

[英]Regex for detecting SQL Injections in WinForms

i uwant to cach input, which seems to be like SQL injection. 我想要cach输入,这似乎像SQL注入。 So I wrote the method: 所以我写了这个方法:

        public static bool IsInjection(string inputText)
    {


        bool isInj = false;


        string regexForTypicalInj = @"/\w*((\%27)|(\'))((\%6F)|o|(\%4F))((\%72)|r|(\%52))/ix";
        Regex reT = new Regex(regexForTypicalInj);
        if (reT.IsMatch(inputText))
            isInj = true;


        string regexForUnion = @"/((\%27)|(\'))union/ix";
        Regex reUn = new Regex(regexForUnion);
        if (reUn.IsMatch(inputText))
            isInj = true;



        string regexForSelect = @"/((\%27)|(\'))select/ix";
        Regex reS = new Regex(regexForSelect);
        if (reS.IsMatch(inputText))
            isInj = true;

        string regexForInsert = @"/((\%27)|(\'))insert/ix";
        Regex reI = new Regex(regexForInsert);
        if (reI.IsMatch(inputText))
            isInj = true;

        string regexForUpdate = @"/((\%27)|(\'))update/ix";
        Regex reU = new Regex(regexForUpdate);
        if (reU.IsMatch(inputText))
            isInj = true;

        string regexForDelete = @"/((\%27)|(\'))delete/ix";
        Regex reDel = new Regex(regexForDelete);
        if (reDel.IsMatch(inputText))
            isInj = true;

        string regexForDrop = @"/((\%27)|(\'))drop/ix";
        Regex reDr = new Regex(regexForDrop);
        if (reDr.IsMatch(inputText))
            isInj = true;

        string regexForAlter = @"/((\%27)|(\'))alter/ix";
        Regex reA = new Regex(regexForAlter);
        if (reA.IsMatch(inputText))
            isInj = true;

        string regexForCreate = @"/((\%27)|(\'))create/ix";
        Regex reC = new Regex(regexForCreate);
        if (reC.IsMatch(inputText))
            isInj = true;

        return isInj;

    }

But seems I have done some mistakes, becouse my code do not detects injections. 但似乎我做了一些错误,因为我的代码不检测注射。 What i do wrong? 我做错了什么? I guess theres something wrong in defining Regex expressions? 我想在定义Regex表达式时有问题吗?

Don't try to do this with RegEx - there are too many ways around it. 不要试图用RegEx做到这一点 - 有很多方法可以解决它。 See this classic SO answer about parsing with RegEx - it is specific to HTML, but still applies. 请参阅这个关于使用RegEx进行解析的经典SO答案 - 它特定于HTML,但仍然适用。

You should use Parameters , these are in the BCL and have anti SQL injection measures built in. 您应该使用参数 ,这些参数位于BCL中并且内置了反SQL注入措施。

Update: (following comments) 更新:(以下评论)

If you really must parse the SQL, do not use RegEx for the reasons outlined in the linked article. 如果您真的必须解析SQL,请不要使用RegEx,原因是链接文章中列出的原因。 RegEx is not a parser and should not be used as one. RegEx不是解析器,不应该用作一个解析器。

Use a SQL parser - this should help with sanitizing attempts. 使用SQL解析器 - 这应该有助于清理尝试。 Here is one , here another . 这是一个 ,这里是另一个

You can continue your scientific investigation with these. 您可以继续进行科学调查。

Do not use string parsing or regular expressions to handle this sort of thing. 不要使用字符串解析或正则表达式来处理这类事情。 SQL syntax is too complicated to reliably parse with regular expressions. SQL语法太复杂,无法使用正则表达式进行可靠的解析。

Instead use parametrized queries with placeholders and avoid string concatenation altogether. 而是使用带占位符的参数化查询,并完全避免字符串连接。 This will defeat SQL injection at its root. 这将在其根源上击败SQL注入。

var command = new SqlCommand(connection);
command.Text = "INSERT INTO foo (a, b, c) VALUES (@a, @b, @c)";
command.Parameters.AddWithValue("a", "this is invulnerable");
command.Parameters.AddWithValue("b", "to any sort of SQL injection");
command.Parameters.AddWithValue("c", "--'; DROP DATABASE");
command.ExecuteNonQuery();

If you really want to help out your "not so experienced programmers", you'd be better off trying to detect when they are doing inline sql in their code. 如果你真的想帮助你的“不那么有经验的程序员”,你最好不要试图检测他们在代码中执行内联sql的时间。 It shouldn't be too difficult to write an FxCop rule to spot it. 写一个FxCop规则来发现它应该不会太困难。 If you include it as part of a post build process, or if you have team system, set the rule to fail the build, they'll soon get the hang of it. 如果您将其作为后期构建过程的一部分包含在内,或者如果您有团队系统,则将规则设置为使构建失败,他们很快就会掌握它。

The problem with SQL injection is, that a user input is used as part of the SQL statement. SQL注入的问题是,用户输入用作SQL语句的一部分。 By using prepared statements you can force the user input to be handled as the content of a parameter (and not as a part of the SQL command). 通过使用预准备语句,您可以强制将用户输入作为参数的内容进行处理(而不是作为SQL命令的一部分)。 Query parameters help to avoid this risk by separating literal values from the SQL syntax. 查询参数通过将文字值与SQL语法分开来帮助避免此风险。

Most client APIs (including .NET) support parameterization of queries. 大多数客户端API(包括.NET)都支持查询的参数化。 This allows embedding the user input as parameters. 这允许将用户输入嵌入为参数。 The parameters are placeholders for user entered value which is replaced at execution time. 参数是用户输入值的占位符,在执行时替换。 That way the user cannot inject SQL code as the whole user entry is treated as value for the parameter, not as string appended to the query. 这样,用户无法注入SQL代码,因为整个用户条目被视为参数的值,而不是作为追加到查询的字符串。

parameterization is the best solution for SQL injection attacks. 参数化是SQL注入攻击的最佳解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM