Java Runtime DLL injection - java

I was wondering if there is a Java method for injecting code into a process during runtime.
The key term; in Java. I have found alot of references to this (the most useful being at this website). The problem is that that all the references I've found do not use Java, and hence are unfavorable for my current situation.
Could anyone help me out, and perhaps point me in the right direction?

You can use Java Native Interface to bind CreateRemoteThread() API to Java.

The closest I know of is System.loadLibrary. That method will load a library by name in a system dependent way.

One way would be to load the non java library you found using Java Native Access. I don't really see a way you will be able to do this without calling native code so unless a some other java developer has already created a JNI or JNA wrapper you will probably have to do it yourself.

Related

How do classes in the Java standard API interact with the OS?

I've been trying to find an answer to this for some time, but I think part of my problem is that I don't really know how to phrase my question. I understand the that JVM ultimately preforms all the system calls at run-time that the Java program needs to make, my confusion is about the underlying way that Java classes tell the JVM to do this.
Take for example, the File class in the standard Java library. As far as I know, this is considered the most fundamental API for opening/creating files in Java. But, File is just another class right? So in theory I should be able to write my own File class from scratch which doesn't utilize the pre-exisitng one, right? How would I do that? What is happening inside the File class that tells the VM to actually create the file? I looked at the source code for the File class, and it looks like it calls another class called VMFile, but I could find no explanation of what VMFile is. When I looked at the VMFile source code, it just had function declarations with no definitions.
Thank you for your help.
The Java Native Interface (JNI) is the glue between Java classes and the OS. Native methods have a 'native' attribute (look it up in the JLS).

call method from DLL in java7

From what I understood dll are not standardized. thus one cannot just call something in a dll.
However I found this :http://johannburkard.de/software/nativecall/
This library allow you to call any method from a dll in java, so it seems that you can call any method in a dll.
but it was done for 32 bit system, thus I cannot use it.
I have this dll, autohotkey.dll, I know there is the method "ahkExec" inside which take a String as parameter.
Is this really not possible to run it from java without using some kind of c++ magic?
Thanks.
ps : here is how it is done with nativeCall : https://gist.github.com/brigand/1526712
You've been able to call DLLs in Java since version 1.0 using Java Native Interface (JNI).
There is no magic in invoking external methods but you have to follow some rules based on what JNI provides .
If you need to use one function from library you could write a specific Wrapper-class like in this tutorial
For more tricky things better to work with SWIG

How to analyze method calls and objects of other classes used in a java class programmatically

i need to detect if a class relies on another class programatically,to detect inappropriate intimacy code smell(i want to analyze other java programs ,using my program).Any directions on
how to achieve this will be a great help.
And
How to identify all the objects created in a java program?
How to identify all the called methods in a java program?
Any help would be appreciated.
You might want to use what's already there instead of building something yourself. Especially if you're not very familiar with the internals of Java and the JVM.
Have a look at JDepend: http://clarkware.com/software/JDepend.html
Use a profiler as JConsole or VisualVM. With the use of profilers you can pretty much see everything that happens at runtime.
One way i think of is using logger, Put some log statement in the construct and in the methods you want to monitor. So through logs you can find out the objects created and methods accessed
I have found very useful the ObjectWeb asm-all Java bytecode manipulation and analysis library, also known as asm-all.jar
It allows you to convert any *.jar application into equivalent XML file. You can fully inspect the application structure, change it in the XML format and convert back into *.jar file
In order to use the XML files you'll need to understand what it contains. Oracle's The Java® Virtual Machine Specification is very good reference to start with
BTW: one thing you can do with this tool is to instrument the bytecode so that it creates runtime profiling information - which methods were called and by whom (as suggested by #upog)

How to include Header File (.h) in Java

I want to use this which says to use a particular method I have to include tcutil.h in my java code. Can anybody help me, how to do that ? Another point: we can easily create an header file and include it in to C code but why reverse is so hard (means lots of work have to do) ? May be stupid, but little bit hints will be helpful.
This might be more complicated than you think. The .h files are C language include files which usually describe the methods and data structures of a C-library. You would have to Java Native Interface (JNI) in order to include these libraries in your java code.
You have basically two options
Go through a tutorial like this
Or look for a java implementation.
There're already java-bindings available.
You can't include C/C++ headers into java source code.
Maybe you want to define a native implementation for a java method.
http://java.sun.com/docs/books/jni/
They seem to have a Java API, which you need to download and include in your classpath. You can't include a C header file in Java.
you cannot do it directly in java. You have to include the header files in your C program and use JNI to call the functions that you want to use.
Refer this : JNI reference
To run native code from Java, you need using JNI technology. Try http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jni.html or http://docs.oracle.com/javase/6/docs/technotes/guides/jni/spec/functions.html of google by keywords "JNI, tutorial".
The page mentions that there is an API for Java available, but does not show it. You should ask them for the Java documentation. Preferrably, the API should be a JDBC driver.

Programmatically inspect .class files

I'm working on a project where we're doing a lot of remote object transfer between a Java service and clients written in other various languages. Given our current constraints I've decided to see what it would take to generate code based on an existing Java class. Basically I need to take a .class file (or a collection of them) parse the bytecode to determine all of the data members and perhaps getters/setters and then write something that can output code in a different language to create a class with the same structure.
I'm not looking for standard decompilers such as JAD. I need to be able to take a .class file and create an object model of its data members and methods. Is this possible at all?
I've used BCEL and find it really quite awkward. ASM is much better. It very extensively uses visitors (which can be a little confusing) and does not create an object model. Not creating an object model turns out to be a bonus, as any model you do want to create is unlikely to look like a literal interpretation of all the data.
I have used BCEL in the past and it was pretty easy to use. It was a few years ago so there may be something better now.
Apache Jakarta BCEL
From your description, it sounds like simple reflection would suffice. You can discover all of the static structure of the class, as well as accessing the fields of a particular instance.
I would only move on to BCEL if you are trying to translate method instructions. (And if that's what you're trying to automate, good luck!)
I'm shocked that no one has mentioned ASM yet. It's the best bytecode library your money can buy. Well, ok it's free.
JAD is a java decompiler that doesn't allow programmatic access. It isn't readily available anymore, and probably won't work for newer projects with Java7 bytecodes.
I think javassist might help you too.
http://www.jboss.org/javassist/
I have never had the need of using it, but if you give it a try, would you let us know your comments about it?
Although I think it is more for bytecode manipulation than .class inspection.

Categories