Search for a String inside all files inside a war - java

Where I work someone had Java project and used Eclipse to export that project into a .war package.
Now I need to search for a string inside all the files that make that .war package. I know that a .war package is just a .zip file, and I have extracted its contents, however, now I have bunch of of .class java files (among images, xmls and other stuff) and I have no idea on how to search strings inside them.
I am a Linux Mint user, so I tried using the "grep -R stringHere ." command without success (I am not an advanced user), this command only searches inside text files.
I also searched and found the crgrep project but it is currently bugged and it does not work.
Does anyone know a linux command that can search inside all the contents of a .war package and check if those contents contain a specific string or code sample?
I really need to find a way to search through the content of the .class files. That is my only priority so far. I don't care about images nor about any other type of text files.
It would help a lot.
Thanks in advance, Pedro.

Unzip your war like any regular zip and from the root search with
find . -name "*.class" -print0 |xargs -0 strings -f |grep -i <your string>
Pray that your string got encoded in clear text inside the binaries.

This is quite simple, you can use something like below.
find /path -name "*.*" -exec grep -l "string-to-search"{} \;
If you need to search by ignoring case, you can use the -i along -l.
You can read further here.

I would recommend you to use jad.
Once installed, the following command should work just fine:
find . -name "*.class" | xargs jad -p | grep "your string"

Using find command:
find mywar.war -name '*.class' -exec strings -f "{}" + | grep "mysearchstringhere"

It is not a java question.
You can however use strings command:
find . -name "*.class" | xargs strings | grep myString
It will probably find string that you are looking for. But why are you looking for string in compiled binary file? What kind of string are you expecting to locate? If this is your code why not to search for strings in the source code, i.e. java files?

The .war file do not have the source code within them. You will see compiled .class files. This can't be read or you won't find anything meaningful in them.
From your question, I think you will have to use a decompiler to convert the .class to .java files. I would suggest the following options.
Cavaj, (no support for WIN 7 + ) .. here..
jad, (my personal favourite).. here
The advantage with jad is that it comes with a neat eclipse plug in .. download it here
So once you have decompiled the .class files, you can use any tools ( like grep ) to search for the string..

Related

Parse tree building with ANTLR and Java

Hello I need some help regarding building a simple parse tree with antlr and java .
I have tried to work with powershell to compile and run the grammar file ( which is the pascal.g4 file ) and I was hoping to generate some java files out of it , however at times when I tried to run it using the command " .\antlr.bat -package pdl -o pdl .\pascal.g4 " I get a message on the powershell
stating " The system cannot find the file specified " .
I think the command I typed in might be wrong but anyway I have got the grammar file and I have got the .bat files ( grun.bat , java.bat & javac.bat ) and the antlr-4.5-complete.jar file .
I think these are basically all the files you need to build the parse tree .
As for the folder location where all these files are located is C:\Users\Lenovo\Documents\test2
I have looked at some examples from others however I really now cannot go any further , can anybody please help me out here to build this .
Thank you
That batch file is little more than a hint.
Try being explicit with the paths and arguments -- and without trying to coerce Powershell into passing arguments in a manner compatible with batchfile execution.
Something like this:
#echo off
rem Execute the Antlr compiler/generator tool
SETLOCAL
set CLASSPATH=C:\Users\Lenovo\Documents\test2\antlr-4.5-complete.jar;%CLASSPATH%
cd /d C:\Users\Lenovo\Documents\test2\
"C:\Program Files\Java\jre8\bin\java.exe" org.antlr.v4.Tool pascal.g4
timeout 4
Here's the one-liner version I use, adopted from The Definitive ANTLR 4 Reference to use a relative reference to the directory containing the .CMD/.BAT file...
#java -cp %~dp0..\ClassLib\antlr-4.5-complete.jar;%CLASSPATH% org.antlr.v4.Tool %*
Use of %~dp0 helps avoid hardcoding an absolute path in it...

How to use sed command to change java package declaration in batch way

Previously, my java package declarations all start with 'zn' like "package zn.tree". Now after I changed the java folder path to remove 'zn' folder, I also want to change all java declarations to remove the prefix 'zn.' in a batch way. For example, I want change "package zn.tree" to "package tree".
So I decided to use bash script to change all the java files. After I googled, I wrote a script to use 'sed' command to do this job. However, it doesn't work and reports error. I still don't get familiar with the regex in 'sed'. My code is shown below:
#! /bin/bash
# change package declaration in batch way
for path in $(find $1 -type f -name "*.java"); do
sed -i "" 's/\<zn/.\>//g' $path
done
Hope someone could give me some clues. Thank you.
If I understand your question, you could do it with a one liner like,
find . -type f -name '*.java' -exec sed -i 's/^package zn\./package /1' {} \;
That will execute the sed command and instruct it to edit in-place on every matching file. Note that I assume you want to match the first line starting with "package zn." and replace it with "package " once.
Using the refactoring feature of an IDE such as Eclipse, IntelliJ or Netbeans will save you a lot of time for those needs. In IntelliJ, you would just create your new package and drag-and-drop your classes from the old package to the new one.

How to get names of classes inside a jar file?

I have a JAR file and I need to get the name of all classes inside this JAR file. How can I do that?
I googled it and saw something about JarFile or Java ClassLoader but I have no idea how to do it.
You can use Java jar tool. List the content of jar file in a txt file and you can see all the classes in the jar.
jar tvf jarfile.jar
-t list table of contents for archive
-v generate verbose output on standard output
-f specify archive file name
Unfortunately, Java doesn't provide an easy way to list classes in the "native" JRE. That leaves you with a couple of options: (a) for any given JAR file, you can list the entries inside that JAR file, find the .class files, and then determine which Java class each .class file represents; or (b) you can use a library that does this for you.
Option (a): Scanning JAR files manually
In this option, we'll fill classNames with the list of all Java classes contained inside a jar file at /path/to/jar/file.jar.
List<String> classNames = new ArrayList<String>();
ZipInputStream zip = new ZipInputStream(new FileInputStream("/path/to/jar/file.jar"));
for (ZipEntry entry = zip.getNextEntry(); entry != null; entry = zip.getNextEntry()) {
if (!entry.isDirectory() && entry.getName().endsWith(".class")) {
// This ZipEntry represents a class. Now, what class does it represent?
String className = entry.getName().replace('/', '.'); // including ".class"
classNames.add(className.substring(0, className.length() - ".class".length()));
}
}
Option (b): Using specialized reflections libraries
Guava
Guava has had ClassPath since at least 14.0, which I have used and liked. One nice thing about ClassPath is that it doesn't load the classes it finds, which is important when you're scanning for a large number of classes.
ClassPath cp=ClassPath.from(Thread.currentThread().getContextClassLoader());
for(ClassPath.ClassInfo info : cp.getTopLevelClassesRecurusive("my.package.name")) {
// Do stuff with classes here...
}
Reflections
I haven't personally used the Reflections library, but it seems well-liked. Some great examples are provided on the website like this quick way to load all the classes in a package provided by any JAR file, which may also be useful for your application.
Reflections reflections = new Reflections("my.project.prefix");
Set<Class<? extends SomeType>> subTypes = reflections.getSubTypesOf(SomeType.class);
Set<Class<?>> annotated = reflections.getTypesAnnotatedWith(SomeAnnotation.class);
Maybe you are looking for jar command to get the list of classes in terminal,
$ jar tf ~/.m2/repository/org/apache/spark/spark-assembly/1.2.0-SNAPSHOT/spark-assembly-1.2.0-SNAPSHOT-hadoop1.0.4.jar
META-INF/
META-INF/MANIFEST.MF
org/
org/apache/
org/apache/spark/
org/apache/spark/unused/
org/apache/spark/unused/UnusedStubClass.class
META-INF/maven/
META-INF/maven/org.spark-project.spark/
META-INF/maven/org.spark-project.spark/unused/
META-INF/maven/org.spark-project.spark/unused/pom.xml
META-INF/maven/org.spark-project.spark/unused/pom.properties
META-INF/NOTICE
where,
-t list table of contents for archive
-f specify archive file name
Or, just grep above result to see .classes only
$ jar tf ~/.m2/repository/org/apache/spark/spark-assembly/1.2.0-SNAPSHOT/spark-assembly-1.2.0-SNAPSHOT-hadoop1.0.4.jar | grep .class
org/apache/spark/unused/UnusedStubClass.class
To see number of classes,
jar tvf launcher/target/usergrid-launcher-1.0-SNAPSHOT.jar | grep .class | wc -l
61079
This is a hack I'm using:
You can use java's autocomplete like this:
java -cp path_to.jar <Tab>
This will give you a list of classes available to pass as the starting class. Of course, trying to use one that has no main file will not do anything, but you can see what java thinks the classes inside the .jar are called.
You can try:
jar tvf jarfile.jar
This will be helpful only if your jar is executable i.e. in manifest you have defined some class as main class
You can use the
jar tf example.jar
Below command will list the content of a jar file.
command :- unzip -l jarfilename.jar.
sample o/p :-
Archive: hello-world.jar
Length Date Time Name
--------- ---------- ----- ----
43161 10-18-2017 15:44 hello-world/com/ami/so/search/So.class
20531 10-18-2017 15:44 hello-world/com/ami/so/util/SoUtil.class
--------- -------
63692 2 files
According to manual of unzip
-l list archive files (short format). The names, uncompressed file sizes and modification dates and times of the specified files are
printed, along with totals for all
files specified. If UnZip was compiled with OS2_EAS defined, the -l option also lists columns for the sizes of stored OS/2
extended attributes (EAs) and OS/2 access
control lists (ACLs). In addition, the zipfile comment and individual file comments (if any) are displayed. If a file was
archived from a single-case file system
(for example, the old MS-DOS FAT file system) and the -L option was given, the filename is converted to lowercase and is
prefixed with a caret (^).
Mac OS:
On Terminal:
vim <your jar location>
after jar gets opened, press / and pass your class name and hit enter
You can try this :
unzip -v /your/jar.jar
This will be helpful only if your jar is executable i.e. in manifest you have defined some class as main class
Use this bash script:
#!/bin/bash
for VARIABLE in *.jar
do
jar -tf $VARIABLE |grep "\.class"|awk -v arch=$VARIABLE '{print arch ":" $4}'|sed 's/\//./g'|sed 's/\.\.//g'|sed 's/\.class//g'
done
this will list the classes inside jars in your directory in the form:
file1.jar:fullyqualifiedclassName
file1.jar:fullyqualifiedclassName
file1.jar:fullyqualifiedclassName
file1.jar:fullyqualifiedclassName
file2.jar:fullyqualifiedclassName
file2.jar:fullyqualifiedclassName
file2.jar:fullyqualifiedclassName
Sample output:
commons-io.jar:org.apache.commons.io.ByteOrderMark
commons-io.jar:org.apache.commons.io.Charsets
commons-io.jar:org.apache.commons.io.comparator.AbstractFileComparator
commons-io.jar:org.apache.commons.io.comparator.CompositeFileComparator
commons-io.jar:org.apache.commons.io.comparator.DefaultFileComparator
commons-io.jar:org.apache.commons.io.comparator.DirectoryFileComparator
commons-io.jar:org.apache.commons.io.comparator.ExtensionFileComparator
commons-io.jar:org.apache.commons.io.comparator.LastModifiedFileComparator
In windows you can use powershell:
Get-ChildItem -File -Filter *.jar |
ForEach-Object{
$filename = $_.Name
Write-Host $filename
$classes = jar -tf $_.Name |Select-String -Pattern '.class' -CaseSensitive -SimpleMatch
ForEach($line in $classes) {
write-host $filename":"(($line -replace "\.class", "") -replace "/", ".")
}
}
Description OF Solution : Eclipse IDE can be used for this by creating a sample java project and add all jars in the Project Build path
STEPS below:
Create a sample Eclipse Java project.
All all the jars you have in its Build Path
CTRL+SHIFT+T and Type the full class name .
Results will be displayed in the window with all the jars having that class. See attached picture .
windows cmd:
This would work if you have all te jars in the same directory and execute the below command
for /r %i in (*) do ( jar tvf %i | find /I "search_string")

Is there a way to get a list of all classes from a .dex file?

I have a .dex file, call it classes.dex.
Is there a way to "read" the contents of that classes.dex and get a list of all classes in there as full class names, including their package, com.mypackage.mysubpackage.MyClass, for exmaple?
I was thinking about com.android.dx.dex.file.DexFile, but I cannot seem to find a method for retrieving an entire set of classes.
Use the command line tool dexdump from the Android-SDK. It's in $ANDROID_HOME/build-tools/<some_version>/dexdump. It prints a lot more info than you probably want. I didn't find a way to make dexdump less verbose, but
dexdump classes.dex | grep 'Class descriptor'
should work.
You can use the dexlib2 library as a standalone library (available in maven), to read the dex file and get a list of classes.
DexFile dexFile = DexFileFactory.loadDexFile("classes.dex", 19 /*api level*/);
for (ClassDef classDef: dexFile.getClasses()) {
System.out.println(classDef.getType());
}
Note that the class names will be of the form "Ljava/lang/String;", which is how they are stored in the dex file (and in a java class file). To convert, just remove the first and last letter, and replace / with .
You can use dex2jar utility that will convert .dex to .jar.
http://code.google.com/p/dex2jar/
Then you can extract that .jar file.
Also , you can use this framework
Dedexer
baksmali has functionality to do this starting in baksmali v2.2.
baksmali list classes my.dex will print a list of all classes in the given dex file.
Reference: It is downloadable from here: https://github.com/JesusFreke/smali.
dxshow mydexfile.dex
dxshow:
strings -a $1 | grep "^L.*/" | grep -v "Ljava" | grep -v "Landroid" | sed "s/^L\(.*\);/\1/" | sed "s:/:.:g"
ezpz hack... didn't wanna spend a lifetime java coding

Find a jar file given the class name?

This must be a very basic question for Java developers, but what is the best way to find the appropriate jar file given a class name?
For example, given "com.ibm.websphere.security.auth.WSSubject", how do you track down the appropriate jar file? ("google" is not the answer I'm looking for!)
The java docs do not give any hint of the jar file, and obviously the names of the jar files themselves offer no clue.
There must be a 'search local jars', or some sort of 'auto-resolve dependencies', trick in the java world. Ideally, I'm looking for the 'official' way to do this. I happen to be on a windows machine without cygwin.
Save this as findclass.sh (or whatever), put it on your path and make it executable:
#!/bin/sh
find "$1" -name "*.jar" -exec sh -c 'jar -tf {}|grep -H --label {} '$2'' \;
The first parameter is the directory to search recursively and the second parameter is a regular expression (typically just a simple class name) to search for.
$ findclass.sh . WSSubject
The script relies on the -t option to the jar command (which lists the contents) and greps each table of contents, labelling any matches with the path of the JAR file in which it was found.
There is no "official" Java way to do this AFAIK.
The way I usually hunt for it is to use find and jar to look through all jar files in a given tree.
> find . -name \*.jar -print -exec jar tf {} oracle/sql/BLOB.class \;
./v2.6.1/lib/csw_library.jar
./v2.6.1/lib/oracle_drivers_12_01.jar
oracle/sql/BLOB.class
If you're on Windows and don't want to install Cygwin, then I suppose you would have to write a batch script to locate the jar files.
I have written a program for this:
https://github.com/javalite/jar-explorer
It will also decompile existing byte code to show you interfaces, methods, super classes, will show contents of other resources - text, images, html, etc.
You could try services like:
http://www.jarhoo.com/
http://www.docjar.com/
http://javacio.us/
http://merobase.com/
Or
Google Desktop with the Airbear Software's IndexZip Plug-in
Or
A maven enterprise repository with a search feature e.g. Nexus (OFC, this would only work if the jars you're looking for are indexed i.e. installed in the repository)
PS: Jarhoo has teamed up with Javacio.us to provide 100,000 Java developers with free access to Jarhoo via links integrated with their Google search results. Subscription to Javacio.us is free and open to anyone with a Google account. For more information, please visit the Jarhoo offer page at Javacio.us.
If the grep on your system (e.g. Solaris) doesn't have -H and --label as used in Dan Dyer's example, you can use:
find . -name '*.jar' -type f | xargs -i bash -c "jar -tvf {}| tr / . | grep WSSubject && echo {}"
To search all jars under the current directory and return the one(s) that contain class a.b.c.D do a:
find . -iname *.jar | while read JARF; do jar tvf $JARF | grep a/b/c/D.class && echo $JARF ; done
It will report all instances of class a.b.c.D (or classes with a similar suffix) and will only print the jars that contain it.
Typical output:
$ find . -iname *.jar | while read JARF; do jar tvf $JARF | grep Log.class && echo $JARF ; done
479 Fri Oct 10 18:19:40 PDT 2003 org/apache/commons/logging/Log.class
3714 Fri Oct 10 18:19:40 PDT 2003 org/apache/commons/logging/impl/Log4JCategoryLog.class
1963 Fri Oct 10 18:19:40 PDT 2003 org/apache/commons/logging/impl/NoOpLog.class
9065 Fri Oct 10 18:19:40 PDT 2003 org/apache/commons/logging/impl/SimpleLog.class
./WebContent/WEB-INF/lib/commons-logging.jar
In Windows, run cmd.exe and type:
for %i in (*.jar) do #jar tvf %i | find "/com/company/MyClass.class"
The jars would have to be in the current directory. For also has a /R option which takes a directory and lets you search recursively.
If Jar.exe isn't in your path, you can do something like #C:\jdk\bin\jar.exe.
Try findjar.com. If it's in the public domain, you should get it. There's alos mavenjava.com (but that site seems to be down)
Printing the list as I go so I can see what I'm checking. Mostly I'm looking in a lib/app directory, but you can substitute a locate for the find.
e.g.
for jar in $(find some_dir/lib -name "*.jar" );
do
echo -------------$jar-------------------
jar -tf $jar | grep TheNameOfTheClassImLookingFor
done
Given your comment on attempting to handle dependencies, what I would do is focus on which libraries you are using. Knowing this, you will know what jars have to be on the classpath, and you can pay attention to that. There are also dependency management builders (Maven and Ant being two popular ones) that can package up projects with their dependencies inside. However, in the end, it is up to the developer to know which dependencies they have, and to pay attention to the requirements for those dependencies. This is one reason why using an IDE like Eclipse, and build tools like Maven or Ant are so nice in large projects, as when you have 20 dependencies for a project, that can become pretty unmanageable.
I use jarscan. It is an executable jar file that can recursively search an entire folder structure for jars that contain the class that you are looking for. It searches by class name, package name or regex.
In windows powershell you can use this command. It list all the JAR files it encounters, but it has an extra line that's very easy to spot where it also finds your class.
Get-ChildItem -recurse -Filter *.jar | Foreach {echo $_.fullname; c:\somepath\JDK\BIN\jar tvf $_.fullname | Select-String -pattern "Cabbages.class"}
There is also this web site that seems to be usefull.
http://www.findjar.com/
locate .jar | xargs grep com.ibm.websphere.security.auth.WSSubject
if you are still searching for WSSubject, then jar is wssec.jar. WSSecurityException class inside sas.jar
I recommend using Maven, Eclipse and m2eclipse.
Step 1 - add specific import
Step 2 - find and download (automatically) desired jar
Building up on Dan's excellent answer, the following script solves the problem of mangled output in case some of the jars are actually broken symlinks (while at the same time not skipping proper symlinks) It also searches in the current directory if no argument is provided.
#!/usr/bin/env bash
if [[ ($# -ne 1) && ($# -ne 2) ]]
then
echo "usage is $0 <grep RegEx to look for in contents of jar> [<top-of-folder-hierarchy> or, if missing, current dir]"
else
REG_EXP=$1
DIR=${2:-.}
if [ ! -d $DIR ]; then
echo "directory [$DIR] does not exist";
exit 1;
fi
find "$DIR" -name "*.jar" -exec sh -c '
(test -e {})
exitStatus=$?
if [ "$exitStatus" -eq 0 ]; then # this is done to avoid broken symlinks
jar -tf {}|grep -i -H --label {} '$REG_EXP'
fi
' \;
fi
in Intellij Idea
on your class press ctrl+B and after that you can find the jar file.
on project bar press scroll from source.
you can see the jar file contains the class.

Categories