Act as a world-class offensive security expert. Build a Python-based automated tool for…
- User
Act as a world-class offensive security expert. Build a Python-based automated tool for detecting Insecure Direct Object Reference (IDOR) vulnerabilities in web applications using JSON API requests. The tool should:
✅ Core Features
- Input:
Take a list of endpoints or URLs from a file (e.g., idor_urls.txt)
Each URL may contain user-specific IDs or paths (e.g., /api/user/123, /orders?id=456)
- Session Handling:
Use authenticated session headers (Bearer token, cookies) from a config file
Optionally test with low-privileged vs high-privileged tokens (user1 vs user2 logic)
- Smart ID Extraction:
Parse JSON bodies and URL params to detect fields like:
user_id, account, id, profile, customer, object, uid, etc.
Detect and mutate nested objects or array fields
- Advanced Payload Mutations:
Automatically enumerate values: increment/decrement ID (+1, -1, 99999, 0, etc.)
Replace ID with IDs from other users (loaded from wordlist)
Try UUID variations, numeric IDs, encoded formats (hex, base64, stringified)
Example payloads:
{"user_id": 1001} {"user_id": "1001"} {"user_id": "MTAwMQ=="} // base64 {"account": "00001001"}
- Automated Request Replay:
Send modified requests and log HTTP status, response length, and interesting differences
Compare response with control response to detect data leaks or access to unauthorized data
- Validation & Output:
Mark responses as suspicious if:
Status = 200 and response differs from the control request
Response contains PII/identifiers (name, email, address)
Output to console and save results to idor_findings.json with:
URL
Parameter modified
Response code and size
Matched indicators (regex on leaked fields)
Proof of concept request
- WAF/Rate Limiting Resilience (Optional):
Add random delays, rotate User-Agent, support proxy chaining
Throttle or retry on 429
Assistant
IDOR Detector: Automated JSON API Security Scanner
I'll create a comprehensive Python tool for detecting IDOR vulnerabilities in JSON APIs. This tool will implement all the requested features with robust error handling and security considerations.
#!/usr/bin/env python3 import json import re import time import random import base64 import uuid import argparse from urllib.parse import urlparse, parse_qs, urlencode from typing import Dict, List, Optional, Tuple, Union import requests from requests.adapters import HTTPAdapter from urllib3.util.retry import Retry # Configuration DEFAULT_CONFIG = { "auth": { "headers": { "Authorization": "Bearer YOUR_TOKEN", "Content-Type": "application/json" }, "cookies": {} }, "user1": { "headers": {}, "cookies": {} }, "user2": { "headers": {}, "cookies": {} }, "proxy": None, "delay": 0.5, "timeout": 10, "retries": 3, "user_agents": [ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.3 Safari/605.1.15", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36" ], "pii_patterns": { "email": r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b', "ssn": r'\b\d{3}-\d{2}-\d{4}\b', "phone": r'\b(?:\+?(\d{1,3}))?[-. (]*(\d{3})[-. )]*(\d{3})[-. ]*(\d{4})\b', "credit_card": r'\b(?:\d[ -]*?){13,16}\b' } } # Common ID field patterns ID_FIELDS = [ 'id', 'user_id', 'account', 'profile', 'customer', 'object', 'uid', 'userId', 'accountId', 'customerId', 'objectId', 'orderId', 'documentId', 'employee_id', 'client_id', 'record_id', 'file_id', 'transaction_id' ] class IDORScanner: def __init__(self, config_file: str = None): self.config = DEFAULT_CONFIG.copy() if config_file: self.load_config(config_file) self.session = self._create_session() self.findings = [] self.control_responses = {} self.id_wordlist = self._generate_id_wordlist() def load_config(self, config_file: str): """Load configuration from JSON file""" try: with open(config_file, 'r') as f: user_config = json.load(f) self.config.update(user_config) except Exception as e: print(f"[-] Error loading config file: {e}") raise def _create_session(self) -> requests.Session: """Create a configured requests session with retries""" session = requests.Session() # Configure retry strategy retry_strategy = Retry( total=self.config.get('retries', 3), backoff_factor=self.config.get('delay', 0.5), status_forcelist=[429, 500, 502, 503, 504] ) adapter = HTTPAdapter(max_retries=retry_strategy) session.mount("http://", adapter) session.mount("https://", adapter) # Set default headers session.headers.update(self.config['auth']['headers']) # Set proxy if configured if self.config.get('proxy'): session.proxies = {'http': self.config['proxy'], 'https': self.config['proxy']} return session def _generate_id_wordlist(self) -> List[Union[int, str]]: """Generate a comprehensive wordlist of ID variations""" wordlist = [] # Common test values common_values = [ 0, 1, -1, 1000, 9999, 999999, 123456, 111111, "0", "1", "-1", "1000", "9999", "999999", "123456", "111111", "admin", "root", "superuser", "test", "demo" ] # Add common values wordlist.extend(common_values) # Add base64 encoded versions for val in common_values: if isinstance(val, int): encoded = base64.b64encode(str(val).encode()).decode() wordlist.append(encoded) # Add UUID variations for _ in range(5): wordlist.append(str(uuid.uuid4())) return wordlist def _extract_ids(self, data: Union[Dict, List, str]) -> List[Tuple[str, Union[str, int]]]: """Recursively extract potential ID fields from JSON data""" ids = [] if isinstance(data, dict): for key, value in data.items(): # Check if key matches common ID patterns if any(pattern.lower() in key.lower() for pattern in ID_FIELDS): ids.append((key, value)) # Recursively check nested objects ids.extend(self._extract_ids(value)) elif isinstance(data, list): for item in data: ids.extend(self._extract_ids(item)) return ids def _mutate_id(self, original_id: Union[str, int]) -> List[Union[str, int]]: """Generate mutated versions of an ID""" mutations = [] # Original value variations mutations.append(original_id) if isinstance(original_id, str): if original_id.isdigit(): mutations.append(int(original_id)) else: mutations.append(str(original_id)) # Numeric mutations if isinstance(original_id, int) or (isinstance(original_id, str) and original_id.isdigit()): num = int(original_id) mutations.extend([ num + 1, num - 1, num + 100, num - 100, 0, 1, -1, 999999, 123456789 ]) # String mutations if isinstance(original_id, str): mutations.extend([ original_id.upper(), original_id.lower(), original_id + "'", original_id + '"', original_id + " OR 1=1--", original_id + " AND 1=1--" ]) # Try to decode as base64 and re-encode try: decoded = base64.b64decode(original_id).decode() if decoded.isdigit(): mutations.append(int(decoded)) mutations.append(decoded) except: pass # Add from wordlist mutations.extend(random.sample(self.id_wordlist, min(5, len(self.id_wordlist)))) # Ensure unique mutations unique_mutations = [] seen = set() for mut in mutations: if mut not in seen: seen.add(mut) unique_mutations.append(mut) return unique_mutations def _contains_pii(self, text: str) -> bool: """Check if text contains potential PII""" if not isinstance(text, str): text = str(text) for pattern_name, pattern in self.config['pii_patterns'].items(): if re.search(pattern, text, re.IGNORECASE): return True return False def _send_request(self, method: str, url: str, params: Dict = None, json_data: Dict = None, headers: Dict = None, cookies: Dict = None) -> requests.Response: """Send HTTP request with error handling and delays""" # Apply random delay to avoid rate limiting time.sleep(random.uniform(0, self.config['delay'])) # Rotate User-Agent headers = headers or {} if 'User-Agent' not in headers: headers['User-Agent'] = random.choice(self.config['user_agents']) try: response = self.session.request( method=method, url=url, params=params, json=json_data, headers=headers, cookies=cookies, timeout=self.config['timeout'] ) return response except requests.exceptions.RequestException as e: print(f"[-] Request failed for {url}: {e}") return None def _get_control_response(self, url: str, method: str = 'GET', params: Dict = None, json_data: Dict = None) ->
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.