================================================================================
FINGERPRINT 7 ("strangetcp") - TCP OPTIONS BUFFER LAYOUT DIAGRAM
================================================================================

BUFFER DECLARATION:
  uint8_t tcpoptions[64];  // Total capacity: 64 bytes

DECLARED LENGTH:
  s->ss->tcpoptions_len = 24;  // Claims only 24 bytes used

ACTUAL BYTES WRITTEN:
  37 bytes (indices 0-36)

================================================================================
VISUAL BUFFER LAYOUT
================================================================================

Index    Hex Data      Description                          Status
-----    --------      -----------                          ------
  0      02            MSS option kind                      ✅ SENT
  1      04            MSS length (4 bytes total)           ✅ SENT
  2      04            MSS value high byte                  ✅ SENT
  3      00            MSS value low byte (1024)            ✅ SENT
        ----
  4      04            SACK Permitted kind                  ✅ SENT
  5      02            SACK length (2 bytes total)          ✅ SENT
        ----
  6      13            MD5 Signature kind (RFC 2385)        ✅ SENT
  7      12            MD5 length (18 bytes = 0x12)         ✅ SENT
  8      xx            Random hash byte 1                   ✅ SENT
  9      xx            Random hash byte 2                   ✅ SENT
 10      xx            Random hash byte 3                   ✅ SENT
 11      xx            Random hash byte 4                   ✅ SENT
 12      xx            Random hash byte 5                   ✅ SENT
 13      xx            Random hash byte 6                   ✅ SENT
 14      xx            Random hash byte 7                   ✅ SENT
 15      xx            Random hash byte 8                   ✅ SENT
 16      xx            Random hash byte 9                   ✅ SENT
 17      xx            Random hash byte 10                  ✅ SENT
 18      xx            Random hash byte 11                  ✅ SENT
 19      xx            Random hash byte 12                  ✅ SENT
 20      xx            Random hash byte 13                  ✅ SENT
 21      xx            Random hash byte 14                  ✅ SENT
 22      xx            Random hash byte 15                  ✅ SENT
 23      xx            Random hash byte 16                  ✅ SENT
        ===============================================================
        ↑↑↑ DECLARED LENGTH BOUNDARY (tcpoptions_len = 24) ↑↑↑
        ===============================================================
 24      08            Timestamps kind                      ❌ NOT SENT!
 25      0a            Timestamps length (10 bytes)         ❌ NOT SENT!
 26      xx            TSval byte 1                         ❌ NOT SENT!
 27      xx            TSval byte 2                         ❌ NOT SENT!
 28      xx            TSval byte 3                         ❌ NOT SENT!
 29      xx            TSval byte 4                         ❌ NOT SENT!
 30      xx            TSecr byte 1                         ❌ NOT SENT!
 31      xx            TSecr byte 2                         ❌ NOT SENT!
 32      xx            TSecr byte 3                         ❌ NOT SENT!
 33      xx            TSecr byte 4                         ❌ NOT SENT!
 34      01            NOP                                  ❌ NOT SENT!
 35      01            NOP (written twice - bug!)           ❌ NOT SENT!
 36      01            NOP                                  ❌ NOT SENT!
        ===============================================================
        ↑↑↑ ACTUAL WRITE BOUNDARY (37 bytes written) ↑↑↑
        ===============================================================
37-63    ??            Uninitialized / unused               (27 bytes)
        ===============================================================
        ↑↑↑ BUFFER END (64 bytes total capacity) ↑↑↑
        ===============================================================

================================================================================
BYTE COUNT SUMMARY
================================================================================

Option Type       Indices    Bytes    Cumulative    Sent?
--------------    -------    -----    ----------    -----
MSS               0-3        4        4             ✅ YES
SACK Permitted    4-5        2        6             ✅ YES
MD5 Signature     6-23       18       24            ✅ YES
                  ─────────────────────────────────────────
                            DECLARED LENGTH = 24
                  ═════════════════════════════════════════
Timestamps        24-33      10       34            ❌ NO
NOP Padding       34-36      3        37            ❌ NO
                  ─────────────────────────────────────────
                            ACTUAL WRITTEN = 37
                  ═════════════════════════════════════════
Unused            37-63      27       64            N/A
                  ─────────────────────────────────────────
                            BUFFER CAPACITY = 64

================================================================================
THE BUG VISUALIZED
================================================================================

┌─────────────────────────────────────────────────────────────────┐
│  tcpoptions[64] buffer                                          │
├───────────────────────────┬─────────────┬───────────────────────┤
│  Indices 0-23             │  24-36      │  37-63                │
│  MSS + SACK + MD5         │  TS + NOPs  │  Unused               │
│                           │             │                       │
│  ✅ SENT in packet        │  ❌ DROPPED │  (garbage)            │
│  (24 bytes)               │  (13 bytes) │  (27 bytes)           │
│                           │             │                       │
│  tcpoptions_len claims ──>│             │                       │
│  we only use 24 bytes     │  ← But we   │                       │
│                           │    wrote    │                       │
│                           │    here!    │                       │
└───────────────────────────┴─────────────┴───────────────────────┘

================================================================================
MEMORY WRITE TRACE (Code Flow)
================================================================================

Line   Index   Value     Comment
----   -----   -----     -------
311    N/A     N/A       Set tcpoptions_len = 24 (THE LIE)

315    [0]     0x02      MSS kind
315    [1]     0x04      MSS length
316    [2-3]   1024      MSS value (memcpy, network order)

319    [4]     0x04      SACK kind
319    [5]     0x02      SACK length

322    [6]     0x13      MD5 Signature kind
322    [7]     0x12      MD5 length (18 bytes)
325    [8-11]  random    First hash word (memcpy)
327    [12-15] random    Second hash word (memcpy)
329    [16-19] random    Third hash word (memcpy)
331    [20-23] random    Fourth hash word (memcpy)

       ┌──────────────────────────────────────────────────┐
       │ ⚠️  BOUNDARY CROSSED: Beyond tcpoptions_len=24  │
       └──────────────────────────────────────────────────┘

333    [24]    0x08      Timestamps kind (BEYOND BOUNDARY!)
333    [25]    0x0a      Timestamps length (BEYOND BOUNDARY!)
334    [26-29] l_tstamp  TSval (memcpy, BEYOND BOUNDARY!)
335    [30-33] r_tstamp  TSecr (memcpy, BEYOND BOUNDARY!)

336    [34]    0x01      NOP (BEYOND BOUNDARY!)
336    [35]    0x01      NOP (BEYOND BOUNDARY!)
337    [35]    0x01      NOP (DUPLICATE WRITE - BUG!)
337    [36]    0x01      NOP (BEYOND BOUNDARY!)

       Total bytes written: 37
       Declared length: 24
       Overflow: 13 bytes

================================================================================
PACKET BUILDER CODE TRACE (makepkt.c)
================================================================================

// Validation checks (lines 114-119):
if (tcpopts_s % 4) PANIC("bad tcp option");
  → 24 % 4 = 0 ✅ PASS

if (tcpopts_s > 60) PANIC("bad tcp optlen");
  → 24 > 60 = false ✅ PASS

// TCP Data Offset calculation (line 136):
th.doff = (sizeof(th) + tcpopts_s) / 4;
  → (20 + 24) / 4 = 11 (TCP header = 44 bytes)

// Copy options to packet (line 158):
memcpy(&pkt_buf[pkt_len], tcpopts, tcpopts_s);
  → Copies ONLY 24 bytes (indices 0-23)
  → Indices 24-36 are IGNORED!

pkt_len += tcpopts_s;
  → pkt_len += 24

================================================================================
RESULTING TCP PACKET STRUCTURE
================================================================================

Byte    Content                 Comment
----    -------                 -------
0-19    Standard TCP header     20 bytes (th structure)
20      0x02                    MSS kind
21      0x04                    MSS length
22-23   0x0400                  MSS value (1024, network order)
24      0x04                    SACK kind
25      0x02                    SACK length
26      0x13                    MD5 Signature kind
27      0x12                    MD5 length (18 bytes)
28-43   [16 random bytes]       MD5 hash data
44+     [IP payload]            Data starts here

                                ❌ MISSING:
                                   - Timestamps option (10 bytes)
                                   - NOP padding (3 bytes)

TCP Header Length: 44 bytes (Data Offset = 11)
Options Length: 24 bytes
Total TCP Header: 44 bytes (20 fixed + 24 options)

================================================================================
COMPARISON: CURRENT vs INTENDED vs CORRECT
================================================================================

                    CURRENT         INTENDED        CORRECT (FIX)
                    -------         --------        -------------
Bytes written       37              37              40
Bytes declared      24              37              40
Bytes sent          24              (crash)         40
Alignment           24%4=0✅        37%4=1❌        40%4=0✅
MSS                 ✅              ✅              ✅
SACK                ✅              ✅              ✅
MD5 Signature       ✅              ✅              ✅
Timestamps          ❌ dropped      ✅              ✅
NOP padding         ❌ dropped      ✅              ✅ (6 NOPs)
Total Options       24 bytes        37 bytes        40 bytes
TCP Data Offset     11 (44 bytes)   N/A (crash)     15 (60 bytes)

================================================================================
FIX RECOMMENDATION: PAD TO 40 BYTES
================================================================================

Index    Hex Data      Description                          Code Change
-----    --------      -----------                          -----------
 0-23    (unchanged)   MSS + SACK + MD5                     (keep existing)
24-33    (unchanged)   Timestamps                           (keep existing)
 34      01            NOP 1                                (keep existing)
 35      01            NOP 2                                (fix: remove dup)
 36      01            NOP 3                                (keep existing)
 37      01            NOP 4 (NEW)                          ADD THIS
 38      01            NOP 5 (NEW)                          ADD THIS
 39      01            NOP 6 (NEW)                          ADD THIS

Set: s->ss->tcpoptions_len = 40;

Result:
  ✅ 40 % 4 = 0 (passes validation)
  ✅ All 40 bytes sent in packet
  ✅ Maximum TCP options size (most evasive)
  ✅ Timestamps included (better stealth)
  ✅ TCP Data Offset = 15 (max value)

================================================================================
TCP DATA OFFSET EXPLAINED
================================================================================

The TCP Data Offset field is 4 bits wide and specifies the TCP header length
in units of 4-byte words (32-bit words).

Minimum:  5  (5 × 4 = 20 bytes, standard TCP header, no options)
Maximum: 15 (15 × 4 = 60 bytes, 20 byte header + 40 byte options)

Current bug:
  tcpoptions_len = 24
  Data Offset = (20 + 24) / 4 = 44 / 4 = 11
  TCP Header = 11 × 4 = 44 bytes

After fix:
  tcpoptions_len = 40
  Data Offset = (20 + 40) / 4 = 60 / 4 = 15
  TCP Header = 15 × 4 = 60 bytes (MAXIMUM!)

================================================================================
WHY THE AUTHOR'S COMMENT MAKES SENSE NOW
================================================================================

Comment (line 311):
  "i cant make this as big as i want, not sure where this is breaking"

What the author discovered:
  1. Added Timestamps (10 bytes) + NOPs (3 bytes) to existing 24 bytes
  2. Total = 37 bytes
  3. Set tcpoptions_len = 37
  4. Program crashed with "bad tcp option"
  5. Reason: 37 % 4 = 1 (not divisible by 4)
  6. Changed back to tcpoptions_len = 24
  7. Forgot to delete the code writing to indices 24-36!

What the author didn't realize:
  - Could have padded to 40 bytes (40 % 4 = 0)
  - 40 is the maximum TCP options size anyway
  - Would have satisfied both goals:
    * Include all desired options
    * Pass the divisibility check

The mystery is solved!

================================================================================
