Comments (3)
woah, i did it by modifying dexlib and it found working in primary tests.
first downloaded source of dexlib, loaded in intelliJ idea, converted it to pom project from gradle file using my script- "gradle2marven.py"(can be found in my repo).
deleted smali and baksmali project folders, kept only dexlib2 project.
modifications -
modifications done in DexWriter, since DexPool is extending from it, so all methods added to this will apear in DexPool class.
aim was to add a interface which let you modify orignal string just before it is written to the StringDataSection.
so in DexWriter-
-
added a field- "
public StringSectionEditor sectionEditor = null;
" -
added a method also-
public void addSectionEditorListener(StringSectionEditor editor){
sectionEditor = editor;
}
- modifyed the writeStrings() method as following-
for (Map.Entry<? extends StringKey, Integer> entry: stringEntries) {
entry.setValue(index++);
indexWriter.writeInt(offsetWriter.getPosition());
String stringValue = entry.getKey().toString();
if(sectionEditor!=null){ // this if-condition is added
stringValue = sectionEditor.modifyString(stringValue);
}
offsetWriter.writeUleb128(stringValue.length());
offsetWriter.writeString(stringValue);
offsetWriter.write(0);
}
- added the interface at the end of file(but inside this class)
public interface StringSectionEditor{
public String modifyString(String orignalString);
}
testing
this was my understanding about DexPool class - DexPool is a container where classes can be added, using internClass() method, and DexPool extracts its needed parts for writing all sections about added ClassDef.
so first we need to create a Dex file, then a DexPool , then ClassDefs need to be added to DexPool from DexFile using internClass method.
but just before we use writeTo() method on DexPool(which creates the output dex file according to interned classes), we will add a Interface. so that when writeStrings() method will be called during dexMaking process, all strings will be prompted to be modified before us on this interface. there we will modify our strings. then they will be written to the dex's StringDataSection.
to test whether the modification caused any errors or not, i used jadx-gui , if the moded dex is loaded in this software correctly without error (same as the orignal dex) then our test is successfull. we should also see the changed package names and changed method names etc, and these changes should appear everywhere in the dex, means if we change the name of a method then it should appear changed all the places where it is called.
testing code was like this-
public static void testModdedWrite(String dexFilePath){
DexFile dexFile = null;
try {
dexFile = loadDexFile(dexFilePath, null);
DexPool dexPool = new DexPool(dexFile.getOpcodes());
for(ClassDef classDef: dexFile.getClasses()){
dexPool.internClass(classDef);
}
for(Map.Entry<String, Integer> s: dexPool.stringSection.getItems()){
System.out.printf("%d : %s\n",s.getValue(),s.getKey());
}
dexPool.addSectionEditorListener(new DexWriter.StringSectionEditor() {
@Override
public String modifyString(String orignalString) {
if(orignalString.startsWith("Lbhakar/manoj/testappv32/")) {
return orignalString.replace("Lbhakar/manoj/testappv32/", "Lbhakar/manoj/testappv50/");
}else if(orignalString.equals("dbToLayout")){
return "modded";
}else{
return orignalString;
}
}
});
dexPool.writeTo(new FileDataStore(new File("H:\\outdex_dexPool_moddedStringSection.dex")));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
and when i tested it. it was super successfull. all classes's package name was changed, and the method named "dbToLayout" was converted into "modded" , it also appeared as "modded" 3 other places where it was called.
i hope this will help someone who want to do refectoring using dexlib2, i wasted 5 days in search of this.
from smali.
I am not sure what you actually intend to do but there is a class called DexRewriter
that may suits your needs. See the following example:
from smali.
i want to do refectoring/renaming/obsfucation. means i want to rename a class from "Lx/y/bar"; to "Lx/y/foo;" in a already compiled dex file.
there are many way to do it.
- first disassemble all classDefs into smali files any then find and replace in all and then compile them to dex again.
this method will take a lot of time because we need to decompile all classes in dex. - using dexRewriter- this method is also heavy because it will need a lot more coding. because first i need to parse dex file then for its all classes, parse fields and methods, then for all methods, parse instructions, and then for all classdef + methods + method parameters + fields + instruction type references, parse annotations. and its not all, because in annotations, i need to parse annotation items and then encoded values which can also be type of annotations. very complex method it is.
- what i thought is - every thing in dex file such as classDef, method, field , annotations, method parameters etc reference a class name such as L/x/y/foo; from a single place. which is StringDataSection of dex file.
if we replace L/x/y/foo; with L/x/y/bar; in StringDataSection of dex file, then it will be automatically replaced in all referenced classes, methods, fields, method parameters , annotatations etc.
thats why i need the class that provide me list of strings in StringDataSection of dex file and then after modification i can set this list of string back to dex file. i should be clear now? aint i?
from smali.
Related Issues (20)
- New official version published? HOT 4
- added support for 4rcc in MutableMethodImplementation
- Ive been Hacked please help me HOT 6
- Parsing smali methode HOT 1
- The Fields do not keep their initial values if they're default value
- odex error
- Error while writing instruction at code offset 0x8
- How to calculate the contribution of each class to the dex HOT 2
- Dex file size increases by ~50% without making changes (DexFileFactory.loadDexFile then DexFileFactory.writeDexFile) HOT 7
- missing EOF at '.class'
- How to create a new method using dexlib2 HOT 2
- Try to run examples/BracketedMemberNames HOT 1
- how to copy class to new dex using dexlib2 HOT 4
- Smali patcher does not support Dex version 039 HOT 1
- java.lang.NoSuchMethodError when calling DexPool.internClass
- smali report error
- how to move the useless classes to a new dex using dexlib2? HOT 1
- [Feature Request] Could we support dex v41? HOT 3
- Won't build under Debian WSL
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from smali.