Skip to content

Commit 9ca1c06

Browse files
committed
modify decrypt javaagent subproject
1 parent 77e417e commit 9ca1c06

File tree

3 files changed

+72
-42
lines changed

3 files changed

+72
-42
lines changed

core/src/main/java/com/robin/core/encrypt/JarRepackager.java

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.robin.core.encrypt;
22

33

4+
import cn.hutool.core.io.FileUtil;
45
import com.robin.core.base.util.MavenUtils;
56
import com.robin.core.hardware.MachineIdUtils;
67
import javassist.ClassPool;
@@ -24,6 +25,7 @@
2425
@Slf4j
2526
public class JarRepackager {
2627
private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
28+
private static final char spacebyteVal=20;
2729
/**
2830
*
2931
* @param inputJarFiles 待加密的包
@@ -39,6 +41,7 @@ public static void repackage(String inputJarFiles,String outputJarFile,String ma
3941
List<String> dependencys= MavenUtils.getDepenendcyList(MavenUtils.getMavenRepository(),mavenSrcPath);
4042
JarMethodClearUtils.loadJars(pool,dependencys);
4143
JarMethodClearUtils.loadJars(pool,inputJarFiles);
44+
FileUtil.clean(basePath);
4245
try(JarInputStream inputStream=new JarInputStream(new FileInputStream(inputJarFiles));
4346
JarOutputStream outputStream=new JarOutputStream(new FileOutputStream(outputJarFile));
4447
ByteArrayOutputStream out1=new ByteArrayOutputStream();
@@ -48,6 +51,7 @@ public static void repackage(String inputJarFiles,String outputJarFile,String ma
4851
dout.write(CipherUtil.m_datapadding);
4952
dout.write(CipherUtil.hexStringToBytes(machineId.toUpperCase()));
5053
dout.writeLong(expireTs);
54+
5155
while((entry=inputStream.getNextEntry())!=null){
5256
if(!entry.isDirectory() && entry.getName().endsWith("class")){
5357

@@ -65,22 +69,19 @@ public static void repackage(String inputJarFiles,String outputJarFile,String ma
6569
byte[] keybytes=encryptByte(keystr.getBytes(),CipherUtil.getEncryptKey(machineId.toUpperCase().getBytes()));
6670
byte[] outbyte = encryptByte(bytes, keystr.getBytes());
6771
byte[] encrypted=encryptByte(outbyte,CipherUtil.getEncryptKey(machineId.toUpperCase().getBytes()));
68-
/* ByteArrayOutputStream output1=new ByteArrayOutputStream();
69-
70-
CipherUtil.decryptByte(machineId.getBytes(),new ByteArrayInputStream(encrypted),output1);
71-
72-
byte[] origin=CipherUtil.decryptByte(output1.toByteArray(),keystr.getBytes());*/
7372

7473
if(!packageName.equals("com.robin.spring") && !"JarMethodClearUtils".equals(clazzName) && !"JarRepackager".equals(clazzName) && !clazzName.contains("Hibernate")) {
7574
dout.write(CipherUtil.m_datapadding);
7675
dout.writeInt(classNameBytes.length);
7776
dout.write(classNameBytes);
7877
List<String> confusedNames=CipherUtil.getConfusedName(16,random);
79-
outputStream.putNextEntry(new JarEntry(basePath + confusedNames.get(0)));
80-
dout.write(confusedNames.get(1).getBytes());
78+
FileOutputStream fileOut=new FileOutputStream(basePath+confusedNames.get(0));
79+
//outputStream.putNextEntry(new JarEntry(basePath + confusedNames.get(0)));
80+
dout.write(indexToBytes(confusedNames.get(1)));
8181
dout.writeInt(keybytes.length);
8282
dout.write(keybytes);
83-
IOUtils.write(encrypted, outputStream);
83+
IOUtils.write(encrypted, fileOut);
84+
fileOut.close();
8485
System.out.println(packageName+"."+clazzName+"="+confusedNames.get(0)+"|"+confusedNames.get(1)+"|"+keystr);
8586
//方法体清理
8687

@@ -97,8 +98,10 @@ public static void repackage(String inputJarFiles,String outputJarFile,String ma
9798
}
9899
}
99100
if(dout!=null){
100-
outputStream.putNextEntry(new JarEntry("META-INF/config.bin"));
101-
IOUtils.write(out1.toByteArray(), outputStream);
101+
//outputStream.putNextEntry(new JarEntry("META-INF/config.bin"));
102+
FileOutputStream fileOut2=new FileOutputStream(basePath+"../config.bin");
103+
IOUtils.write(out1.toByteArray(), fileOut2);
104+
fileOut2.close();
102105
}
103106

104107
}catch (IOException ex){
@@ -136,8 +139,27 @@ private static byte[] decryptByte(byte[] bytes, byte[] key) {
136139
}
137140
return null;
138141
}
142+
private static byte[] indexToBytes(String index){
143+
byte[] outputBytes=new byte[index.length()];
144+
for(int i=0;i<index.length();i++){
145+
outputBytes[i]=(byte)((index.charAt(i)-spacebyteVal));
146+
}
147+
return outputBytes;
148+
}
149+
private static String bytesToIndex(byte[] bytes){
150+
StringBuilder builder=new StringBuilder();
151+
for(byte bytes1:bytes){
152+
builder.append((char)((int)bytes1+spacebyteVal));
153+
}
154+
return builder.toString();
155+
}
139156
public static void main(String[] args){
140-
repackage("E:/dev/workspaceframe/JavaFramework/core/target/core-1.0_proguard_base.jar","e:/tmp/encrypt.jar","E:/dev/workspaceframe/JavaFramework/core","META-INF/ext/", MachineIdUtils.getMachineId().replace("-","").toUpperCase(),System.currentTimeMillis()+365*3600*24*1000L);
141-
157+
repackage("E:/dev/core-obs.jar","E:/dev/maven/repository/com/robin/core-encrypt/1.0/core-encrypt-1.0.jar","E:/dev/workspaceframe/JavaFramework/core","E:/dev/workspaceframe/JavaFramework/decryptagent/src/main/resources/META-INF/ext/", MachineIdUtils.getMachineId().replace("-","").toUpperCase(),System.currentTimeMillis()+365*3600*24*1000L);
158+
/*Random random=new Random();
159+
List<String> strs=CipherUtil.getConfusedName(16,random);
160+
System.out.println(strs.get(1));
161+
byte[] bytes=indexToBytes(strs.get(1));
162+
System.out.println(bytes.length);
163+
System.out.println(bytesToIndex(bytes));*/
142164
}
143165
}

decryptagent/src/main/java/com/robin/agent/ClassTransformer.java

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
import java.io.*;
66
import java.lang.instrument.ClassFileTransformer;
77
import java.lang.instrument.IllegalClassFormatException;
8-
import java.security.NoSuchAlgorithmException;
98
import java.security.ProtectionDomain;
10-
import java.security.spec.InvalidKeySpecException;
119
import java.util.HashMap;
1210
import java.util.Map;
1311

@@ -16,17 +14,19 @@ public class ClassTransformer implements ClassFileTransformer {
1614
private Map<String, String> encryptKeyMap = new HashMap<>();
1715
private String machineCode;
1816
private Long expireTs;
19-
private static final String DEFAULTALGORITHM = "AES";
20-
private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
21-
public static final byte[] m_datapadding = {0x7F};
22-
public static final byte[] m_ending = {0x00};
23-
private static final String[] CONFUSEDSTRS = {"i", "I", "l", "O", "0", "1"};
17+
private final String DEFAULTALGORITHM = "AES";
18+
private final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
19+
private final byte[] m_datapadding = {0x7F};
2420

25-
public static final byte[] mzHeader = new byte[19];
21+
private final String[] CONFUSEDSTRS = {"i", "I", "l", "O", "0", "1"};
2622

27-
public ClassTransformer(){
23+
private final byte[] mzHeader = new byte[19];
24+
private final char spacebyteVal=20;
25+
26+
public ClassTransformer() {
2827
init();
2928
}
29+
3030
private void init() {
3131
try (DataInputStream dInput = new DataInputStream(getClass().getClassLoader().getResourceAsStream("META-INF/config.bin"))) {
3232
dInput.read(mzHeader);
@@ -38,7 +38,7 @@ private void init() {
3838
machineCode = bytesToHexString(machineCodeByte);
3939
expireTs = dInput.readLong();
4040
checkExpire();
41-
int classNameBytesLen ;
41+
int classNameBytesLen;
4242
while (dInput.available() > 0) {
4343
dInput.read(paddingbyte);
4444
checkPadding(paddingbyte);
@@ -47,20 +47,21 @@ private void init() {
4747
dInput.read(classNameEncryptBytes);
4848
byte[] decryptClassNameBytes = decryptByte(classNameEncryptBytes, machineCode.getBytes());
4949
String className = new String(decryptClassNameBytes);
50-
byte[] posByte=new byte[16];
50+
byte[] posByte = new byte[16];
5151
dInput.read(posByte);
52-
String confusedName = decodeConfusedNameByCode(new String(posByte));
52+
String confusedName = decodeConfusedNameByCode(posByte);
5353
int keyLength = dInput.readInt();
5454
byte[] keyEncryptByte = new byte[keyLength];
5555
dInput.read(keyEncryptByte);
5656
byte[] keyDecryptByte = decryptByte(keyEncryptByte, machineCode.getBytes());
5757
String key = new String(keyDecryptByte);
58-
encryptKeyMap.put(className, confusedName+"|"+key);
58+
encryptKeyMap.put(className, confusedName + "|" + key);
5959
}
6060
} catch (Exception ex) {
6161
ex.printStackTrace();
6262
}
6363
}
64+
6465
private void checkPadding(byte[] paddingbyte) {
6566
if (paddingbyte[0] != m_datapadding[0]) {
6667
System.err.println("jar package corrupted");
@@ -74,15 +75,15 @@ private void checkExpire() {
7475
System.exit(1);
7576
}
7677
}
78+
7779
private byte[] loadEncryptDataStream(String confusedName) throws IOException {
7880
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
7981
doCopy(getClass().getClassLoader().getResourceAsStream("META-INF/ext/" + confusedName), outputStream);
8082
return outputStream.toByteArray();
8183
}
8284

8385

84-
85-
private static void doCopy(InputStream is, OutputStream os) throws IOException {
86+
private void doCopy(InputStream is, OutputStream os) throws IOException {
8687
byte[] bytes = new byte[2048];
8788
int numBytes;
8889
while ((numBytes = is.read(bytes)) != -1) {
@@ -93,7 +94,7 @@ private static void doCopy(InputStream is, OutputStream os) throws IOException {
9394
is.close();
9495
}
9596

96-
private static byte[] decryptByte(byte[] bytes, byte[] key) {
97+
private byte[] decryptByte(byte[] bytes, byte[] key) {
9798
try {
9899
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
99100
cipher.init(Cipher.DECRYPT_MODE, toKey(key));
@@ -103,8 +104,14 @@ private static byte[] decryptByte(byte[] bytes, byte[] key) {
103104
}
104105
return null;
105106
}
106-
107-
private static String bytesToHexString(byte[] bytes) {
107+
private String bytesToIndex(byte[] bytes){
108+
StringBuilder builder=new StringBuilder();
109+
for(byte bytes1:bytes){
110+
builder.append((char)((int)bytes1+spacebyteVal));
111+
}
112+
return builder.toString();
113+
}
114+
private String bytesToHexString(byte[] bytes) {
108115
StringBuilder builder = new StringBuilder();
109116
for (byte b : bytes) {
110117
builder.append(String.format("%02X", b));
@@ -113,39 +120,40 @@ private static String bytesToHexString(byte[] bytes) {
113120
}
114121

115122

116-
private static SecretKeySpec toKey(byte[] keybyte) {
123+
private SecretKeySpec toKey(byte[] keybyte) {
117124
return new SecretKeySpec(keybyte, DEFAULTALGORITHM);
118125
}
119126

120-
private static String decodeConfusedNameByCode(String code) {
127+
private String decodeConfusedNameByCode(byte[] bytes) {
128+
String index=bytesToIndex(bytes);
121129
StringBuilder builder = new StringBuilder();
122-
for (char input : code.toCharArray()) {
123-
builder.append(CONFUSEDSTRS[Integer.parseInt(String.valueOf(input))-1]);
130+
for (char input : index.toCharArray()) {
131+
builder.append(CONFUSEDSTRS[Integer.parseInt(String.valueOf(input)) - 1]);
124132
}
125133
return builder.toString();
126134
}
127135

128136
@Override
129137
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
130138
try {
131-
String loadClass=className.replace("/",".");
139+
String loadClass = className.replace("/", ".");
132140
if (encryptKeyMap.containsKey(loadClass)) {
133-
//System.out.println("load class "+className);
134-
if(loadedClassPool.containsKey(loadClass)){
141+
System.out.println("load class "+className);
142+
if (loadedClassPool.containsKey(loadClass)) {
135143
return loadedClassPool.get(loadClass);
136144
}
137145
String[] arr = encryptKeyMap.get(loadClass).split("\\|");
138146
byte[] encryptBytes = loadEncryptDataStream(arr[0]);
139-
byte[] machineDecr=decryptByte(encryptBytes,machineCode.getBytes());
140-
byte[] decryptByte=decryptByte(machineDecr,arr[1].getBytes());
147+
byte[] machineDecr = decryptByte(encryptBytes, machineCode.getBytes());
148+
byte[] decryptByte = decryptByte(machineDecr, arr[1].getBytes());
141149
if (decryptByte != null && decryptByte[0] == -54 && decryptByte[1] == -2 && decryptByte[2] == -70 && decryptByte[3] == -66) {
142-
loadedClassPool.put(loadClass,decryptByte);
150+
loadedClassPool.put(loadClass, decryptByte);
143151
return decryptByte;
144-
}else{
152+
} else {
145153
throw new RuntimeException("decrypt error!");
146154
}
147155
}
148-
}catch (Exception ex){
156+
} catch (Exception ex) {
149157
ex.printStackTrace();
150158
}
151159
return classfileBuffer;

decryptagent/src/main/resources/META-INF/ext/README

Whitespace-only changes.

0 commit comments

Comments
 (0)