Fix MSSQL JSON parsing in UNION injection with truncation recovery #5995
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix MSSQL JSON Parsing Issues in UNION Injection Technique
Problem Description
When using UNION-based SQL injection against Microsoft SQL Server with JSON aggregation mode (
FOR JSON AUTO), sqlmap encountered multiple critical issues that prevented successful data extraction:Issue 1: Truncated JSON Responses
Large JSON responses were being truncated due to HTTP response size limits (
MAX_CONNECTION_TOTAL_SIZE), resulting in incomplete JSON that could not be parsed. This caused errors like:Issue 2: HTML Entity Encoding
JSON responses containing HTML entities (e.g.,
"instead of") were failing to parse, as the entities were not being decoded before JSON parsing.Issue 3: Inefficient Field Extraction
The original implementation used regex to extract field names from only the first JSON object, which was:
Solution
This PR implements a robust solution with the following improvements:
1. Automatic JSON Repair for Truncated Responses
When a
JSONDecodeErroroccurs due to truncation:]2. HTML Entity Decoding
Added automatic HTML entity decoding using Python's
html.unescape()before JSON parsing:3. Improved Field Extraction
Replaced regex-based field extraction with direct dictionary key access:
4. Better Error Handling
.get()method for safer field accessTesting
Tested against Microsoft SQL Server with:
Before Fix
After Fix
Impact
Files Changed
lib/techniques/union/use.py- Enhanced MSSQL JSON parsing in_oneShotUnionUse()functionBackward Compatibility
This change is fully backward compatible. The improvements only affect the MSSQL JSON aggregation code path and gracefully fall back to the original behavior if the repair attempt fails.