├── .github └── workflows │ ├── integration.yml │ └── lint.yml ├── CONTRIBUTORS.md ├── INSTALL ├── LICENSE ├── README.md ├── plugins ├── fake-bot-after.conf ├── fake-bot-before.conf ├── fake-bot-config.conf └── fake-bot.lua ├── renovate.json ├── tests └── regression │ └── fake-bot-plugin │ └── 9504120.yaml └── util └── fake-bot-report.sh /.github/workflows/integration.yml: -------------------------------------------------------------------------------- 1 | --- 2 | on: [push, pull_request] # yamllint disable-line rule:truthy 3 | name: Integration tests 4 | 5 | jobs: 6 | integration-tests: 7 | uses: coreruleset/crs-plugin-test-action/.github/workflows/integration.yaml@main 8 | -------------------------------------------------------------------------------- /.github/workflows/lint.yml: -------------------------------------------------------------------------------- 1 | --- 2 | on: [push, pull_request] # yamllint disable-line rule:truthy 3 | 4 | jobs: 5 | plugin-lint: 6 | uses: coreruleset/crs-plugin-test-action/.github/workflows/lint.yaml@main 7 | -------------------------------------------------------------------------------- /CONTRIBUTORS.md: -------------------------------------------------------------------------------- 1 | # Contributors to Fake Bot Plugin 2 | 3 | - [Christian Folini](https://github.com/dune73) 4 | - [Max Leske](https://github.com/theseion) 5 | - [Jozef Sudolský](https://github.com/azurit) 6 | -------------------------------------------------------------------------------- /INSTALL: -------------------------------------------------------------------------------- 1 | See README.md. 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2024 the OWASP CRS contributors 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OWASP CRS - Fake Bot Plugin 2 | 3 | ## Description 4 | 5 | This is a plugin that brings blocking of bots and impersonators faking well known 6 | User-Agents in their HTTP requests. 7 | 8 | The plugin is able to detect bots pretending to be: 9 | 10 | * Amazonbot 11 | * Applebot 12 | * Bingbot 13 | * Facebookbot 14 | * Googlebot 15 | * LinkedInBot 16 | * Twitterbot 17 | 18 | Detection is done using DNS PTR records. 19 | 20 | Upon successful detection, the requests will then be blocked by CRS depending on the CRS configuration. 21 | 22 | ## Prerequisities 23 | 24 | * ModSecurity compiled with Lua support 25 | * Lua 26 | * LuaSocket library 27 | * Fully working DNS resolving 28 | 29 | ## How to determine whether you have Lua support in ModSecurity 30 | 31 | Most modern distro packages come with Lua support compiled in. If you are unsure, or if you get odd error messages (e.g. `EOL found`) chances are you are unlucky. To be really sure look for ModSecurity announce Lua support when launching your web server: 32 | 33 | ``` 34 | ... ModSecurity for Apache/2.9.5 (http://www.modsecurity.org/) configured. 35 | ... ModSecurity: APR compiled version="1.7.0"; loaded version="1.7.0" 36 | ... ModSecurity: PCRE compiled version="8.39 "; loaded version="8.39 2016-06-14" 37 | ... ModSecurity: LUA compiled version="Lua 5.3" 38 | ... 39 | ``` 40 | 41 | If this line is missing, then you are probably stuck without Lua. Check out the documentation at [coreruleset.org](https://coreruleset.org/docs) to learn how to get Lua support for your installation. 42 | 43 | ## LuaSocket library installation 44 | 45 | LuaSocket library should be part of your linux distribution. Here is an example 46 | of installation on Debian / Ubuntu Linux: 47 | 48 | `apt install lua-socket` 49 | 50 | ## Plugin installation 51 | 52 | For full and up to date instructions for the different available plugin 53 | installation methods, refer to [How to Install a Plugin](https://coreruleset.org/docs/concepts/plugins/#how-to-install-a-plugin) 54 | in the official CRS documentation. 55 | 56 | ## Plugin configuration 57 | 58 | The settings for this plugin reside in `plugins/fake-bot-config.conf`. 59 | 60 | ### tx.fake-bot-plugin_whitelist_broken_apple_devices 61 | 62 | Some software used by Apple devices send requests pretending to be a Facebook or Twitter (X) bot. 63 | iMessage, for example, does this to obtain link previews. 64 | If you want to allow this behavior and not block such requests, set this variable to `1`. 65 | 66 | Note: This setting, when enabled, opens a hole in the fake bot detection, 67 | which can be used by fake bots to bypass the protection provided by this plugin. 68 | 69 | Default value: 0 70 | 71 | ## Testing 72 | 73 | After installation, plugin should be tested, for example, using: 74 | `curl http://localhost --header "User-Agent: Googlebot"` 75 | 76 | Using default CRS configuration, this request should end with status 403 with 77 | the following message in the log: 78 | 79 | `ModSecurity: Warning. Fake Bot Plugin: Detected fake Googlebot. [file "/path/plugins/fake-bot-after.conf"] [line "27"] [id "9504110"] [msg "Fake bot detected: Googlebot"] [data "Matched Data: googlebot found within REQUEST_HEADERS:User-Agent: Googlebot"] [severity "CRITICAL"] [ver "fake-bot-plugin/1.0.0"] [tag "application-multi"] [tag "language-multi"] [tag "platform-multi"] [tag "attack-bot"] [tag "capec/1000/225/22/77/13"] [tag "PCI/6.5.10"] [tag "paranoia-level/1"] [hostname "localhost"] [uri "/"] [unique_id "YebRag1XU2Ir-Zmt0Zlo2wAAAAA"]` 80 | 81 | If you are running with a higher [Anomaly Threshold](https://coreruleset.org/docs/concepts/paranoia_levels/#how-paranoia-levels-relate-to-anomaly-scoring), you probably won't be blocked, but the alert message will still be there. 82 | 83 | ## Reporting 84 | 85 | Please find a script named `fake-bot-report.sh` in the util folder. 86 | 87 | ## License 88 | 89 | Copyright (c) 2022-2025 OWASP CRS project. All rights reserved. 90 | 91 | The OWASP CRS and its official plugins are distributed 92 | under Apache Software License (ASL) version 2. Please see the enclosed LICENSE 93 | file for full details. 94 | -------------------------------------------------------------------------------- /plugins/fake-bot-after.conf: -------------------------------------------------------------------------------- 1 | # ------------------------------------------------------------------------ 2 | # OWASP CRS Plugin 3 | # Copyright (c) 2022-2025 CRS project. All rights reserved. 4 | # 5 | # The OWASP CRS plugins are distributed under 6 | # Apache Software License (ASL) version 2 7 | # Please see the enclosed LICENSE file for full details. 8 | # ------------------------------------------------------------------------ 9 | 10 | # OWASP CRS Plugin 11 | # Plugin name: fake-bot 12 | # Plugin description: 13 | # Rule ID block base: 9,504,000 - 9,504,999 14 | # Plugin version: 1.0.0 15 | 16 | # Documentation can be found here: 17 | # https://github.com/coreruleset/fake-bot-plugin 18 | 19 | SecRule TX:FAKE-BOT-PLUGIN_WHITELIST_BROKEN_APPLE_DEVICES "@streq 1" \ 20 | "id:9504110,\ 21 | phase:1,\ 22 | pass,\ 23 | t:none,\ 24 | msg:'Fake Bot Plugin: Broken Apple device detected from %{REMOTE_ADDR} - whitelisting',\ 25 | logdata:'Matched Data: REQUEST_HEADERS:User-Agent: %{REQUEST_HEADERS.User-Agent}',\ 26 | tag:'paranoia-level/1',\ 27 | ver:'fake-bot-plugin/1.0.0',\ 28 | skip:1,\ 29 | chain" 30 | SecRule REQUEST_HEADERS:User-Agent "@endsWith facebookexternalhit/1.1 Facebot Twitterbot/1.0" 31 | 32 | SecRule REQUEST_HEADERS:User-Agent "@pm amazonbot applebot bingbot linkedinbot facebookbot facebookcatalog googlebot twitterbot" \ 33 | "id:9504120,\ 34 | phase:1,\ 35 | block,\ 36 | capture,\ 37 | t:none,\ 38 | msg:'Fake Bot Plugin: Detected fake %{tx.fake-bot-plugin_bot_name}.',\ 39 | logdata:'Matched Data: %{TX.0} found within REQUEST_HEADERS:User-Agent: %{REQUEST_HEADERS.User-Agent}',\ 40 | tag:'application-multi',\ 41 | tag:'language-multi',\ 42 | tag:'platform-multi',\ 43 | tag:'attack-bot',\ 44 | tag:'capec/1000/225/22/77/13',\ 45 | tag:'PCI/6.5.10',\ 46 | tag:'paranoia-level/1',\ 47 | ver:'fake-bot-plugin/1.0.0',\ 48 | severity:'CRITICAL',\ 49 | chain" 50 | SecRule TX:0 "@inspectFile fake-bot.lua" \ 51 | "t:none,\ 52 | setvar:'tx.anomaly_score_pl1=+%{tx.critical_anomaly_score}',\ 53 | setvar:'tx.inbound_anomaly_score_pl1=+%{tx.critical_anomaly_score}'" 54 | -------------------------------------------------------------------------------- /plugins/fake-bot-before.conf: -------------------------------------------------------------------------------- 1 | # ------------------------------------------------------------------------ 2 | # OWASP CRS Plugin 3 | # Copyright (c) 2022-2025 CRS project. All rights reserved. 4 | # 5 | # The OWASP CRS plugins are distributed under 6 | # Apache Software License (ASL) version 2 7 | # Please see the enclosed LICENSE file for full details. 8 | # ------------------------------------------------------------------------ 9 | 10 | # OWASP CRS Plugin 11 | # Plugin name: fake-bot 12 | # Plugin description: 13 | # Rule ID block base: 9,504,000 - 9,504,999 14 | # Plugin version: 1.0.0 15 | 16 | # Documentation can be found here: 17 | # https://github.com/coreruleset/fake-bot-plugin 18 | 19 | # Generic rule to disable plugin 20 | SecRule TX:fake-bot-plugin_enabled "@eq 0" "id:9504099,phase:1,pass,nolog,ctl:ruleRemoveById=9504100-9504999" 21 | -------------------------------------------------------------------------------- /plugins/fake-bot-config.conf: -------------------------------------------------------------------------------- 1 | # ------------------------------------------------------------------------ 2 | # OWASP CRS Plugin 3 | # Copyright (c) 2022-2025 CRS project. All rights reserved. 4 | # 5 | # The OWASP CRS plugins are distributed under 6 | # Apache Software License (ASL) version 2 7 | # Please see the enclosed LICENSE file for full details. 8 | # ------------------------------------------------------------------------ 9 | 10 | # OWASP CRS Plugin 11 | # Plugin name: fake-bot 12 | # Plugin description: 13 | # Rule ID block base: 9,504,000 - 9,504,999 14 | # Plugin version: 1.0.0 15 | 16 | # Documentation can be found here: 17 | # https://github.com/coreruleset/fake-bot-plugin 18 | 19 | # Generic rule to disable the plugin 20 | # 21 | # Plugins are enabled by default. 22 | # 23 | # They become active by placing them in the plugin folder. It is possible to 24 | # control plugin activation via setting a variable. This can be done in the 25 | # plugin config file here. 26 | # 27 | # The predefined variable name is meant to be "-plugin_enabled". 28 | # For the fake-bot-plugin, this means it can be disabled by setting 29 | # tx.fake-bot-plugin_enabled=0. 30 | # 31 | # Note that a global setting of this variable overrides the setting here. 32 | # That means the "enabled" variable is only set by this rule if it has not 33 | # been set before. 34 | # 35 | # Feel free to set the variable unconditionally here by replacing the 36 | # SecRule line with an unconditional SecAction statement. 37 | # 38 | #SecRule &TX:fake-bot-plugin_enabled "@eq 0" \ 39 | # "id:9504010,\ 40 | # phase:1,\ 41 | # pass,\ 42 | # nolog,\ 43 | # setvar:'tx.fake-bot-plugin_enabled=0'" 44 | 45 | SecAction \ 46 | "id:9504020,\ 47 | phase:1,\ 48 | nolog,\ 49 | pass,\ 50 | t:none,\ 51 | ver:'fake-bot-plugin/1.0.0',\ 52 | setvar:'tx.fake-bot-plugin_whitelist_broken_apple_devices=0'" 53 | -------------------------------------------------------------------------------- /plugins/fake-bot.lua: -------------------------------------------------------------------------------- 1 | -- ----------------------------------------------------------------------- 2 | -- OWASP CRS Plugin 3 | -- Copyright (c) 2022-2025 CRS project. All rights reserved. 4 | -- 5 | -- The OWASP CRS plugins are distributed under 6 | -- Apache Software License (ASL) version 2 7 | -- Please see the enclosed LICENSE file for full details. 8 | -- ----------------------------------------------------------------------- 9 | 10 | -- Code inspired by http://lua-users.org/wiki/StringRecipes . 11 | function ends_with(str, ending) 12 | str = str:lower() 13 | ending = ending:lower() 14 | return ending == "" or str:sub(-#ending) == ending 15 | end 16 | 17 | function check_ip(ip) 18 | if type(ip) ~= "string" then 19 | return false 20 | end 21 | -- IPv4 22 | local chunks = {ip:match("^(%d+)%.(%d+)%.(%d+)%.(%d+)$")} 23 | if #chunks == 4 then 24 | for _, chunk in pairs(chunks) do 25 | if tonumber(chunk) > 255 then 26 | return false 27 | end 28 | end 29 | return true 30 | end 31 | -- IPv6 32 | local chunks = {ip:match("^"..(("([a-fA-F0-9]*):"):rep(8):gsub(":$", "$")))} 33 | if #chunks == 8 or (#chunks < 8 and ip:match("::") and not ip:gsub("::", "", 1):match("::")) then 34 | for _, chunk in pairs(chunks) do 35 | if #chunk > 0 and tonumber(chunk, 16) > 65535 then 36 | return false 37 | end 38 | end 39 | return true 40 | end 41 | return false 42 | end 43 | 44 | function main(matched_bot) 45 | pcall(require, "m") 46 | local ok, socket = pcall(require, "socket") 47 | if not ok then 48 | m.log(2, "Fake Bot Plugin ERROR: LuaSocket library not installed, please install it or disable this plugin.") 49 | return nil 50 | end 51 | if matched_bot == "googlebot" then 52 | -- https://developers.google.com/search/docs/advanced/crawling/verifying-googlebot 53 | bot_domains = {".googlebot.com", ".google.com"} 54 | bot_name = "Googlebot" 55 | -- We can no longer support 'facebookexternalhit' UA string as Facebook started to use IP addresses without reverse record in DNS. 56 | elseif matched_bot == "facebookcatalog" or matched_bot == "facebookbot" then 57 | -- https://developers.facebook.com/docs/sharing/webmasters/web-crawlers 58 | bot_domains = {".facebook.com", ".fbsv.net"} 59 | bot_name = "Facebookbot" 60 | elseif matched_bot == "bingbot" then 61 | -- https://blogs.bing.com/webmaster/2012/08/31/how-to-verify-that-bingbot-is-bingbot 62 | bot_domains = {".search.msn.com"} 63 | bot_name = "Bingbot" 64 | elseif matched_bot == "twitterbot" then 65 | -- https://developer.twitter.com/en/docs/twitter-for-websites/cards/guides/troubleshooting-cards#validate_twitterbot 66 | bot_domains = {".twitter.com", ".twttr.com"} 67 | bot_name = "Twitterbot" 68 | elseif matched_bot == "applebot" then 69 | -- https://support.apple.com/en-us/HT204683 70 | bot_domains = {".applebot.apple.com"} 71 | bot_name = "Applebot" 72 | elseif matched_bot == "linkedinbot" then 73 | -- https://udger.com/resources/ua-list/bot-detail?bot=LinkedInBot 74 | bot_domains = {".fwd.linkedin.com"} 75 | bot_name = "LinkedInBot" 76 | elseif matched_bot == "amazonbot" then 77 | -- https://developer.amazon.com/support/amazonbot 78 | bot_domains = {".crawl.amazonbot.amazon"} 79 | bot_name = "Amazonbot" 80 | else 81 | return nil 82 | end 83 | local remote_addr = m.getvar("REMOTE_ADDR", "none") 84 | local remote_host = m.getvar("REMOTE_HOST", "none") 85 | -- If Apache HostnameLookups is On, we can do one less DNS lookup. 86 | if not check_ip(remote_host) then 87 | hosts = { [1] = remote_host } 88 | else 89 | hosts = socket.dns.getnameinfo(remote_addr) 90 | end 91 | for k1, host in pairs(hosts) do 92 | for k2, domain in pairs(bot_domains) do 93 | if ends_with(host, domain) then 94 | addrinfo = socket.dns.getaddrinfo(host) 95 | if addrinfo ~= nil then 96 | for k3, ips in pairs(addrinfo) do 97 | for k4, ip in pairs(ips) do 98 | if remote_addr == ip then 99 | return nil 100 | end 101 | end 102 | end 103 | end 104 | end 105 | end 106 | end 107 | m.setvar("tx.fake-bot-plugin_bot_name", bot_name) 108 | return string.format("Fake Bot Plugin: Detected fake %s.", bot_name) 109 | end 110 | -------------------------------------------------------------------------------- /renovate.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://docs.renovatebot.com/renovate-schema.json", 3 | "extends": [ 4 | "local>coreruleset/renovate-config" 5 | ] 6 | } 7 | -------------------------------------------------------------------------------- /tests/regression/fake-bot-plugin/9504120.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | meta: 3 | author: "azurit" 4 | description: "Fake Bot Plugin" 5 | enabled: true 6 | name: 9504120.yaml 7 | tests: 8 | - test_title: 9504120-1 9 | desc: Check that plugin is not blocking standard User-Agents 10 | stages: 11 | - stage: 12 | input: 13 | dest_addr: 127.0.0.1 14 | headers: 15 | Host: localhost 16 | User-Agent: "OWASP CRS test agent" 17 | Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5 18 | port: 80 19 | method: GET 20 | version: HTTP/1.1 21 | uri: /get 22 | output: 23 | no_log_contains: id "9504120" 24 | - test_title: 9504120-2 25 | desc: Check for blocking of fake Googlebot 26 | stages: 27 | - stage: 28 | input: 29 | dest_addr: 127.0.0.1 30 | headers: 31 | Host: localhost 32 | User-Agent: "OWASP CRS test agent: Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; Googlebot/2.1; +http://www.google.com/bot.html) Chrome/W.X.Y.Z Safari/537.36" 33 | Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5 34 | port: 80 35 | method: GET 36 | version: HTTP/1.1 37 | uri: /get 38 | output: 39 | log_contains: id "9504120" 40 | - test_title: 9504120-3 41 | desc: Check for blocking of fake Bingbot 42 | stages: 43 | - stage: 44 | input: 45 | dest_addr: 127.0.0.1 46 | headers: 47 | Host: localhost 48 | User-Agent: "OWASP CRS test agent: Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm) W.X.Y.Z Safari/537.36" 49 | Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5 50 | port: 80 51 | method: GET 52 | version: HTTP/1.1 53 | uri: /get 54 | output: 55 | log_contains: id "9504120" 56 | - test_title: 9504120-4 57 | desc: Check for blocking of fake Twitterbot 58 | stages: 59 | - stage: 60 | input: 61 | dest_addr: 127.0.0.1 62 | headers: 63 | Host: localhost 64 | User-Agent: "OWASP CRS test agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36 Twitterbot/1.0" 65 | Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5 66 | port: 80 67 | method: GET 68 | version: HTTP/1.1 69 | uri: /get 70 | output: 71 | log_contains: id "9504120" 72 | - test_title: 9504120-5 73 | desc: Check for blocking of fake Applebot 74 | stages: 75 | - stage: 76 | input: 77 | dest_addr: 127.0.0.1 78 | headers: 79 | Host: localhost 80 | User-Agent: "OWASP CRS test agent: Mozilla/5.0 (Device; OS_version) AppleWebKit/WebKit_version (KHTML, like Gecko) Version/Safari_version Safari/WebKit_version (Applebot/Applebot_version)" 81 | Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5 82 | port: 80 83 | method: GET 84 | version: HTTP/1.1 85 | uri: /get 86 | output: 87 | log_contains: id "9504120" 88 | - test_title: 9504120-6 89 | desc: Check for blocking of fake LinkedInBot 90 | stages: 91 | - stage: 92 | input: 93 | dest_addr: 127.0.0.1 94 | headers: 95 | Host: localhost 96 | User-Agent: "OWASP CRS test agent: LinkedInBot/1.0 (compatible; Mozilla/5.0; +http://www.linkedin.com)" 97 | Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5 98 | port: 80 99 | method: GET 100 | version: HTTP/1.1 101 | uri: /get 102 | output: 103 | log_contains: id "9504120" 104 | - test_title: 9504120-7 105 | desc: Check for blocking of fake Amazonbot 106 | stages: 107 | - stage: 108 | input: 109 | dest_addr: 127.0.0.1 110 | headers: 111 | Host: localhost 112 | User-Agent: "OWASP CRS test agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/600.2.5 (KHTML, like Gecko) Version/8.0.2 Safari/600.2.5 (Amazonbot/0.1; +https://developer.amazon.com/support/amazonbot)" 113 | Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5 114 | port: 80 115 | method: GET 116 | version: HTTP/1.1 117 | uri: /get 118 | output: 119 | log_contains: id "9504120" 120 | -------------------------------------------------------------------------------- /util/fake-bot-report.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # Script to analyze the alerts of the OWASP CRS 4 | # Fake Bot Plugin based on the error log alerts. 5 | # 6 | # Usage: cat error.log | ./fake-bot-report.sh 7 | # 8 | # Created in 2022 by Christian Folini. 9 | # License: Public Domain 10 | # 11 | # CAVEAT: Please note that this script only reports 1 User-Agent 12 | # per offending IP. 13 | # Also, need to have geoiplookup installed. 14 | 15 | UAWIDTH=`expr $(tput cols) - 27` 16 | TMPFILE=$(mktemp) 17 | 18 | cat | grep 9504110 > $TMPFILE 19 | 20 | (cat $TMPFILE | grep -o "\[client [^]]*" | cut -b9- | sort | uniq | while read IP; do 21 | N=$(grep -c $IP $TMPFILE) 22 | COUNTRY=$(geoiplookup $IP 2>/dev/null | egrep -o "[A-Z]{2}," | tr -d ,) 23 | USERAGENT=$(grep $IP $TMPFILE | grep -o 'REQUEST_HEADERS:User-Agent: [^]]*' | sort | uniq | cut -d: -f3- | sed -e 's/\(.\{'$UAWIDTH'\}\).*/\1 .../' | tr -d \" | head -1) 24 | printf "%3s %2s %-15s %s\n" $N $COUNTRY $IP "$USERAGENT" 25 | done) | sort -nr 26 | 27 | trap 'rm -f "$TMPFILE"' EXIT 28 | --------------------------------------------------------------------------------