frida-java-bridge icon indicating copy to clipboard operation
frida-java-bridge copied to clipboard

Memory.readUtf8String returns error UTF-8 when reading strings from memory

Open enovella opened this issue 7 years ago • 1 comments

Hi,

When intercepting a Java method that takes an Integer and String as arguments and returns another String (Lpackage/class;->method(Ljava/lang/String;I)Ljava/lang/String;), I got this frida error:

Error: invalid UTF-8
    at frida/node_modules/frida-java/lib/env.js:922
    at frida/node_modules/frida-java/lib/class-factory.js:2020
    at input:1

This seems to be caused by this code snippet: ((https://github.com/frida/frida-java/blob/master/lib/env.js#L922)) and it is triggered when trying to read UTF-8 string as shown below:

Env.prototype.stringFromJni = function (str) {
  const utf = this.getStringUtfChars(str);
  if (utf.isNull()) {
    throw new Error("Can't access the string.");
  }
  try {
    return Memory.readUtf8String(utf);
  } finally {
    this.releaseStringUtfChars(str, utf);
  }
};

Important to mention that try-catch-ing the printing in stdout or the entire hook does not prevent showing the error on screen.

Any idea how to fix this?

NB.- Using latest Frida version

enovella avatar Oct 20 '17 09:10 enovella

I'm getting a related, but different problem when I'm calling a method that returns a Java String BluetoothGattCharacteristic.getStringValue(). This is trying to convert it to a UTF-8 string, when the original string is an ASCII string.

This causes it to break on any UTF-8 special characters (e.g. 0xc0):

Error: can't decode byte 0xc0 in position 3
    at frida/node_modules/frida-java/lib/env.js:922
    at frida/node_modules/frida-java/lib/class-factory.js:2347
    at input:1

The code that's doing this is:

function newWriteCharacteristic(data)
{
	console.log("write: ", data.getUuid(), "\n", 
		" Data: ", shexdump(data.getStringValue(0),16));
	return this.writeCharacteristic(data);
}

Where shexdump is a simple function I stole from the Internets to convert a string to a hexdump.

tautology0 avatar Jun 01 '18 11:06 tautology0