Skip to content

Commit

Permalink
Docs: add more docs
Browse files Browse the repository at this point in the history
  • Loading branch information
BryanGIG committed Dec 9, 2024
1 parent a83dd23 commit 4f0fbce
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 22 deletions.
60 changes: 51 additions & 9 deletions app/src/main/java/com/dumper/android/dumper/OutputHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import com.dumper.android.utils.TAG
import java.io.FileOutputStream
import java.io.OutputStream

/**
* Class responsible for handling output messages.
*/
class OutputHandler {
private var isRoot = false
private lateinit var parcelFileDescriptor: ParcelFileDescriptor
Expand All @@ -16,27 +19,31 @@ class OutputHandler {
private constructor()

/**
* This method is used to send message to client
* Use this method if you're on root services
* @param from: Message from client
* @param reply: Message to client
*/
* Constructor for root services.
*
* @param parcelFileDescriptor The ParcelFileDescriptor for root services.
*/
constructor(parcelFileDescriptor: ParcelFileDescriptor) : this() {
isRoot = true
this.parcelFileDescriptor = parcelFileDescriptor
outputStream = FileOutputStream(parcelFileDescriptor.fileDescriptor)
}

/**
* This method is used to append message to console
* Use this method if you're on non-root
* @param console: ConsoleViewModel to append
*/
* Constructor for non-root services.
*
* @param console The ConsoleViewModel to append messages to.
*/
constructor(console: ConsoleViewModel) : this() {
isRoot = false
this.console = console
}

/**
* Process the input string and send it to the appropriate output.
*
* @param str The string to be processed.
*/
private fun processInput(str: String) {
if (isRoot) {
try {
Expand All @@ -49,6 +56,11 @@ class OutputHandler {
}
}

/**
* Finish the output handling and close resources.
*
* @param code The exit code.
*/
fun finish(code: Int) {
if (isRoot) {
try {
Expand All @@ -62,26 +74,56 @@ class OutputHandler {
}
}

/**
* Append text to the output.
*
* @param text The text to append.
*/
fun append(text: String) {
processInput(text)
}

/**
* Append a line of text to the output.
*
* @param text The text to append.
*/
fun appendLine(text: String) {
processInput(text + "\n")
}

/**
* Append an error message to the output.
*
* @param text The error message to append.
*/
fun appendError(text: String) {
appendLine("[ERROR] $text")
}

/**
* Append a warning message to the output.
*
* @param text The warning message to append.
*/
fun appendWarning(text: String) {
appendLine("[WARNING] $text")
}

/**
* Append an info message to the output.
*
* @param text The info message to append.
*/
fun appendInfo(text: String) {
appendLine("[INFO] $text")
}

/**
* Append a success message to the output.
*
* @param text The success message to append.
*/
fun appendSuccess(text: String) {
appendLine("[SUCCESS] $text")
}
Expand Down
20 changes: 20 additions & 0 deletions app/src/main/java/com/dumper/android/dumper/elf/ElfUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,17 @@ import java.io.RandomAccessFile
import java.nio.ByteBuffer
import java.nio.channels.FileChannel

// ELF header magic number
private val hElf =
byteArrayOf(0x7F, 'E'.code.toByte(), 'L'.code.toByte(), 'F'.code.toByte())

/**
* Checks if the memory at the given start address of a process is an ELF file.
*
* @param pid The process ID.
* @param startAddress The start address in the process memory.
* @return True if the memory at the start address is an ELF file, false otherwise.
*/
fun isELF(pid: Int, startAddress: Long): Boolean {
val mFile = RandomAccessFile("/proc/$pid/mem", "r")
val channel = mFile.channel
Expand All @@ -19,11 +27,23 @@ fun isELF(pid: Int, startAddress: Long): Boolean {
return byteHeader.array().contentEquals(hElf)
}

/**
* Determines the architecture of the ELF file in the given memory.
*
* @param mFile The file channel of the memory.
* @param memory The memory map line parser.
* @return The architecture of the ELF file.
*/
fun getArchELF(mFile: FileChannel, memory: MapLineParser): Arch {
val byteHeader = ByteBuffer.allocate(5)

mFile.read(byteHeader, memory.getStartAddress())

for (i in 0 until 4) {
if (byteHeader[i] != hElf[i]) {
return Arch.UNKNOWN
}
}
if (byteHeader[0] != hElf[0] || byteHeader[1] != hElf[1] ||
byteHeader[2] != hElf[2] || byteHeader[3] != hElf[3]
) {
Expand Down
25 changes: 22 additions & 3 deletions app/src/main/java/com/dumper/android/dumper/process/Process.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,25 @@ import java.io.File

object Process {

/**
* Get all processes running on the device.
*
* @param ctx The application context.
* @param isRoot Boolean indicating if the device is rooted.
* @return List of ProcessData containing process information.
*/
fun getAllProcess(ctx: Context, isRoot: Boolean) =
if (isRoot)
getAllProcessRoot(ctx)
else
getAllProcessNoRoot()


/**
* Get all processes running on a rooted device.
*
* @param ctx The application context.
* @return List of ProcessData containing process information.
*/
private fun getAllProcessRoot(ctx: Context): List<ProcessData> {
val activityManager = ctx.getSystemService(ACTIVITY_SERVICE) as ActivityManager
return activityManager.runningAppProcesses
Expand All @@ -36,6 +48,11 @@ object Process {
}
}

/**
* Get all processes running on a non-rooted device.
*
* @return List of ProcessData containing process information.
*/
private fun getAllProcessNoRoot(): List<ProcessData> {
return File("/proc")
.listFiles().orEmpty()
Expand All @@ -58,8 +75,10 @@ object Process {
}

/**
* Get the PID
* @return pid of process or null if process id is not found
* Get the PID of a process by its package name.
*
* @param pkg The package name of the process.
* @return The PID of the process or null if not found.
*/
fun getProcessID(pkg: String): Int? {
return File("/proc")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,5 @@ import kotlinx.parcelize.Parcelize

@Parcelize
data class ProcessData(val processName: String, val appName: String): Parcelable {
fun getDisplayName(): String {
return if (processName.contains(":"))
"$appName (${processName.substringAfter(":")})"
else
appName
}
fun getDisplayName() = if (processName.contains(":")) "$appName (${processName.substringAfter(":")})" else appName
}
22 changes: 20 additions & 2 deletions app/src/main/java/com/dumper/android/dumper/sofixer/Fixer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,21 @@ import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.runBlocking
import java.io.File

/**
* Class responsible for fixing ELF files using SoFixer.
*
* @property fixerPath The path to the SoFixer executable.
*/
class Fixer(private val fixerPath: String) {

/**
* Fix the dumped ELF file.
*
* @param startAddress The start address of the ELF file.
* @param archELF The architecture of the ELF file.
* @param outputFile The output file to be fixed.
* @param outLog The output handler for logging messages.
*/
fun fixELFFile(
startAddress: Long,
archELF: Arch,
Expand All @@ -32,7 +45,13 @@ class Fixer(private val fixerPath: String) {
}

/**
* Run SoFixer
* Run SoFixer to fix the dumped ELF file.
*
* @param arch The architecture of the ELF file.
* @param dumpFile The dumped ELF file to be fixed.
* @param startAddress The start address of the ELF file in hexadecimal format.
* @param onSuccess Callback function to handle success messages.
* @param onError Callback function to handle error messages.
*/
private fun fixDump(
arch: Arch,
Expand All @@ -49,7 +68,6 @@ class Fixer(private val fixerPath: String) {
"0x$startAddress"
)
)
.redirectErrorStream(true)
.start()

runBlocking {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import java.io.File
object FixerUtils {

/**
* Extract folder assets into filesDir and
* set permissions to 777 so the file can be executed
* Extracts folder assets into the application's files directory and
* sets permissions to 777 so the files can be executed.
*
* @param ctx The application context.
*/
fun extractLibs(ctx: Context) {
val filesDir = ctx.filesDir
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ class ConsoleViewModel : ViewModel() {

val console = MutableLiveData("")
val finishCode = MutableLiveData<Event<Int>>()

fun copyConsole(context: Context) {
val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val clip = ClipData.newPlainText("PADumper-Log", console.value)
clipboard.setPrimaryClip(clip)
Toast.makeText(context, "Log Copied!", Toast.LENGTH_SHORT).show()
}

fun append(text: String) {
console.value += text
}
Expand Down

0 comments on commit 4f0fbce

Please sign in to comment.