Skip to content

Commit

Permalink
Fix write and add types for those events, fixing some other type bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
radare committed Nov 19, 2023
1 parent 45eae99 commit 2369267
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
30 changes: 21 additions & 9 deletions src/agent/io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ function invalidateCachedRanges() {
Script.nextTick(() => { cachedRanges = [] ; });
}

export function read(params: any) {
const { offset, count, fast } = params;
export function read(params: R2FIOReadParameters) {
const { offset, count } = params;
const fast = false;
if (typeof r2frida.hookedRead === 'function') {
return r2frida.hookedRead(offset, count);
}
Expand All @@ -26,19 +27,20 @@ export function read(params: any) {
}
return [{}, []];
}

const o = ptr(offset);
if (config.getBoolean('io.safe')) {
try {
if (cachedRanges.length === 0) {
const foo = (map: RangeDetails) : PointerPair => [map.base, map.base.add(map.size)];
cachedRanges = Process.enumerateRanges('').map(foo);
}
const o = ptr(offset);
for (const map of cachedRanges) {
if (o.compare(map[0]) >= 0 && o.compare(map[1]) < 0) {
let left = count;
if (o.add(count).compare(map[1]) > 0) {
const rest = o.add(count).sub(map[1]);
left = left.sub(rest);
left = left - rest.toUInt32();
}
const bytes = o.readByteArray(left);
return [{}, (bytes !== null) ? bytes : []];
Expand All @@ -49,7 +51,7 @@ export function read(params: any) {
// console.error('safeio-read', e);
}
}
if (offset < 2) {
if (o.compare(2) < 0) {
return [{}, []];
}
try {
Expand Down Expand Up @@ -80,8 +82,18 @@ function isExecutable(address: NativePointer) : boolean {
return currentRange.protection.indexOf('x') !== -1;
}

export function write(params: any, data: any) {
const ptroff = params.offset;
interface R2FIOWriteParameters {
offset: string;
}
interface R2FIOReadParameters {
offset: string;
count: number;
fast: boolean;
}

export function write(params: R2FIOWriteParameters, data: any) {
const ptroff = ptr(params.offset);
console.log(typeof data);
if (typeof r2frida.hookedWrite === 'function') {
return r2frida.hookedWrite(ptroff, data);
}
Expand All @@ -91,10 +103,10 @@ export function write(params: any, data: any) {
p.writeByteArray(data);
});
} else {
params.offset.writeByteArray(data);
ptroff.writeByteArray(data);
}
} else {
params.offset.writeByteArray(data);
ptroff.writeByteArray(data);
}
return [{}, null];
}
5 changes: 3 additions & 2 deletions src/agent/lib/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ export function searchJson(args: string[]): SearchHit[] {
hits.forEach(hit => {
try {
const bytes = io.read({
offset: hit.address,
count: 60
offset: hit.address.toString(),
count: 60,
fast : false
})[1];
hit.content = filterPrintable(bytes);
} catch (e) {
Expand Down
2 changes: 2 additions & 0 deletions src/io_frida.c
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,8 @@ static int __read(RIO *io, RIODesc *fd, ut8 *buf, int count) {
add_offset_parameter (builder, io->off);
json_builder_set_member_name (builder, "count");
json_builder_add_int_value (builder, count);
json_builder_set_member_name (builder, "fast");
json_builder_add_boolean_value (builder, false);

JsonObject *result = perform_request (rf, builder, NULL, &bytes);
if (!result) {
Expand Down

0 comments on commit 2369267

Please sign in to comment.