I see a common method of encoding in PHP malware where there's some bytes of key, which gets used to decode that many bytes of encoded text. Decoded bytes get appended to the key on the fly. The decoding is subtracting the key byte from the encoded byte, modulo 256. It seems like this method of encoding would be prone to a known-text attack, since the cleartext ends up used in the encoding key.
What is this method of encoding called? I have absolutely failed to google up anything other than more examples of the algorithm in PHP. The method seems similar to Running Key Ciphers, but it recycles the cleartext. It seems like what I've called cleartext is often compressed (gzip, gzdeflate, etc) so that the "cleartext" is close to random bytes.
When I've seen it, the variables in the decoding function are obfuscated into $l__l_
or $g___g__
, or variants with more or less underscores.
Here's an example of PHP malware that uses this encoding, if that helps.
The two variants of the algorithm I can readily find look like this:
$key = ...
$cleartext = ...
$i = 0;
do {
$cipherbyte = $ciphertext[$i];
$keybyte = $key[$i];
$value = (ord($cipherbyte) - ord($keybyte))%256;
$clearbyte = chr($value);
$cleartext[$i] = $clearbyte;
$key .= $clearbyte;
$i++;
} while ($i < strlen($ciphertext));
A more compact form shows up sometimes:
$key = ...
$cleartext = "";
for($i=0; $i<strlen($ciphertext); $i++) {
$cleartext[$i] = chr((ord($cleartext[$i])-ord($key[$i]))%256);
$key .= $cleartext[$i];
}
I believe that for the encoding function (which I've never seen), the key would get composed like this:
$key = $seed . $cleartext;
So the length of the key is always more bytes than the cleartext.
What's this method of encoding called?
S[S[i] + S[j]]
). I don't see any of those here. – duskwuff -inactive- Jan 23 '19 at 21:12