Understanding UNION Query Shape
A conditional test suggests that a product identifier may influence a database query. Understanding UNION query shape asks whether a second result can fit the first and actually reach the page. The challenge is not simply making an unusual request stop producing an error. Follow three requirements in order: the number of columns, the compatible value positions, and the position the application displays.
The question isn’t whether you remember a payload. It’s whether the application’s query can be combined with a second query whose results appear in the response. Before that can work, you need to understand the shape of the original result.
How many columns does it return? Which positions can accept the data types you want to supply? Which returned positions, if any, are actually rendered on the page? A successful-looking status code alone won’t answer those questions.
UNION is a Structured Query Language operation that combines the result sets of compatible SELECT statements. In this authorized lab, SQL injection exists because untrusted input has been incorporated unsafely into a dynamic query. Your input can alter the intended query structure rather than remaining a simple item identifier. Parameterized queries are a primary defense because they separate the query’s structure from its untrusted values.
Your immediate goal is narrower than extracting everything the database account can read. First, determine whether UNION behavior is available and find the result shape required by the original query. Then identify a position that can carry a controlled marker into the application’s response. Only after those conditions are supported should you use the behavior to demonstrate access to a small piece of synthetic data that the application wasn’t intended to disclose.
Begin by confirming the authorization boundary and recording the exact target, endpoint, parameter, request method, and current access level. Capture an ordinary request and its ordinary response. Note the response status, approximate content, relevant text, and any stable application behavior. That baseline gives you something meaningful to compare against. Without it, an error page, an empty result, or a changed page length can look important even when it’s ordinary application noise.
Write down a prediction before the first UNION test. If your hypothesis is correct and the original query returns three columns, then a second SELECT with three suitable expressions may be accepted. A version with two or four should fail or behave differently. If one of those three result positions is displayed by the application, placing a unique lab marker there should cause that marker to appear in the response. These are testable predictions, not conclusions you’ve already reached.
Column count is the number of values produced in each row of the result set. For UNION to combine two SELECT results, their column counts must match. Corresponding values also need data types the database can treat as compatible. That creates two separate problems. You must find the correct number of expressions, and you must find expressions that don’t cause a type conflict in their respective positions.
One manual approach is to vary the number of expressions in a bounded UNION SELECT test. You might begin with one NULL value, then two NULL values, then three, changing only the count. NULL is useful for initial shape testing because databases can often treat it as compatible with more than one data type. It isn’t a guarantee across every query or database management system, but it reduces unnecessary assumptions while you investigate the result shape.
The surrounding query still matters. Your input has to fit its quoting and syntax context. The application may expect a number, a quoted string, or another expression.
The original query might continue after the injected value, requiring appropriate termination in that specific lab. Comment syntax and accepted expression syntax can vary by database management system. If you haven’t established those details, a rejected request doesn’t tell you that UNION is impossible. It may only tell you that the injected fragment didn’t fit the original statement.
Suppose the baseline product request returns a normal page. A one-column test causes a database-related error, and a two-column test does the same. A three-column test restores a response that resembles the baseline.
That difference supports a three-column hypothesis, but it doesn’t yet prove unauthorized data access. The application might have handled the malformed input differently for some unrelated reason. Repeat the observation, preserve the exact requests, and use a controlled marker to test whether your second result set is reaching the response.
Now consider data-type compatibility. Imagine that the original query returns a numeric identifier, a text product name, and a numeric price. A second result containing text in all three positions may fail because the first or third position expects numeric-compatible data.
Instead of changing the entire test, keep NULL in positions you aren’t evaluating and place a distinctive text marker in one position at a time. Test marker, NULL, NULL. Then test NULL, marker, NULL. Finally, test NULL, NULL, marker.
The outcomes answer two questions. If the database accepts text only in the second position, you’ve found a likely text-compatible column. If the marker also appears in the page, you’ve found an output position rendered by the application.
Acceptance and visibility aren’t the same thing. A position may accept your value but never be included in the returned page. Conversely, an application error might hide a database result that was otherwise valid. Treat the page as one observation in a larger chain, not as an all-knowing verdict.
Count, compatibility, and visibility answer different questions. A three-column result can have the right shape but still put text where a number is expected. Even a compatible value proves little about disclosure until the application actually displays it.
Suppose the second test returns the marker in the product-name area. You can now explain the mechanism. User input changed the original query. The injected SELECT matched the original three-column shape.
Its second value was compatible with the corresponding output type. The application rendered that second value in the response. Each part of that explanation is supported by an observation. You’re no longer relying on the mere fact that a payload didn’t crash.
Within the deliberately vulnerable lab, the next bounded test can replace that marker with a small, approved synthetic value from a table the application account can read. For example, the lab may contain a demonstration account record specifically intended for this exercise. If that record appears in the same rendered position, you’ve demonstrated unauthorized data access through the vulnerable parameter. Preserve enough context to show that the value came from the injected result rather than from data already present on the ordinary page.
Be precise about what this proves. It shows that this request path allowed you to alter a query and return the selected synthetic value under the database permissions available to the application. It doesn’t automatically prove that every table is readable, that administrative access is available, or that operating-system command execution is possible. Those are separate claims with separate prerequisites. A successful UNION test is evidence for the capability you observed, not permission to infer an entire compromise chain.
Return to the example’s three positions: identifier, product name, and price. The product-name position may accept and display text even when the other positions do not. A missing marker therefore calls for checking compatibility and rendering, not changing every part of the request.
Failure also needs careful interpretation. Imagine that every UNION test returns the same generic error page. Several prerequisites remain unverified. The column count may be wrong.
The text marker may be in a numeric position. The quoting context or query termination may be incorrect. The database management system may interpret the syntax differently. The application could be suppressing database details, filtering the input, or issuing a query whose output isn’t reflected in the response.
Return to the smallest unresolved assumption. If you don’t know whether the failure comes from column count or data type, keep the test values as neutral as the lab permits and vary only the count. If a likely count has been identified, keep that count fixed while moving one text marker through the positions. If the response changes only when the marker reaches the second position, record that result before attempting another variation. One controlled change gives you a clearer comparison than several simultaneous edits.
Response differences deserve the same discipline. A database error directly confirms that some database processing failed, but it may not identify the exact cause. A normal page suggests that the request was accepted, but it doesn’t prove that your added row was used. A missing marker could mean the position isn’t rendered, the original query returned no compatible result, or the application transformed the output. Repeating a controlled request and comparing stable details helps separate injection-driven behavior from inconsistent page content.
A failed first attempt therefore doesn’t establish that the vulnerability is absent. It establishes that this particular test, under these particular assumptions, didn’t produce the predicted evidence. Keep the failed request and response. They narrow the hypothesis space and prevent you from unknowingly repeating the same mistake. If later evidence shows that the query needed three columns rather than two, your notes can explain why the first test failed and why the corrected test was justified.
Manual work matters here because it forces you to understand the relationship among query shape, type compatibility, rendered output, and accessible data. Automation may accelerate testing in an authorized training environment, but a tool’s finding isn’t equivalent to a validated exploit chain. If it reports a possible UNION injection, you still need to know what request changed, which conditions made it work, what data was actually returned, and whether the result is repeatable.
Your evidence should allow a technically competent reader to reconstruct the result without guessing. Record the target identifier and the vulnerable endpoint. Include the affected parameter and whether the request was sent in the path, query string, or body. Preserve the baseline request, the tests used to determine column count, the checks for type-compatible and visible positions, and the final bounded request that returned the synthetic unauthorized value. Keep meaningful responses or extracts that show the comparison.
For each step, distinguish what you observed from what you concluded. You observed that a three-expression result restored the expected response while neighboring counts produced an error. You concluded that three was the likely column count.
You observed that a marker in the second position appeared in the product-name area. You concluded that the second position accepted text and was rendered. You observed that the approved synthetic account value later appeared there. You concluded that the injection enabled unauthorized reading of that value through the application’s database context.
Also record the circumstances that could affect reproducibility. Note whether authentication was required, which application role you used, and whether the page needed a particular product or filter state. Preserve exact request syntax rather than writing only that UNION injection worked. If a screenshot is needed, it should show the target context, the relevant input or request, and the returned evidence clearly enough to support the written explanation. A screenshot without the surrounding reasoning can’t replace reproducible steps.
The finding’s impact should match the evidence. If the lab test retrieved one synthetic record, explain that the vulnerable query allowed data outside the intended application workflow to be returned. Don’t claim complete database extraction unless you actually validated that scope and were authorized to do so. The report can also connect the root cause to unsafe dynamic query construction and identify parameterized queries as a key prevention approach. That ties the observed exploit to a meaningful mitigation without pretending one generic recommendation covers every implementation detail.
Once the result is documented, choose the next action based on the assessment question. If a controlled disclosure is enough to establish the finding, further extraction may add risk without adding useful evidence. If the lab objective requires another value, use the validated column shape and visible text position rather than restarting at random. If no output position is reflected despite careful validation, visible UNION extraction may not be the appropriate path, and the next investigation may involve error-based or blind behavior under the same disciplined evidence standard.
UNION-based testing depends on compatible query shape, column count, data placement, and rendered evidence that comes from the database. Before reaching for a tool, answer this: How will you establish the compatible column count and data position without changing multiple assumptions at once? The matching study reference keeps these review points together for later practice. This lesson was produced by Bare Metal Cyber dot com.