|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- # |
| 3 | + |
| 4 | +""" |
| 5 | +cms-detector: A Python Package to detect the Content Management System of a Website. |
| 6 | +
|
| 7 | +MIT License |
| 8 | +Copyright (c) 2023 SERP Wings www.serpwings.com |
| 9 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | +of this software and associated documentation files (the "Software"), to deal |
| 11 | +in the Software without restriction, including without limitation the rights |
| 12 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | +copies of the Software, and to permit persons to whom the Software is |
| 14 | +furnished to do so, subject to the following conditions: |
| 15 | +The above copyright notice and this permission notice shall be included in all |
| 16 | +copies or substantial portions of the Software. |
| 17 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | +SOFTWARE. |
| 24 | +""" |
| 25 | + |
| 26 | + |
| 27 | +# +++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 28 | +# IMPORTS Standard Library |
| 29 | +# +++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 30 | + |
| 31 | +import re |
| 32 | +from unittest.mock import Mock |
| 33 | + |
| 34 | +# +++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 35 | +# IMPORTS 3rd Party Libraries |
| 36 | +# +++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 37 | + |
| 38 | +import requests |
| 39 | +from requests.adapters import HTTPAdapter |
| 40 | +from requests.models import Response |
| 41 | +from bs4 import BeautifulSoup |
| 42 | + |
| 43 | +# +++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 44 | +# DATABASE/CONSTANTS LIST |
| 45 | +# +++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 46 | + |
| 47 | +HEADER = { |
| 48 | + "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0", |
| 49 | +} |
| 50 | + |
| 51 | +# +++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 52 | +# Utility Functions |
| 53 | +# +++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 54 | + |
| 55 | + |
| 56 | +def mock_requests_object(url): |
| 57 | + """Generates a mock request object""" |
| 58 | + response = Mock(spec=Response) |
| 59 | + response.text = "" |
| 60 | + response.status_code = 9999 |
| 61 | + response.url = url |
| 62 | + return response |
| 63 | + |
| 64 | + |
| 65 | +def get_remote_content(url, max_retires=2): |
| 66 | + """Get remote content avialble on a given url""" |
| 67 | + try: |
| 68 | + s = requests.Session() |
| 69 | + s.mount(url, HTTPAdapter(max_retries=max_retires)) |
| 70 | + return s.get(url, headers=HEADER) |
| 71 | + except: |
| 72 | + return mock_requests_object(url) |
| 73 | + |
| 74 | + |
| 75 | +def get_corrected_url(url, fix_slash="/"): |
| 76 | + """correct scheme and end slash of a url""" |
| 77 | + if not url.startswith("http://") and not url.startswith("https://"): |
| 78 | + url = f"http://{url}" |
| 79 | + |
| 80 | + if not url.endswith(fix_slash): |
| 81 | + url = f"{url}{fix_slash}" |
| 82 | + |
| 83 | + return url |
| 84 | + |
| 85 | + |
| 86 | +def wp_details(target_url): |
| 87 | + """Check if WordPress is installed on a given webiste. |
| 88 | +
|
| 89 | + It will also return name of plugins and themes, if installed on the website. |
| 90 | +
|
| 91 | + """ |
| 92 | + |
| 93 | + target_url = get_corrected_url(target_url, fix_slash="/") |
| 94 | + response = get_remote_content(target_url) |
| 95 | + |
| 96 | + if response.status_code < 400: |
| 97 | + link_regex = re.compile( |
| 98 | + "((https?):((/)|(\\\\))+([\w\d:#@%/;$()~_?\+-=\\\.&](#!)?)*)", |
| 99 | + re.DOTALL, |
| 100 | + ) |
| 101 | + all_link = set([link[0] for link in re.findall(link_regex, response.text)]) |
| 102 | + wp_content = [meta for meta in all_link if "wp-content" in meta] |
| 103 | + wp_includes = [meta for meta in all_link if "wp-includes" in meta] |
| 104 | + wp_json = [meta for meta in all_link if "wp-json" in meta] |
| 105 | + |
| 106 | + themes = [ |
| 107 | + re.search("/themes/(.*)/", link) for link in all_link if "/themes/" in link |
| 108 | + ] |
| 109 | + |
| 110 | + if themes: |
| 111 | + themes = list( |
| 112 | + set([theme.group(1).split("/")[0] for theme in themes if theme]) |
| 113 | + ) |
| 114 | + |
| 115 | + plugins = [ |
| 116 | + re.search("/plugins/(.*)/", link) |
| 117 | + for link in all_link |
| 118 | + if "/plugins/" in link |
| 119 | + ] |
| 120 | + |
| 121 | + if plugins: |
| 122 | + plugins = list( |
| 123 | + set([plugin.group(1).split("/")[0] for plugin in plugins if plugin]) |
| 124 | + ) |
| 125 | + |
| 126 | + wp_found = False |
| 127 | + wp_version = "" |
| 128 | + |
| 129 | + if any([wp_content, wp_includes, wp_json]): |
| 130 | + wp_found = True |
| 131 | + soup_xml = BeautifulSoup(response.content, "lxml") |
| 132 | + wp_version_tag = soup_xml.find("meta", attrs={"name": "generator"}) |
| 133 | + if wp_version_tag: |
| 134 | + wp_version = wp_version_tag.get("content") |
| 135 | + |
| 136 | + return { |
| 137 | + "is_wp_installed": wp_found, |
| 138 | + "wp_version": wp_version, |
| 139 | + "themes": themes, |
| 140 | + "plugins": plugins, |
| 141 | + } |
0 commit comments