| 1 | #!/usr/bin/perl -w | ||
| 2 | # Build chain-of-custody test vectors into tests/expected/. Each forged | ||
| 3 | # message has a TOP instance (m=2) whose hashes are correct AND whose signature | ||
| 4 | # is valid; only the recipe lies, so reconstructing m=1 fails its hash check. | ||
| 5 | # Consumed by t/fraud-detection.t; also used as hand-off vectors for hosted | ||
| 6 | # verifiers (socketlabs, dkim2.eu). | ||
| 7 | # | ||
| 8 | # chain-fraud-control.eml honest recipe; whole chain validates. | ||
| 9 | # chain-fraud-header.eml recipe omits an added Reply-To (header lie). | ||
| 10 | # chain-fraud-body.eml body changed, recipe declares no body change. | ||
| 11 | # chain-fraud-body-truncate.eml innocent-looking "drop trailing block" copy- | ||
| 12 | # range recipe that hides an edited first line. | ||
| 13 | # chain-fraud-mi-rewrite.eml content replaced wholesale; the recipe | ||
| 14 | # declares no content change but rewrites | ||
| 15 | # the m=1 Message-Instance to one hashed over | ||
| 16 | # the new content (a forged history). | ||
| 17 | # chain-fraud-mi-rewrite-wsp.eml the same under the key "message-instance " | ||
| 18 | # (not a field name: invalid JSON). | ||
| 19 | # | ||
| 20 | # All signed d=test1.dkim2.com s=sel1 (real DNS published) with NO | ||
| 21 | # Authentication-Results / Delivered-To, so the only thing that can trip a | ||
| 22 | # verifier is the recipe reconstruction. | ||
| 23 | |||
| 24 | use 5.020; | ||
| 25 | use strict; | ||
| 26 | use Path::Tiny; | ||
| 27 | use Email::MIME; | ||
| 28 | use lib 'lib'; | ||
| 29 | use Mail::DKIM2::MessageInstance; | ||
| 30 | use Mail::DKIM2::Signer; | ||
| 31 | use JSON::PP; | ||
| 32 | |||
| 33 | sub crlf { my $s = shift; $s =~ s/\r?\n/\r\n/g; return $s } | ||
| 34 | |||
| 35 | # ---- Original message (state O) -------------------------------------------- | ||
| 36 | my $body = crlf(<<'EOB'); | ||
| 37 | Please reply to me directly with your thoughts. | ||
| 38 | EOB | ||
| 39 | |||
| 40 | my $orig = Email::MIME->new(crlf(<<'EOH') . "\r\n" . $body); | ||
| 41 | Date: Tue, 08 Jul 2026 10:00:00 +0000 | ||
| 42 | To: user@test3.dkim2.com | ||
| 43 | From: alice@test1.dkim2.com | ||
| 44 | Subject: chain-of-custody forgery test | ||
| 45 | Message-Id: <orig.20260708@test1.dkim2.com> | ||
| 46 | EOH | ||
| 47 | |||
| 48 | # m=1 Message-Instance over the original. | ||
| 49 | my $mi1 = Mail::DKIM2::MessageInstance->calculate($orig); | ||
| 50 | (my $mi1s = "Message-Instance: " . $mi1->as_string()) =~ s/^Message-Instance: //; | ||
| 51 | $orig->header_raw_prepend('Message-Instance', $mi1s); | ||
| 52 | # $orig now == "O with m=1 MI" (this is our chain-hop1 / previous state). | ||
| 53 | |||
| 54 | # ---- Modified message (state C): a hop adds List-Id and Reply-To ----------- | ||
| 55 | sub make_current { | ||
| 56 | my $c = Email::MIME->new($orig->as_string); | ||
| 57 | # header_raw_prepend puts these at the top; they are new (absent from O). | ||
| 58 | $c->header_raw_prepend('Reply-To', 'attacker@evil.example'); | ||
| 59 | $c->header_raw_prepend('List-Id', '<test.list.test1.dkim2.com>'); | ||
| 60 | return $c; | ||
| 61 | } | ||
| 62 | |||
| 63 | sub sign_and_write { | ||
| 64 | my ($current, $file) = @_; | ||
| 65 | my $signer = Mail::DKIM2::Signer->new( | ||
| 66 | Domain => 'test1.dkim2.com', | ||
| 67 | Selector => 'sel1', | ||
| 68 | KeyFile => '../keys/sel1._domainkey.test1.dkim2.com.pem', | ||
| 69 | Algorithm=> 'rsa-sha256', | ||
| 70 | Timestamp=> 1783372800, | ||
| 71 | MailFrom => 'bounce@test1.dkim2.com', | ||
| 72 | RcptTo => ['user@test3.dkim2.com'], | ||
| 73 | ); | ||
| 74 | $signer->PRINT($current->as_string()); | ||
| 75 | $signer->CLOSE; | ||
| 76 | (my $sig = $signer->as_string()) =~ s/^DKIM2-Signature:\s*//; | ||
| 77 | $current->header_raw_prepend('DKIM2-Signature', $sig); | ||
| 78 | path("tests/expected/$file")->spew_raw($current->as_string()); | ||
| 79 | say "wrote tests/expected/$file"; | ||
| 80 | } | ||
| 81 | |||
| 82 | # ---- CONTROL: honest recipe ------------------------------------------------ | ||
| 83 | { | ||
| 84 | my $c = make_current(); | ||
| 85 | my $mi2 = Mail::DKIM2::MessageInstance->calculate($c, $orig); | ||
| 86 | say "honest recipe rh keys: ", join(",", sort keys %{ $mi2->{bits}{rh} // {} }); | ||
| 87 | (my $s = "Message-Instance: " . $mi2->as_string()) =~ s/^Message-Instance: //; | ||
| 88 | $c->header_raw_prepend('Message-Instance', $s); | ||
| 89 | sign_and_write($c, 'chain-fraud-control.eml'); | ||
| 90 | } | ||
| 91 | |||
| 92 | # ---- FRAUD (header): lying recipe drops the reply-to entry ----------------- | ||
| 93 | { | ||
| 94 | my $c = make_current(); | ||
| 95 | my $mi2 = Mail::DKIM2::MessageInstance->calculate($c, $orig); | ||
| 96 | delete $mi2->{bits}{rh}{'reply-to'}; # <-- the lie: undeclared Reply-To | ||
| 97 | say "fraud-header recipe rh keys: ", join(",", sort keys %{ $mi2->{bits}{rh} // {} }); | ||
| 98 | (my $s = "Message-Instance: " . $mi2->as_string()) =~ s/^Message-Instance: //; | ||
| 99 | $c->header_raw_prepend('Message-Instance', $s); | ||
| 100 | sign_and_write($c, 'chain-fraud-header.eml'); | ||
| 101 | } | ||
| 102 | |||
| 103 | # ---- FRAUD (body): body content changed, recipe claims it was NOT ---------- | ||
| 104 | # A hop rewrites the body (e.g. swaps payment details) but declares no body | ||
| 105 | # recipe, so reconstructing m=1 leaves the tampered body and its body hash no | ||
| 106 | # longer matches the signed m=1 body hash. The "ideal" forgery: the top MI | ||
| 107 | # body hash is genuinely correct for the tampered body AND validly signed; | ||
| 108 | # only the recipe lies about the body being unchanged. | ||
| 109 | { | ||
| 110 | my $c = Email::MIME->new($orig->as_string); # O + m=1 MI | ||
| 111 | $c->body_set(crlf("Please send payment to account EVIL-9999 instead.\r\n")); | ||
| 112 | my $mi2 = Mail::DKIM2::MessageInstance->calculate($c, $orig); | ||
| 113 | # calculate() produced a body recipe (rb) that would rebuild the old body. | ||
| 114 | # The lie: delete it so the recipe claims the body never changed. | ||
| 115 | delete $mi2->{bits}{rb}; | ||
| 116 | say "fraud-body has body recipe after tamper: ", | ||
| 117 | (exists $mi2->{bits}{rb} ? "yes" : "no (lie)"); | ||
| 118 | (my $s = "Message-Instance: " . $mi2->as_string()) =~ s/^Message-Instance: //; | ||
| 119 | $c->header_raw_prepend('Message-Instance', $s); | ||
| 120 | sign_and_write($c, 'chain-fraud-body.eml'); | ||
| 121 | } | ||
| 122 | |||
| 123 | # ---- FRAUD (body, sneaky): truncating copy-range recipe -------------------- | ||
| 124 | # The recipe DOES describe a body change (so "changed hash but no body recipe" | ||
| 125 | # is not enough to catch it): it says "the trailing signature/footer block was | ||
| 126 | # added, drop it to recover the previous body" -> rb = [ {"c":[1,1]} ]. That | ||
| 127 | # is exactly what an honest footer-append would look like. The lie: the hop | ||
| 128 | # ALSO rewrote line 1 (GOOD -> EVIL). Copying current line 1 reproduces the | ||
| 129 | # tampered line, so the reconstructed m=1 body hash != the signed m=1 hash. | ||
| 130 | # Only a verifier that actually APPLIES the recipe and re-hashes notices; one | ||
| 131 | # that trusts a plausible-looking recipe does not. The appended sig block is | ||
| 132 | # the cover story — the signature the recipe "cuts off". | ||
| 133 | { | ||
| 134 | my $o2 = Email::MIME->new(crlf(<<'EOH2') . "\r\n" . crlf("Pay account GOOD-0001\r\n")); | ||
| 135 | Date: Tue, 08 Jul 2026 10:00:00 +0000 | ||
| 136 | To: user@test3.dkim2.com | ||
| 137 | From: alice@test1.dkim2.com | ||
| 138 | Subject: invoice | ||
| 139 | Message-Id: <inv.20260708@test1.dkim2.com> | ||
| 140 | EOH2 | ||
| 141 | my $m1 = Mail::DKIM2::MessageInstance->calculate($o2); | ||
| 142 | (my $m1s = "Message-Instance: " . $m1->as_string()) =~ s/^Message-Instance: //; | ||
| 143 | $o2->header_raw_prepend('Message-Instance', $m1s); | ||
| 144 | |||
| 145 | my $c = Email::MIME->new($o2->as_string); | ||
| 146 | # attacker rewrites line 1 AND appends a plausible signature block: | ||
| 147 | $c->body_set(crlf("Pay account EVIL-9999\r\n-- \r\nAlice <alice\@test1.dkim2.com>\r\n")); | ||
| 148 | my $mi2 = Mail::DKIM2::MessageInstance->calculate($c, $o2); | ||
| 149 | # Overwrite the honest diff with the innocent-looking "drop the footer" | ||
| 150 | # recipe: copy only current line 1. h1/b1 (hashes of the delivered body) | ||
| 151 | # stay correct, so the TOP MI still passes. | ||
| 152 | $mi2->{bits}{rb} = [ [1, 1] ]; | ||
| 153 | delete $mi2->{bits}{rh}; # no header changes to declare | ||
| 154 | say "fraud-body-truncate rb = [[1,1]] (\"drop trailing signature block\")"; | ||
| 155 | (my $s = "Message-Instance: " . $mi2->as_string()) =~ s/^Message-Instance: //; | ||
| 156 | $c->header_raw_prepend('Message-Instance', $s); | ||
| 157 | sign_and_write($c, 'chain-fraud-body-truncate.eml'); | ||
| 158 | } | ||
| 159 | |||
| 160 | # ---- FRAUD (history): a header Recipe that rewrites Message-Instance -------- | ||
| 161 | # The hop replaces headers and body wholesale, declares no content change, and | ||
| 162 | # instead names message-instance in its header Recipe with a literal m=1 hashed | ||
| 163 | # over the NEW content. A verifier that re-reads the instances from the rebuilt | ||
| 164 | # message checks m=1 against the forgery and reports the original as unchanged. | ||
| 165 | # Message-Instance and DKIM2-Signature fields are never content (-09 section | ||
| 166 | # 6.2): a Recipe naming them acts on content fields of that name, of which | ||
| 167 | # there are none, so the received m=1 still governs and its header hash fails. | ||
| 168 | { | ||
| 169 | my $c = Email::MIME->new(crlf(<<'EOH3') . "\r\n" . crlf("Please wire \$1M to account 666\r\n")); | ||
| 170 | Date: Tue, 08 Jul 2026 10:00:00 +0000 | ||
| 171 | To: victim@example.org | ||
| 172 | From: alice@test1.dkim2.com | ||
| 173 | Subject: URGENT wire money | ||
| 174 | Message-Id: <evil.20260708@test1.dkim2.com> | ||
| 175 | EOH3 | ||
| 176 | my $forged = Mail::DKIM2::MessageInstance->calculate($c)->as_string; | ||
| 177 | $c->header_raw_prepend('Message-Instance', $mi1s); # the genuine m=1 | ||
| 178 | my $mi2 = Mail::DKIM2::MessageInstance->calculate($c, $c); | ||
| 179 | delete $mi2->{bits}{rb}; | ||
| 180 | $mi2->{bits}{rh} = { 'message-instance' => [ " $forged" ] }; | ||
| 181 | say "fraud-mi-rewrite rh = { message-instance => [forged m=1] }"; | ||
| 182 | (my $s = "Message-Instance: " . $mi2->as_string()) =~ s/^Message-Instance: //; | ||
| 183 | my $c2 = Email::MIME->new($c->as_string); | ||
| 184 | $c->header_raw_prepend('Message-Instance', $s); | ||
| 185 | sign_and_write($c, 'chain-fraud-mi-rewrite.eml'); | ||
| 186 | |||
| 187 | # The same under a key with WSP in it, "message-instance ": no field | ||
| 188 | # name, so invalid JSON -- where name matching that drops WSP before a | ||
| 189 | # colon would take it for Message-Instance. | ||
| 190 | $mi2->{bits}{rh} = { 'message-instance ' => [ " $forged" ] }; | ||
| 191 | (my $s2 = "Message-Instance: " . $mi2->as_string()) =~ s/^Message-Instance: //; | ||
| 192 | $c2->header_raw_prepend('Message-Instance', $s2); | ||
| 193 | sign_and_write($c2, 'chain-fraud-mi-rewrite-wsp.eml'); | ||
| 194 | } |