Interface IDexUnit
- All Superinterfaces:
IAddressableUnit,ICodeUnit,IEventSource,IInteractiveUnit,IUnit,IUnitCreator,IUserDataSupport
Dex units use Java-style internal addresses to identify items:
- package: Lcom/abc/
- type: Lcom/abc/Foo;
- method: Lcom/abc/Foo;->bar(I[JLjava/Lang/String;)V
- field: Lcom/abc/Foo;->flag1:Z
More information here.
Note that in the case of multi-Dex APKs, the Dex unit represents a virtual, unified view of the
separate Dex files contained in the APK. If required, the individual information about those Dex
files can be retrieved via IDexFile.
Like many units, Dex unit objects emit J.UnitChange when the unit contents is being
changed. IDexUnit set up event objects such that JebEvent.getData() will return a
UnitChangeEventData with fields reasonably populated.
How use the JEB API to interact with those objects?
- Writing JEB client scripts in Python is a great way to ease into the JEB API.
- Visit this public
GitHub repository for sample code
- In the JEB client, use F2 to bring up the script manager, and try out some sample scripts
Below is a sample client script that shows how to retrieve a project, find the main dex unit,
enumerate dex methods, check the bytecode and search for specific instructions.
File: ListDexMethodsWithXor.py
#?description=List dex methods making use of xor instructions
#?shortcut=
from com.pnfsoftware.jeb.client.api import IScript
from com.pnfsoftware.jeb.core.units.code.android import IDexUnit
class ListDexMethodsWithXor(IScript):
def run(self, ctx):
prj = ctx.getMainProject()
assert prj, 'Need a project'
dex = prj.findUnit(IDexUnit)
assert dex, 'Need a dex unit'
cnt = 0
for m in dex.getMethods():
if m.isInternal():
ci = m.getData().getCodeItem()
if ci and self.checkMethod(ci):
print(m.getSignature(True, False))
cnt += 1
print('Found %d methods' % cnt)
def checkMethod(self, ci):
for insn in ci.getInstructions():
if insn.toString().find('xor-') >= 0:
return True
return False
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intDex access flag for abstract items.static final intDex access flag for annotation classes.static final intDex access flag for bridge methods.static final intDex access flag for constructor methods.static final intDex access flag for declared synchronized methods.static final intDex access flag for enumeration classes and fields.static final intDex access flag for final items.static final intDex access flag for interfaces.static final intDex access flag for native methods.static final intDex access flag for private items.static final intDex access flag for protected items.static final intDex access flag for public items.static final intDex access flag for static items.static final intDex access flag for strict floating-point methods.static final intDex access flag for synchronized methods.static final intDex access flag for synthetic items.static final intDex access flag for transient fields.static final intDex access flag for variable argument methods.static final intDex access flag for volatile fields.static final intInlining mode: allowed.static final intInlining mode: auto-determined.static final intInlining mode: blocked.static final intInlining mode: forced.static final intEncoded address tag for bytecode locations.static final intEncoded address tag for classes.static final intDo not use directly - reserved by dex decompilersstatic final intEncoded address tag for fields.static final intEncoded address tag for immediates.static final intEncoded address tag for methods.static final intEncoded address tag for packages.static final intEncoded address tag for parameters.static final intEncoded address tag for strings.static final intEncoded address tag for types.static final intEncoded address tag for variables.static final intEncoded address tag for virtual variables.static final intBits reserved for an encoded Dex item address tag.static final intBit shift of the item id in encoded Dex item addresses.static final StringUnit property name for the context information database path. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdd (merge) an additional Dex file into this Dex unit.Create a new field and add it to the field pool index.Create a new field and add it to the field pool index.Create a new method reference and add it to the method pool index.Create a new method reference and add it to the method pool index.addPackage(String signature) Create a package and add it to the package pool.addPrototype(String prototypeString) Create a new Dex prototype and add it to the prototype pool index.Create a new string and add it to the string pool index.Create a new type and add it to the type pool index.findClassByName(String searchedName) Find a class by effective name, unambiguously.findClassByName(String searchedName, int maxCount, boolean alsoCheckOriginalName) Find a class by name.intRetrieve the pool index of a string, by value.intGet the number of invalid type descriptors.getCallSite(int index) Retrieve a call site by index.List<? extends IDexCallSite> Get the Dex call site pool.getClass(int index) Convenience method to retrieve a class by its Dex class pool index.Convenience method used to retrieve a class by name.Get the Dex class poolGet the comment manager.Retrieve the constant library object.Retrieve the context information provider.intRetrieve the count of dex entries managed by this unit.default intDeprecated.getCrossReferences(DexPoolType poolType, int index) Retrieve a list of addresses referencing the provided pool item.getCrossReferences(DexPoolType poolType, int index, int cap) Retrieve a list of addresses referencing the provided pool item.Retrieve or create a decompiler for this unit.Retrieve the collection of dex entries managed by this dex unit.getDexEntry(int index) Retrieve a dex entry by index.default IDexFilegetDexFile(int index) Deprecated.Deprecated.usegetDexEntries()This convenience method provides the entire disassembly of the bytecode making up the Dex file.Convenience method to retrieve the text document representing the disassembly of this code unit.getField(int index) Convenience method to retrieve a field by its Dex field pool index.Convenience method used to retrieve a field by name.Get the Dex field poollongRetrieve the total amount of instructions in this Dex unit.getMethod(int index) Convenience method to retrieve a method by its Dex method pool index.Convenience method used to retrieve a method by name.getMethodHandle(int index) Retrieve a method handle by index.List<? extends IDexMethodHandle> Get the Dex method handle pool.Retrieve the method-match object attached to a dex method.List<? extends IDexMethod> Get the Dex method poolgetObjectById(long id) Retrieve an object by encoded id.getPackage(String signature) Retrieve a package by signature.List<? extends IDexPackage> Get the Dex package pool.getPrototype(int index) Convenience method to retrieve a prototype by its Dex prototype pool index.List<? extends IDexPrototype> Get the list of Dex prototypes defined in the Dex file (prototype pool).Retrieve the cross-references manager.Retrieve a map of renamed identifiers.getStaticFieldInitializer(int index) Retrieve the initializer for the static field of a class.getString(int index) Convenience method to retrieve a string by its Dex string pool index.intGet the number of strings present in the aggregated string pools represented by this Dex unit.List<? extends IDexString> Get the Dex string pool, including extra strings added viaaddString.getType(int index) Retrieve a type by its Dex type pool index.Retrieve a type by its fully-qualified name.getTypeHierarchy(String typesig, int maxNodeCount, boolean includeSuperTypes) Generate a type hierarchy tree.getTypes()Get the Dex prototype pooldefault booleanMove a class or a package to another package, class, or method.default booleanMove a class or a package to another package, class, or method.booleanMove a class or a package to another package, class, or method.setMethodMatch(IDexMethod m, MethodMatch match) Attach a method-match object to a dex method.Methods inherited from interface com.pnfsoftware.jeb.core.units.IAddressableUnit
getAddressLabel, getAddressLabels, getAddressOfItem, getCanonicalAddress, getItemAtAddress, getItemObject, getRelatedItems, getWellKnownAddresses, isValidAddressMethods inherited from interface com.pnfsoftware.jeb.core.units.code.ICodeUnit
getAddressFromCodeCoordinates, getCodeCoordinatesFromAddress, getCodeItemByAddress, getHierarchyMethods inherited from interface com.pnfsoftware.jeb.util.events.IEventSource
addListener, countListeners, getListeners, getParentSource, insertListener, notifyListeners, removeListener, setParentSourceMethods inherited from interface com.pnfsoftware.jeb.core.units.IInteractiveUnit
addressToLocation, canExecuteAction, executeAction, executeAction, getAddressActions, getFullComment, getFullComments, getGlobalActions, getInlineComment, getInlineComments, getItemActions, getMetadataManager, locationToAddress, prepareExecution, setInlineCommentMethods inherited from interface com.pnfsoftware.jeb.core.units.IUnit
addChild, addChild, canBePersisted, dispose, execprvfunc, generateQuickState, getChildren, getContributions, getCreationTimestamp, getDescription, getExtraInputs, getFormatter, getFormatType, getIconData, getInput, getInterpreters, getLock, getName, getNotes, getNotificationManager, getParent, getParentArtifact, getParentProject, getPropertyDefinitionManager, getPropertyManager, getRealName, getStatus, getUid, getUnitProcessor, initializePropertyObjects, isDisposed, isProcessed, isStale, isTransientChild, notifyGenericChange, postDeserialization, process, removeChild, setName, setNotes, setParent, setRealName, setUnitProcessorMethods inherited from interface com.pnfsoftware.jeb.core.IUserDataSupport
clearAllData, getAllData, getData, setData
-
Field Details
-
ACC_PUBLIC
static final int ACC_PUBLICDex access flag for public items.- See Also:
-
ACC_PRIVATE
static final int ACC_PRIVATEDex access flag for private items.- See Also:
-
ACC_PROTECTED
static final int ACC_PROTECTEDDex access flag for protected items.- See Also:
-
ACC_STATIC
static final int ACC_STATICDex access flag for static items.- See Also:
-
ACC_FINAL
static final int ACC_FINALDex access flag for final items.- See Also:
-
ACC_SYNCHRONIZED
static final int ACC_SYNCHRONIZEDDex access flag for synchronized methods.- See Also:
-
ACC_VOLATILE
static final int ACC_VOLATILEDex access flag for volatile fields.- See Also:
-
ACC_BRIDGE
static final int ACC_BRIDGEDex access flag for bridge methods.- See Also:
-
ACC_TRANSIENT
static final int ACC_TRANSIENTDex access flag for transient fields.- See Also:
-
ACC_VARARGS
static final int ACC_VARARGSDex access flag for variable argument methods.- See Also:
-
ACC_NATIVE
static final int ACC_NATIVEDex access flag for native methods.- See Also:
-
ACC_INTERFACE
static final int ACC_INTERFACEDex access flag for interfaces.- See Also:
-
ACC_ABSTRACT
static final int ACC_ABSTRACTDex access flag for abstract items.- See Also:
-
ACC_STRICT
static final int ACC_STRICTDex access flag for strict floating-point methods.- See Also:
-
ACC_SYNTHETIC
static final int ACC_SYNTHETICDex access flag for synthetic items.- See Also:
-
ACC_ANNOTATION
static final int ACC_ANNOTATIONDex access flag for annotation classes.- See Also:
-
ACC_ENUM
static final int ACC_ENUMDex access flag for enumeration classes and fields.- See Also:
-
ACC_CONSTRUCTOR
static final int ACC_CONSTRUCTORDex access flag for constructor methods.- See Also:
-
ACC_DECLARED_SYNCHRONIZED
static final int ACC_DECLARED_SYNCHRONIZEDDex access flag for declared synchronized methods.- See Also:
-
INLINE_AUTO
static final int INLINE_AUTOInlining mode: auto-determined. This mode is to be understood as 'limited', that is, inlining is blocked except for trivial cases- See Also:
-
INLINE_BLOCKED
static final int INLINE_BLOCKEDInlining mode: blocked. The method must not be inlined by code processors.- See Also:
-
INLINE_ALLOWED
static final int INLINE_ALLOWEDInlining mode: allowed. The method may be inlined by code processors.- See Also:
-
INLINE_FORCED
static final int INLINE_FORCEDInlining mode: forced. The method should be inlined by code processors if possible.- See Also:
-
itagItemIdShift
static final int itagItemIdShiftBit shift of the item id in encoded Dex item addresses.- See Also:
-
itagBitsize
static final int itagBitsizeBits reserved for an encoded Dex item address tag.- See Also:
-
ITAG_STRING
static final int ITAG_STRINGEncoded address tag for strings.- See Also:
-
ITAG_PACKAGE
static final int ITAG_PACKAGEEncoded address tag for packages.- See Also:
-
ITAG_TYPE
static final int ITAG_TYPEEncoded address tag for types.- See Also:
-
ITAG_CLASS
static final int ITAG_CLASSEncoded address tag for classes.- See Also:
-
ITAG_FIELD
static final int ITAG_FIELDEncoded address tag for fields.- See Also:
-
ITAG_METHOD
static final int ITAG_METHODEncoded address tag for methods.- See Also:
-
ITAG_BYTECODE
static final int ITAG_BYTECODEEncoded address tag for bytecode locations.- See Also:
-
ITAG_PARAMETER
static final int ITAG_PARAMETEREncoded address tag for parameters.- See Also:
-
ITAG_VARIABLE
static final int ITAG_VARIABLEEncoded address tag for variables.- See Also:
-
ITAG_IMMEDIATE
static final int ITAG_IMMEDIATEEncoded address tag for immediates.- See Also:
-
ITAG_VIRTUAL_VAR
static final int ITAG_VIRTUAL_VAREncoded address tag for virtual variables.- See Also:
-
ITAG_CUSTOM
static final int ITAG_CUSTOMDo not use directly - reserved by dex decompilers- See Also:
-
propnameContextInfoDb
Unit property name for the context information database path.- See Also:
-
-
Method Details
-
getCountOfDexEntries
int getCountOfDexEntries()Retrieve the count of dex entries managed by this unit.- Returns:
- the number of dex entries
-
getCountOfDexFiles
Deprecated.- Returns:
- the number of dex entries
-
getDexEntries
Retrieve the collection of dex entries managed by this dex unit.When the unit is the result of processing a multi-dex APK, dex files containing multiple dex entries (container dex, version 41+), or if the unit received
additional dex files, this method will return a list with multiple items.- Returns:
- a non-empty collection of dex entries
-
getDexFiles
Deprecated.usegetDexEntries()- Returns:
- a non-empty collection of dex entries
-
getDexEntry
Retrieve a dex entry by index.- Parameters:
index- an index- Returns:
- the dex entry
-
getDexFile
Deprecated.- Parameters:
index- dex entry index- Returns:
- the dex entry
-
findStringIndex
Retrieve the pool index of a string, by value.- Parameters:
s- a string- Returns:
- an string pool index, -1 if the string was not found
-
getStrings
List<? extends IDexString> getStrings()Get the Dex string pool, including extra strings added viaaddString.- Specified by:
getStringsin interfaceICodeUnit- Returns:
- code strings
-
getString
Convenience method to retrieve a string by its Dex string pool index.- Parameters:
index- string pool index- Returns:
- the Dex string
-
getStringCount
int getStringCount()Get the number of strings present in the aggregated string pools represented by this Dex unit.- Returns:
- the string count
-
addString
Create a new string and add it to the string pool index.- Parameters:
value- string value- Returns:
- the created Dex string
-
getPrototypes
List<? extends IDexPrototype> getPrototypes()Get the list of Dex prototypes defined in the Dex file (prototype pool).- Returns:
- a list of prototypes
-
getPrototype
Convenience method to retrieve a prototype by its Dex prototype pool index.- Parameters:
index- prototype index- Returns:
- the Dex prototype
-
addPrototype
Create a new Dex prototype and add it to the prototype pool index.- Parameters:
prototypeString- a full prototype string, such as:(typeParam1,typeParam2,...)typeReturn- Returns:
- the created Dex prototype
-
getTypes
Get the Dex prototype pool -
getType
Retrieve a type by its Dex type pool index.- Parameters:
index- type pool index- Returns:
- the Dex type
-
getType
Retrieve a type by its fully-qualified name.- Parameters:
fqname- eg,Lcom/foo/Bar;- Returns:
- the Dex type, or null if none was found
-
addType
Create a new type and add it to the type pool index.- Parameters:
typeString- a fully-qualified type name, using the standard Java internal signature notation (L...;)- Returns:
- the created Dex type
-
getBadTypeCount
int getBadTypeCount()Get the number of invalid type descriptors.- Returns:
- the bad type count
-
getClass
Description copied from interface:ICodeUnitConvenience method used to retrieve a class by name. -
getClasses
Get the Dex class pool- Specified by:
getClassesin interfaceICodeUnit- Returns:
- code classes
-
getClass
Convenience method to retrieve a class by its Dex class pool index.- Parameters:
index- class pool index- Returns:
- the Dex class
-
findClassByName
Find a class by effective name, unambiguously.- Parameters:
searchedName- class name, which could be a simple name, e.g. Bar forcom.foo.Baror Inner1 forcom.foo.Bar$Inner1- Returns:
- the class or null if nothing was found or if the searched name was ambiguous
-
findClassByName
Find a class by name.- Parameters:
searchedName- class name, which could be a simple name, e.g. Bar forcom.foo.Baror Inner1 forcom.foo.Bar$Inner1maxCount- maximum count of results (-1 means no max)alsoCheckOriginalName- if true, the original (pre-renaming) class names are checked as well- Returns:
- the list of classes
-
getFields
Get the Dex field pool -
getField
Description copied from interface:ICodeUnitConvenience method used to retrieve a field by name. -
getField
Convenience method to retrieve a field by its Dex field pool index.- Parameters:
index- field pool index- Returns:
- the Dex field
-
getStaticFieldInitializer
Retrieve the initializer for the static field of a class.- Parameters:
index- the field index- Returns:
- the static initializer, or null if there's none
-
addField
Create a new field and add it to the field pool index.- Parameters:
signature- full, egLcom/foo/Bar;->val:I- Returns:
- the created Dex field
-
addField
Create a new field and add it to the field pool index.- Parameters:
type- full, egLcom/foo/Bar;->val:Ifieldname- simple namefieldtype- full, egLcom/foo/Bar;->val:I- Returns:
- the created Dex field
-
getMethods
List<? extends IDexMethod> getMethods()Get the Dex method pool- Specified by:
getMethodsin interfaceICodeUnit- Returns:
- code methods
-
getMethod
Description copied from interface:ICodeUnitConvenience method used to retrieve a method by name. -
getMethod
Convenience method to retrieve a method by its Dex method pool index.- Parameters:
index- method pool index- Returns:
- the Dex method
-
addMethod
Create a new method reference and add it to the method pool index.- Parameters:
signature- full signature, including type name, eg:La/b/Foo;->bar(ILjava/lang/String;)Z- Returns:
- the created Dex method
-
addMethod
Create a new method reference and add it to the method pool index.- Parameters:
type- defining type signaturemethodname- method nameprotostring- method prototype string- Returns:
- the created Dex method
-
getCallSite
Retrieve a call site by index.- Parameters:
index- call site pool index- Returns:
- the Dex call site
-
getCallSites
List<? extends IDexCallSite> getCallSites()Get the Dex call site pool.- Returns:
- the call sites
-
getMethodHandle
Retrieve a method handle by index.- Parameters:
index- method handle pool index- Returns:
- the Dex method handle
-
getMethodHandles
List<? extends IDexMethodHandle> getMethodHandles()Get the Dex method handle pool.- Returns:
- the method handles
-
getPackages
List<? extends IDexPackage> getPackages()Get the Dex package pool.- Specified by:
getPackagesin interfaceICodeUnit- Returns:
- code packages
-
getPackage
Retrieve a package by signature.- Specified by:
getPackagein interfaceICodeUnit- Parameters:
signature- fully-qualified package name, e.g.com.foo.bar- Returns:
- the Dex package, or null if none was found
-
addPackage
Create a package and add it to the package pool.- Parameters:
signature- fully-qualified package name, e.g.com.foo.bar- Returns:
- the created Dex package
-
moveTo
boolean moveTo(IDexItem src, IDexItem dst, boolean skipChecks, boolean neverAnonymous, boolean notify) Move a class or a package to another package, class, or method.- Parameters:
src- anIDexPackageorIDexClassdst- anIDexPackage,IDexClassorIDexMethodskipChecks- skip extra sanity checks (if applicable)neverAnonymous- legal only when moving a class to another method (else N/A); if true, the moved class will never be made an anonymous class of the destination method; if false, an anonymous class will be favored, if it is possible to do sonotify- true to notify listeners of the change- Returns:
- success indicator
-
moveTo
Move a class or a package to another package, class, or method.- Parameters:
src- anIDexPackageorIDexClassdst- anIDexPackage,IDexClassorIDexMethodskipChecks- skip extra sanity checks (if applicable)neverAnonymous- legal only when moving a class to another method (else N/A); if true, the moved class will never be made an anonymous class of the destination method; if false, an anonymous class will be favored, if it is possible to do so- Returns:
- success indicator
-
moveTo
Move a class or a package to another package, class, or method. Checks are not skipped; when moving a class to a method, the class will be made anonymous.- Parameters:
src- anIDexPackageorIDexClassdst- anIDexPackage,IDexClassorIDexMethod- Returns:
- success indicator
-
getInstructionCount
long getInstructionCount()Retrieve the total amount of instructions in this Dex unit. Note that if this unit represents the virtual Dex file resulting from the merge of several classesX.dex files, the number of instructions returned is the sum of instructions of each individual Dex file.- Returns:
- the instruction count
-
getDisassemblyDocument
IDexDisassemblyDocument getDisassemblyDocument()Description copied from interface:ICodeUnitConvenience method to retrieve the text document representing the disassembly of this code unit.The caller is responsible for disposing the returned document after usage.
- Specified by:
getDisassemblyDocumentin interfaceICodeUnit- Returns:
- disassembly document
-
getDisassembly
String getDisassembly()This convenience method provides the entire disassembly of the bytecode making up the Dex file. This method is a convenience method: the disassembly text document object can always be retrieved viaIUnit.getFormatter().- Specified by:
getDisassemblyin interfaceICodeUnit- Returns:
- disassembly text
-
getCrossReferences
Retrieve a list of addresses referencing the provided pool item. This method is left for convenience and legacy only. Newer scripts should usegetReferenceManager().- Parameters:
poolType- pool item type; currently supported for xrefs: STRING, TYPE, FIELD, METHODindex- pool item indexcap- max number of references to return (leave to 0 to mean return everything possible)- Returns:
- a list of cross-reference addresses
-
getCrossReferences
Retrieve a list of addresses referencing the provided pool item. This method is left for convenience and legacy only. Newer scripts should usegetReferenceManager().- Parameters:
poolType- pool item type; currently supported for xrefs: STRING, TYPE, FIELD, METHODindex- pool item index- Returns:
- a list of cross-reference addresses
-
getReferenceManager
IDexReferenceManager getReferenceManager()Retrieve the cross-references manager.- Returns:
- the reference manager
-
getCommentManager
DexCommentManager getCommentManager()Description copied from interface:IInteractiveUnitGet the comment manager. This method is optional. When the unit is disposed, this method must return null. The default implementation returns null.- Specified by:
getCommentManagerin interfaceICodeUnit- Specified by:
getCommentManagerin interfaceIInteractiveUnit- Returns:
- a comment manager, or null if the unit does not have one
-
getRenamedIdentifiers
Map<IdentifierCoordinates,String> getRenamedIdentifiers()Retrieve a map of renamed identifiers. Only renamed identifiers are stored in the returned object.- Returns:
- the renamed identifier map
-
getObjectById
Retrieve an object by encoded id.- Parameters:
id- encoded object id- Returns:
- the object, or null if none was found
-
addDex
Add (merge) an additional Dex file into this Dex unit.- Parameters:
dexInput- a Dexinput(file input, bytes input, etc.) or a ZIP input containing a singleclasses.dexentry- Throws:
IOException- if the input could not be read or merged
-
getConstantsLibrary
DexConstantLibrary getConstantsLibrary()Retrieve the constant library object. The constants library holds the constant fields of this dex unit (that is, allstatic finalfields ,regardless of their visibility attributes) as well as values attached to pure field references (external fields)- Returns:
- the constant library
-
getContextInfoProvider
IDexContextInfoProvider getContextInfoProvider()Retrieve the context information provider. This provider can be used to retrieve methods' context access information (context-sensitivity, side-effect) and fields' effective finality information.- Returns:
- the context information provider
-
getDecompiler
IDexDecompilerUnit getDecompiler()Retrieve or create a decompiler for this unit. Ifdexdec(the Dex Decompiler module) is not available with your JEB license, null is returned.- Returns:
- the decompiler unit, or null if unavailable
-
getTypeHierarchy
Generate a type hierarchy tree.- Parameters:
typesig- root type signaturemaxNodeCount- maximum number of nodes to generateincludeSuperTypes- true to include supertypes- Returns:
- the type hierarchy root node
-
setMethodMatch
Attach a method-match object to a dex method.This method is thread-safe. Match objects can be attached by any component.
- Parameters:
m- the target methodmatch- a match item- Returns:
- the previously attached match item, null if none
-
getMethodMatch
Retrieve the method-match object attached to a dex method.This method is thread-safe. Match objects can be attached by any component.
- Parameters:
m- the target method- Returns:
- the attached match item, or null if none
-
getCountOfDexEntries()