Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
582 changes: 243 additions & 339 deletions packages/backend/src/checks/user-agent-dependent-response/index.spec.ts

Large diffs are not rendered by default.

167 changes: 85 additions & 82 deletions packages/backend/src/checks/user-agent-dependent-response/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { continueWith, defineCheck, done, Severity } from "engine";
import { defineCheckV2, Result, Severity } from "engine";

import { Tags } from "../../types";
import { keyStrategy } from "../../utils";
Expand All @@ -16,79 +16,112 @@ const USER_AGENTS = [
},
];

type State = {
originalStatus: number;
originalLength: number;
probes: {
userAgent: string;
responseCode: number;
bodyLength: number;
}[];
type ProbeResult = {
userAgent: string;
responseCode: number;
bodyLength: number;
};

const bodyLength = (text: string | undefined): number =>
text === undefined ? 0 : text.length;

export default defineCheck<State>(({ step }) => {
step("probeUserAgents", async (state, context) => {
const { request, response } = context.target;
if (response === undefined) {
return done({ state });
const hasMeaningfulDifference = (
left: { responseCode: number; bodyLength: number },
right: { responseCode: number; bodyLength: number },
): boolean => {
if (left.responseCode !== right.responseCode) {
return true;
}

const lengthDifference = Math.abs(left.bodyLength - right.bodyLength);
return lengthDifference > 100;
};

const hasProbeVariance = (probes: ProbeResult[]): boolean => {
for (let i = 0; i < probes.length; i++) {
const firstProbe = probes[i];
if (firstProbe === undefined) {
continue;
}

const probes: State["probes"] = [];
for (let j = i + 1; j < probes.length; j++) {
const secondProbe = probes[j];
if (secondProbe === undefined) {
continue;
}

if (hasMeaningfulDifference(firstProbe, secondProbe)) {
return true;
}
}
}

return false;
};

export default defineCheckV2({
id: "user-agent-dependent-response",
name: "User agent dependent response",
description:
"Detects differences in responses when varying the User-Agent header.",
type: "active",
tags: [Tags.INFORMATION_DISCLOSURE, Tags.INPUT_VALIDATION],
severities: [Severity.INFO],
aggressivity: {
minRequests: USER_AGENTS.length,
maxRequests: USER_AGENTS.length,
},
dedupeKey: keyStrategy().withHost().withPath().build(),
when: (target) =>
target.request.getMethod().toUpperCase() === "GET" &&
target.response !== undefined,
async execute(ctx) {
const originalResponse = ctx.target.response;
if (originalResponse === undefined) {
return;
}

const probes: ProbeResult[] = [];

for (const profile of USER_AGENTS) {
const spec = request.toSpec();
const spec = ctx.target.request.toSpec();
spec.setHeader("User-Agent", profile.value);

const result = await context.sdk.requests.send(spec);
const probeResponse = result.response;
if (probeResponse === undefined) {
const result = await ctx.send(spec);
if (Result.isErr(result)) {
continue;
}

probes.push({
userAgent: profile.label,
responseCode: probeResponse.getCode(),
bodyLength: bodyLength(probeResponse.getBody()?.toText()),
responseCode: result.value.response.getCode(),
bodyLength: bodyLength(result.value.response.getBody()?.toText()),
});
}

return continueWith({
nextStep: "evaluateDifferences",
state: {
originalStatus: response.getCode(),
originalLength: bodyLength(response.getBody()?.toText()),
probes,
},
});
});

step("evaluateDifferences", (state, context) => {
if (state.probes.length === 0) {
return done({ state });
if (probes.length < 2) {
return;
}

const originalStatus = state.originalStatus;
const originalLength = state.originalLength;

const differences = state.probes.filter((probe) => {
if (probe.responseCode !== originalStatus) {
return true;
}

const lengthDifference = Math.abs(probe.bodyLength - originalLength);
return lengthDifference > 100;
});
const original = {
responseCode: originalResponse.getCode(),
bodyLength: bodyLength(originalResponse.getBody()?.toText()),
};

const differences = probes.filter((probe) =>
hasMeaningfulDifference(probe, original),
);
if (differences.length === 0) {
return done({ state });
return;
}

if (!hasProbeVariance(probes)) {
return;
}

const details = differences
.map((probe) => {
return `- User agent \`${probe.userAgent}\` received status ${probe.responseCode} (body length ${probe.bodyLength}), while original response was status ${originalStatus} (body length ${originalLength})`;
return `- User agent \`${probe.userAgent}\` received status ${probe.responseCode} (body length ${probe.bodyLength}), while original response was status ${original.responseCode} (body length ${original.bodyLength})`;
})
.join("\n");

Expand All @@ -100,40 +133,10 @@ export default defineCheck<State>(({ step }) => {
"Such behaviour can indicate user-agent based content filtering or potential fingerprinting opportunities.",
].join("\n");

return done({
state,
findings: [
{
name: "User agent dependent response detected",
description,
severity: Severity.INFO,
correlation: {
requestID: context.target.request.getId(),
locations: [],
},
},
],
ctx.finding({
name: "User agent dependent response detected",
description,
severity: Severity.INFO,
});
});

return {
metadata: {
id: "user-agent-dependent-response",
name: "User agent dependent response",
description:
"Detects differences in responses when varying the User-Agent header.",
type: "active",
tags: [Tags.INFORMATION_DISCLOSURE, Tags.INPUT_VALIDATION],
severities: [Severity.INFO],
aggressivity: {
minRequests: USER_AGENTS.length,
maxRequests: USER_AGENTS.length,
},
},
initState: () => ({ originalStatus: 0, originalLength: 0, probes: [] }),
dedupeKey: keyStrategy().withHost().withPath().build(),
when: (target) =>
target.request.getMethod().toUpperCase() === "GET" &&
target.response !== undefined,
};
},
});
3 changes: 3 additions & 0 deletions packages/backend/src/stores/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ export class ConfigStore {
return;
}

const allScopeIDs = await this.getAllScopeIDs();
this.config.passive.scopeIDs = allScopeIDs;

const defaultPreset = this.getDefaultPreset();
if (defaultPreset) {
this.config.active.overrides = defaultPreset.active;
Expand Down
3 changes: 2 additions & 1 deletion packages/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"primevue": "4.1.0",
"shared": "workspace:*",
"ts-deepmerge": "7.0.3",
"vue": "3.5.27"
"vue": "3.5.27",
"vue-virtual-scroller": "2.0.0-beta.8"
},
"devDependencies": {
"@caido/sdk-backend": "0.55.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend/src/components/checks/Expansion.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const { getAggressivityText } = useTable();
</script>

<template>
<div class="px-4 py-2 space-y-2">
<div class="px-6 py-4 space-y-2">
<div>
<span class="font-bold">Type:</span>
<span class="capitalize ml-2">{{ check.type }}</span>
Expand Down
Loading