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

not able to intercept java.lang package #1690

Closed
sagarvyass opened this issue Aug 8, 2024 · 9 comments
Closed

not able to intercept java.lang package #1690

sagarvyass opened this issue Aug 8, 2024 · 9 comments
Assignees
Labels
Milestone

Comments

@sagarvyass
Copy link

not able to intercept java.lang.Runtime

package com.security.aspect_example;

import net.bytebuddy.dynamic.loading.ClassInjector;
import net.bytebuddy.utility.JavaModule;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

import java.io.File;
import java.lang.instrument.Instrumentation;
import java.nio.file.Files;
import net.bytebuddy.agent.ByteBuddyAgent;
import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.ClassFileLocator;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.dynamic.loading.ClassInjector;
import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.implementation.bind.annotation.SuperCall;
import net.bytebuddy.matcher.ElementMatchers;

import java.io.File;
import java.lang.instrument.Instrumentation;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.security.ProtectionDomain;
import java.util.Collections;
import java.util.concurrent.Callable;


public class AspectExampleApplication {

	public static void main(String[] args) throws Exception {
		premain(null, ByteBuddyAgent.install());
		Runtime.getRuntime().exec("ls");
	
	}

	public static void premain(String arg, Instrumentation inst) throws Exception {
		File temp = Files.createTempDirectory("tmp").toFile();
		ClassInjector.UsingInstrumentation.of(temp, ClassInjector.UsingInstrumentation.Target.BOOTSTRAP, inst).inject(Collections.singletonMap(
				new TypeDescription.ForLoadedType(MyInterceptor.class),
				ClassFileLocator.ForClassLoader.read(MyInterceptor.class)));
		new AgentBuilder.Default()
				.ignore(ElementMatchers.nameStartsWith("net.bytebuddy."))

				.type(ElementMatchers.nameEndsWith("java.lang.Runtime"))
				.transform(new AgentBuilder.Transformer() {

					@Override
					public DynamicType.Builder<?> transform(DynamicType.Builder<?> builder, TypeDescription typeDescription, ClassLoader classLoader,JavaModule javaModule, ProtectionDomain protectionDomain) {
						return builder.method(ElementMatchers.named("getRuntime")).intercept(MethodDelegation.to(MyInterceptor.class));
					}
				}).installOn(inst);
	}
	public static class MyInterceptor {

		public static String intercept(@SuperCall Callable<String> zuper) throws Exception {
			System.out.println("Intercepted!");
			return zuper.call();
		}
	}

}

@raphw
Copy link
Owner

raphw commented Aug 9, 2024

You would need to enable retransformation using RetransformationStrategy.RETRANSFORM, and likely set disableClassFormatChanges(). Runtime will already be loaded when the JVM dispatches the agent.

@raphw raphw self-assigned this Aug 9, 2024
@raphw raphw added the question label Aug 9, 2024
@raphw raphw added this to the 1.14.18 milestone Aug 9, 2024
@sagarvyass
Copy link
Author

update to this, still not working

public static void premain(String arg, Instrumentation inst) throws Exception {
		File temp = Files.createTempDirectory("tmp").toFile();
		ClassInjector.UsingInstrumentation.of(temp, ClassInjector.UsingInstrumentation.Target.BOOTSTRAP, inst).inject(Collections.singletonMap(
				new TypeDescription.ForLoadedType(MyInterceptor.class),
				ClassFileLocator.ForClassLoader.read(MyInterceptor.class)));
		new AgentBuilder.Default()
				.with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION)
				.disableClassFormatChanges()
				.ignore(ElementMatchers.nameStartsWith("net.bytebuddy."))

				.type(ElementMatchers.nameEndsWith("java.lang.Runtime"))
				.transform(new AgentBuilder.Transformer() {

					@Override
					public DynamicType.Builder<?> transform(DynamicType.Builder<?> builder, TypeDescription typeDescription, ClassLoader classLoader,JavaModule javaModule, ProtectionDomain protectionDomain) {
						return builder.method(ElementMatchers.named("getRuntime")).intercept(MethodDelegation.to(MyInterceptor.class));
					}
				}).installOn(inst);
	}
	public static class MyInterceptor {

		public static String intercept(@SuperCall Callable<String> zuper) throws Exception {
			System.out.println("Intercepted!");
			return zuper.call();
		}
	}

@raphw
Copy link
Owner

raphw commented Aug 12, 2024

If you add a listener, you will see an error message. With retransofation, you cannot apply all forms of transformations. You will likely need to switch over to Advice.

@sagarvyass
Copy link
Author

would the advice be able to intercept core java packages like Runtime.exec() ?

@raphw
Copy link
Owner

raphw commented Aug 13, 2024

Certainly.

@sagarvyass
Copy link
Author

I have tried LTW using aspectJ weaver as a java agent, still couldn’t intercept. Is there some resource you could help with?

@raphw
Copy link
Owner

raphw commented Aug 13, 2024

If you look up YouTube and Vimeo, I have given a row of introductions to the topic. Looking at Byte Buddy's tests is also a good option.

@sagarvyass
Copy link
Author

can you please share the links?

@raphw
Copy link
Owner

raphw commented Aug 13, 2024

For example: https://www.youtube.com/watch?v=o9NVLXKRKeY

@raphw raphw closed this as completed Sep 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants