Simplify loops and catch more specific exceptions.

This commit is contained in:
Andy Nguyen 2021-10-26 20:22:19 +02:00
parent 3f94ab0869
commit bac2425948
2 changed files with 14 additions and 25 deletions

View File

@ -10,6 +10,7 @@ package com.bdjb;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
/** API class to access native data and execute native code. */ /** API class to access native data and execute native code. */
@ -356,7 +357,9 @@ public final class API {
handleField.setLong(nativeLibrary, handle); handleField.setLong(nativeLibrary, handle);
} }
return ((Long) findMethod.invoke(nativeLibrary, new Object[] {symbol})).longValue(); return ((Long) findMethod.invoke(nativeLibrary, new Object[] {symbol})).longValue();
} catch (Exception e) { } catch (IllegalAccessException e) {
return 0;
} catch (InvocationTargetException e) {
return 0; return 0;
} finally { } finally {
if (executableHandle != 0) { if (executableHandle != 0) {
@ -366,12 +369,8 @@ public final class API {
} }
public long addrof(Object obj) { public long addrof(Object obj) {
try { unsafe.putObject(LONG_VALUE, longValueOffset, obj);
unsafe.putObject(LONG_VALUE, longValueOffset, obj); return unsafe.getLong(LONG_VALUE, longValueOffset);
return unsafe.getLong(LONG_VALUE, longValueOffset);
} catch (Exception e) {
return 0;
}
} }
public byte read8(long addr) { public byte read8(long addr) {
@ -480,8 +479,7 @@ public final class API {
} }
public int strcmp(long s1, long s2) { public int strcmp(long s1, long s2) {
int i = 0; for (int i = 0; ; i++) {
while (true) {
byte b1 = read8(s1 + i); byte b1 = read8(s1 + i);
byte b2 = read8(s2 + i); byte b2 = read8(s2 + i);
if (b1 != b2) { if (b1 != b2) {
@ -490,14 +488,12 @@ public final class API {
if (b1 == (byte) 0 && b2 == (byte) 0) { if (b1 == (byte) 0 && b2 == (byte) 0) {
return 0; return 0;
} }
i++;
} }
} }
public int strcmp(long s1, String s2) { public int strcmp(long s1, String s2) {
byte[] bytes = toCBytes(s2); byte[] bytes = toCBytes(s2);
int i = 0; for (int i = 0; ; i++) {
while (true) {
byte b1 = read8(s1 + i); byte b1 = read8(s1 + i);
byte b2 = bytes[i]; byte b2 = bytes[i];
if (b1 != b2) { if (b1 != b2) {
@ -506,7 +502,6 @@ public final class API {
if (b1 == (byte) 0 && b2 == (byte) 0) { if (b1 == (byte) 0 && b2 == (byte) 0) {
return 0; return 0;
} }
i++;
} }
} }
@ -515,42 +510,36 @@ public final class API {
} }
public long strcpy(long dest, long src) { public long strcpy(long dest, long src) {
int i = 0; for (int i = 0; ; i++) {
while (true) {
byte ch = read8(src + i); byte ch = read8(src + i);
write8(dest + i, ch); write8(dest + i, ch);
if (ch == (byte) 0) { if (ch == (byte) 0) {
break; break;
} }
i++;
} }
return dest; return dest;
} }
public long strcpy(long dest, String src) { public long strcpy(long dest, String src) {
byte[] bytes = toCBytes(src); byte[] bytes = toCBytes(src);
int i = 0; for (int i = 0; ; i++) {
while (true) {
byte ch = bytes[i]; byte ch = bytes[i];
write8(dest + i, ch); write8(dest + i, ch);
if (ch == (byte) 0) { if (ch == (byte) 0) {
break; break;
} }
i++;
} }
return dest; return dest;
} }
public String readString(long src, int n) { public String readString(long src, long n) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
int i = 0; for (int i = 0; i < n; i++) {
while (true) {
byte ch = read8(src + i); byte ch = read8(src + i);
if (ch == (byte) 0 || i == n) { if (ch == (byte) 0) {
break; break;
} }
outputStream.write(new byte[] {ch}, 0, 1); outputStream.write(new byte[] {ch}, 0, 1);
i++;
} }
return outputStream.toString(); return outputStream.toString();
} }

View File

@ -84,7 +84,7 @@ class Exploit implements Runnable {
int ret = (int) api.call(payload, api.dlsym(API.LIBKERNEL_MODULE_HANDLE, "sceKernelDlsym")); int ret = (int) api.call(payload, api.dlsym(API.LIBKERNEL_MODULE_HANDLE, "sceKernelDlsym"));
Screen.println("[+] Result: " + ret); Screen.println("[+] Result: " + ret);
} catch (Exception e) { } catch (Exception e) {
Screen.println("[-] Error: " + e.getCause()); Screen.println("[-] Error: " + e.getMessage());
} }
} }
} }