|
| 1 | +import requests |
| 2 | +import xmltodict |
| 3 | + |
| 4 | + |
| 5 | +class PowerPages: |
| 6 | + """ |
| 7 | + A class that is responsible for the Power Pages scan |
| 8 | + """ |
| 9 | + |
| 10 | + def __init__(self, args): |
| 11 | + self.args = args |
| 12 | + url = self.normalize_url(args.url) |
| 13 | + self.url = url |
| 14 | + self.scan() |
| 15 | + |
| 16 | + def normalize_url(self, url): |
| 17 | + if not url.startswith("https://"): |
| 18 | + url = "https://" + url |
| 19 | + if url.endswith("/"): |
| 20 | + url = url[:-1] |
| 21 | + url = url.replace("www.", "") |
| 22 | + return url |
| 23 | + |
| 24 | + def scan(self): |
| 25 | + url = self.url |
| 26 | + print(f"Checking `{url}`") |
| 27 | + try: |
| 28 | + odata_tables, api_tables = self.get_odata_tables() |
| 29 | + if len(odata_tables) == 0: |
| 30 | + print("You are safe!") |
| 31 | + else: |
| 32 | + print(f"Found {len(odata_tables)} open tables in '{url}'.") |
| 33 | + if len(api_tables): |
| 34 | + print(f"Examining additional {len(api_tables)} potential tables through the api") |
| 35 | + print("\nChecking each table:") |
| 36 | + for table in odata_tables: |
| 37 | + try: |
| 38 | + self.get_odata_table_data(table) |
| 39 | + except Exception: |
| 40 | + print(f"Can't access table {table['name']} through the odata right now") |
| 41 | + for table in api_tables: |
| 42 | + try: |
| 43 | + self.get_api_table_data(table) |
| 44 | + except Exception: |
| 45 | + print(f"Can't access table {table['name']} through the api right now") |
| 46 | + except Exception: |
| 47 | + print(f"Can't access `{url}` anonymously") |
| 48 | + |
| 49 | + def get_odata_tables(self): |
| 50 | + url = self.url |
| 51 | + odata_url = f"{url}/_odata/$metadata" |
| 52 | + headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36"} |
| 53 | + resp = requests.get(odata_url, headers=headers, timeout=10) |
| 54 | + odata_tables = [] |
| 55 | + api_tables = [] |
| 56 | + if 200 <= resp.status_code <= 299: |
| 57 | + content_xml = resp.content.decode("utf-8") |
| 58 | + resp_json = xmltodict.parse(content_xml) |
| 59 | + entity_types = resp_json.get("edmx:Edmx", {}).get("edmx:DataServices", {}).get("Schema", {}).get("EntityType", []) |
| 60 | + entity_sets = ( |
| 61 | + resp_json.get("edmx:Edmx", {}).get("edmx:DataServices", {}).get("Schema", {}).get("EntityContainer", {}).get("EntitySet", []) |
| 62 | + ) |
| 63 | + table_names = {entity_set["@EntityType"][4:]: entity_set["@Name"] for entity_set in entity_sets} |
| 64 | + |
| 65 | + for entity in entity_types: |
| 66 | + name = table_names.get(entity.get("@Name", "unknown"), "unknown") |
| 67 | + key = entity.get("Key", {}).get("PropertyRef", {}).get("@Name", "unknown") |
| 68 | + columns = [prop.get("@Name", "unknown") for prop in entity.get("Property", [])] |
| 69 | + odata_tables.append({"name": name, "key": key, "columns": columns}) |
| 70 | + if name.endswith("Set"): |
| 71 | + api_table_name = name.lower()[:-3] + "s" |
| 72 | + api_tables.append({"name": api_table_name, "key": key, "columns": columns}) |
| 73 | + |
| 74 | + return odata_tables, api_tables |
| 75 | + |
| 76 | + def get_api_table_data(self, table): |
| 77 | + url = self.url |
| 78 | + table_name = table["name"] |
| 79 | + table_columns = table["columns"] |
| 80 | + api_table_url = f"{url}/_api/{table_name}?$top=1" |
| 81 | + headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36"} |
| 82 | + resp = requests.get(api_table_url, headers=headers, timeout=10) |
| 83 | + if 200 <= resp.status_code <= 299: |
| 84 | + resp_json = resp.json() |
| 85 | + if len(resp_json["value"]) > 0: |
| 86 | + print(f"Table {table_name} is exposed with all columns through the API!") |
| 87 | + return len(table_columns) |
| 88 | + else: |
| 89 | + print(f"Table {table_name} is accessible but returned no data through the API!") |
| 90 | + return 0 |
| 91 | + elif resp.status_code in [400, 403]: |
| 92 | + try: |
| 93 | + resp_json = resp.json() |
| 94 | + error_code = resp_json.get("error", {}).get("code", "") |
| 95 | + if not error_code == "90040101": |
| 96 | + raise Exception("Unknown error code") |
| 97 | + except Exception: |
| 98 | + print(f"Table {table_name} data is safe through the API") |
| 99 | + return |
| 100 | + print(f"Table {table_name} data is not exposed as a whole, checking each column...") |
| 101 | + exposed = [] |
| 102 | + for column in table_columns: |
| 103 | + api_column_url = f"{url}/_api/{table_name}?select={column}&$top=1" |
| 104 | + resp = requests.get(api_column_url, headers=headers, timeout=5) |
| 105 | + if 200 <= resp.status_code <= 299: |
| 106 | + resp_json = resp.json() |
| 107 | + if len(resp_json["value"]) > 0: |
| 108 | + exposed.append(column) |
| 109 | + if len(exposed): |
| 110 | + print(f"Table {table_name} has the following columns exposed: {', '.join(exposed)} through the API") |
| 111 | + return len(exposed) |
| 112 | + else: |
| 113 | + print(f"Table {table_name} data is safe through the API") |
| 114 | + return 0 |
| 115 | + |
| 116 | + def get_odata_table_data(self, table): |
| 117 | + url = self.url |
| 118 | + table_name = table["name"] |
| 119 | + table_columns = table["columns"] |
| 120 | + table_url = f"{url}/_odata/{table_name}?$top=1" |
| 121 | + headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36"} |
| 122 | + resp = requests.get(table_url, headers=headers, timeout=10) |
| 123 | + if 200 <= resp.status_code <= 299: |
| 124 | + resp_json = resp.json() |
| 125 | + if len(resp_json["value"]) > 0: |
| 126 | + print(f"Table {table_name} is exposed with all columns through the odata!") |
| 127 | + return len(table_columns) |
| 128 | + else: |
| 129 | + print(f"Table {table_name} is accessible but returned no data through the odata!") |
| 130 | + return 0 |
| 131 | + elif resp.status_code in [400, 403]: |
| 132 | + try: |
| 133 | + resp_json = resp.json() |
| 134 | + error_code = resp_json.get("error", {}).get("code", "") |
| 135 | + if not error_code == "90040101": |
| 136 | + raise Exception("Unknown error code") |
| 137 | + except Exception: |
| 138 | + print(f"Table {table_name} data is safe through the odata") |
| 139 | + return |
| 140 | + print(f"Table {table_name} data is not exposed as a whole, checking each column...") |
| 141 | + exposed = [] |
| 142 | + for column in table_columns: |
| 143 | + column_url = f"{url}/_odata/{table_name}?select={column}&$top=1" |
| 144 | + resp = requests.get(column_url, headers=headers, timeout=5) |
| 145 | + if 200 <= resp.status_code <= 299: |
| 146 | + resp_json = resp.json() |
| 147 | + if len(resp_json["value"]) > 0: |
| 148 | + exposed.append(column) |
| 149 | + if len(exposed): |
| 150 | + print(f"Table {table_name} has the following columns exposed: {', '.join(exposed)} through the odata") |
| 151 | + return len(exposed) |
| 152 | + else: |
| 153 | + print(f"Table {table_name} data is safe through the odata") |
| 154 | + return 0 |
0 commit comments