Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用agentArg的方式传入应用的lib路径 #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/main/java/io/zhile/crack/atlassian/agent/Agent.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,16 @@
*/
public class Agent {
public static void premain(String args, Instrumentation inst) {

System.out.println("====================================================");
System.out.println("======= Atlassian Crack Agent =======");
System.out.println("====================================================");

//noinspection UnnecessaryLocalVariable
final String appLibPath = args;
System.out.printf("application lib: %s \n", appLibPath);
try {
inst.addTransformer(new KeyTransformer());
inst.addTransformer(new KeyTransformer(appLibPath));
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down
26 changes: 20 additions & 6 deletions src/main/java/io/zhile/crack/atlassian/agent/KeyTransformer.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public class KeyTransformer implements ClassFileTransformer {

private static final String LICENSE_DECODER_PATH = "com/atlassian/extras/decoder/v2/Version2LicenseDecoder";
private static final String LICENSE_DECODER_CLASS = "com.atlassian.extras.decoder.v2.Version2LicenseDecoder";
private final String libPath;

public KeyTransformer(String libPath) {
this.libPath = libPath;
}

@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
Expand All @@ -28,8 +33,14 @@ public byte[] transform(ClassLoader loader, String className, Class<?> classBein

if (className.equals(CN_KEY_SPEC)) {
return handleKeySpec();
} else if(className.equals(LICENSE_DECODER_PATH)) {
return handleLicenseDecoder();
}

if (libPath == null){
return classfileBuffer;
}

if (className.equals(LICENSE_DECODER_PATH)) {
return handleLicenseDecoder(libPath);
}

return classfileBuffer;
Expand Down Expand Up @@ -66,11 +77,14 @@ private byte[] handleKeySpec() throws IllegalClassFormatException {
* @return 修改过的类的字节码
* @throws IllegalClassFormatException 当某些地方出问题了就会抛出这个异常
*/
private byte[] handleLicenseDecoder() throws IllegalClassFormatException {
private byte[] handleLicenseDecoder(String libFilePath) throws IllegalClassFormatException {
try {
// 我不知道怎么从 com.atlassian.bitbucket.internal.launcher.BitbucketServerLauncher 读取这个路径,所以我直接 HARD CODE
// Forgive me pls...
File libs = new File("/opt/atlassian/bitbucket/7.21.0/app/WEB-INF/lib");
File libs = new File(libFilePath);
if (!libs.exists()) {
System.err.printf("path[%s]not found, please check the parameters.\n", libFilePath);
}else if (!libs.isDirectory()){
System.err.printf("path[%s]not a correct directory, please check the parameters.\n", libFilePath);
}
ClassPool cp = ClassPool.getDefault();

Arrays.stream(Objects.requireNonNull(libs.listFiles())).map(File::getAbsolutePath).forEach((it) -> {
Expand Down