b4 icon indicating copy to clipboard operation
b4 copied to clipboard

MCP: a set's fake payload is a number, and the topic does not say which payload each number is

Open DanielLavrushin opened this issue 3 weeks ago • 0 comments

b4_get_set and b4_get_config return a set's fake payload as faking.sni_type, a number whose defined values are 0 to 8. Both tools marshal the SetConfig struct as it is (mcp.go#L600-L606, mcp.go#L786-L799, mcp.go#L1123-L1134), and the field is a plain int (types.go#L420). The numbers are the FakePayload* constants in iota order (types.go#L163-L173):

Value Constant Web UI label
0 FakePayloadRandom Random
1 FakePayloadCustom none
2 FakePayloadDefault1 (the default) Preset: Google (classic)
3 FakePayloadDefault2 Preset: DuckDuckGo
4 FakePayloadCapture My own Payload File
5 FakePayloadZero All Zeros
6 FakePayloadInverted Inverted Original
7 FakePayloadDomain Generated from Domain
8 FakePayloadSTUN Preset: STUN

The topic faking.sni_type, which b4_get_topic returns, names the payloads in words only: 'random', 'google'/'duckduckgo', 'file', 'zeros', 'inverted', 'domain' (topics.json#L32). It gives no numbers. A model reading "sni_type": 2 cannot tell Google from DuckDuckGo, and nothing says whether 4 is 'file' or 'zeros'. Two values are missing from the topic entirely:

  • 8, STUN. It is Discovery's default payload. bestPayload starts as STUN (discovery.go#L72), selectBestPayload picks the fastest payload that worked and falls back to STUN when none did (payloads.go#L216-L247), and applyBestPayload writes it into the set (payloads.go#L286-L287). 22 of the 26 SNIType: fields in preset.go are also FakePayloadSTUN (for example preset.go#L45).
  • 1, Custom. b4 sends the bytes of faking.custom_payload as they are (payload.go#L126-L127). In b4's own code only the zapret/byedpi command-line import sets it, for an inline fake (emit.go#L753-L755).

The web UI has the same gap for 1. Its payload select lists eight options and none of them is 1 (TcpFaking.tsx#L70-L79). When no option matches, B4Select shows the raw value (B4Select.tsx#L76-L79), so an imported set with an inline fake shows its payload type as a bare 1. The select's AI explain button (B4AiExplain) sends the same topic, with the current value and available: [0, 2, 3, 8, 4, 5, 6, 7], numbers only (TcpFaking.tsx#L322-L325).

Writes

b4_set_config_value takes sets[<set>].faking.sni_type as a number only. The path has no entry in mcpEnums, mcpRanges or mcpValueAliases. The log level is the only aliased setting so far (mcp_write.go#L159-L163). As a result:

  • b4_list_writable_paths lists sets[].faking.sni_type as number with no options (mcp_write.go#L863-L872).
  • A write of stun is refused with value "stun" is not a whole number that fits sets[].faking.sni_type.
  • A write of 9 or -3 is saved and reported as changed: true. Beyond an overflow check, the only check on the number is mcpCheckRange, which has no range for this path (mcp_write.go#L617-L628), and config.Validate does not look at the field.

An unknown value gets no payload data in LoadCapturePayloads (methods.go#L1376-L1377). GetPayload then falls through to the Google ClientHello (payload.go#L121-L129). So a set written with 9 sends the Google preset, and no tool says so.

Fix notes

  • Put one name table in config, indexed by the constant, for example FakePayloadNames. Today the names disagree: the topic says 'file' and 'zeros', Discovery's log names say capture and zero (payloads.go#L249-L276), and the import notes say custom. Discovery's list plus custom is a natural base: random, custom, google, duckduckgo, capture, zero, inverted, domain, stun. getPayloadName can read from the table.
  • Topic faking.sni_type: give each payload its stored number and table name, say that 2 is the default, and add 1 (custom: the bytes of custom_payload, set by the import) and 8 (STUN, what Discovery writes). The entry is 634 bytes, and topics_test.go caps an entry at 2800. This is the fix for reads: b4_get_set and b4_get_config keep returning the number, and only the topic can say what it means.
  • Alias: add mcpSetPathPrefix + ".faking.sni_type" to mcpValueAliases, built from the table, with all nine values including custom. mcpApplyAlias refuses anything outside the list, so a stored 1 can still be written back. The same rule makes out-of-range numbers like 9 fail (mcp_write.go#L180-L192). Write results, b4_revert_last_change and b4_list_writable_paths then report the name through mcpFormatCurrent (mcp_write.go#L549-L557).
  • UI: add an option for 1 (payloadCustom in en.json and ru.json) so an imported set no longer shows a bare 1.
  • Tests:
    • In src/ai, TestFakePayloadTopicNamesEveryValue: for every entry in the name table, faking.sni_type contains the number and the name. The test fails as long as 1 and 8 are missing.
    • In src/http/handler, TestMCPWriteFakePayloadByName, modelled on TestMCPWriteLogLevelByName. Writing stun stores 8 and returns current: "stun". Writing 9 is refused. b4_list_writable_paths lists the nine names for sets[].faking.sni_type.

DanielLavrushin avatar Nov 07 '26 20:11 DanielLavrushin