about summary refs log tree commit diff
path: root/frida_mode/util
diff options
context:
space:
mode:
Diffstat (limited to 'frida_mode/util')
-rw-r--r--frida_mode/util/bin2c.c117
-rwxr-xr-xfrida_mode/util/get_symbol_addr.sh32
2 files changed, 149 insertions, 0 deletions
diff --git a/frida_mode/util/bin2c.c b/frida_mode/util/bin2c.c
new file mode 100644
index 00000000..899d0101
--- /dev/null
+++ b/frida_mode/util/bin2c.c
@@ -0,0 +1,117 @@
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+void fatal(char *msg) {
+
+  perror(msg);
+  exit(1);
+
+}
+
+void bin2c_write(char *name, char *output, unsigned char *buff, size_t size) {
+
+  int fd = open(output, O_CREAT | O_WRONLY | O_TRUNC, 00660);
+  if (fd < 0) { fatal("open"); }
+
+  /* Write the array definition */
+  dprintf(fd, "unsigned char %s[] = {\n", name);
+
+  /* 12 bytes per row, just like xxd means we fit an 80 character width */
+  for (size_t i = 0; i < size; i += 12) {
+
+    for (size_t j = 0; j < 12; j++) {
+
+      size_t idx = i + j;
+
+      /* If we get to the end of the input, then break */
+      if (idx >= size) { break; }
+
+      /* If we are writing the first column, then we need a leading indent */
+      if (j == 0) { dprintf(fd, "  "); }
+
+      /* Write the hexadecimal byte value */
+      dprintf(fd, "0x%02x", buff[idx]);
+
+      /* If we have just written the last byte, then stop */
+      if (idx == size - 1) { break; }
+
+      /*
+       * If we have written the last byte in a row, then follow with a comma
+       * and a newline
+       */
+      if (j == 11) {
+
+        dprintf(fd, ",\n");
+
+        /*
+         * Otherwise, follow with a command and a space
+         */
+
+      } else {
+
+        dprintf(fd, ", ");
+
+      }
+
+    }
+
+  }
+
+  /* Write the closing brace for the array */
+  dprintf(fd, "\n};\n");
+
+  /* Write a parameter describing the length of the array */
+  dprintf(fd, "unsigned int %s_len = %lu;\n", name, size);
+
+  if (close(fd) < 0) { fatal("close"); }
+
+}
+
+void bin2c(char *name, char *input, char *output) {
+
+  int fd = open(input, O_RDONLY);
+  if (fd < 0) { fatal("open(input)"); }
+
+  size_t size = lseek(fd, 0, SEEK_END);
+  if (size < 0) { fatal("lseek(SEEK_END)"); }
+
+  if (lseek(fd, 0, SEEK_SET) < 0) { fatal("lseek(SEEK_SET)"); }
+
+  unsigned char *buff = malloc(size);
+  if (buff == NULL) { fatal("malloc(size)"); }
+
+  if (read(fd, buff, size) != size) { fatal("read(size)"); }
+
+  bin2c_write(name, output, buff, size);
+
+  free(buff);
+  if (close(fd) < 0) { fatal("close(fd_in)"); }
+
+}
+
+int main(int argc, char **argv) {
+
+  if (argc < 4) {
+
+    dprintf(STDERR_FILENO, "%s <name> <input> <output>\n", argv[0]);
+    return 1;
+
+  }
+
+  char *name = argv[1];
+  char *input = argv[2];
+  char *output = argv[3];
+
+  dprintf(STDOUT_FILENO, "bin2c:\n");
+  dprintf(STDOUT_FILENO, "\tname: %s\n", name);
+  dprintf(STDOUT_FILENO, "\tinput: %s\n", input);
+  dprintf(STDOUT_FILENO, "\toutput: %s\n", output);
+
+  bin2c(name, input, output);
+
+  return 0;
+
+}
+
diff --git a/frida_mode/util/get_symbol_addr.sh b/frida_mode/util/get_symbol_addr.sh
new file mode 100755
index 00000000..f5d8df91
--- /dev/null
+++ b/frida_mode/util/get_symbol_addr.sh
@@ -0,0 +1,32 @@
+#!/bin/bash
+# Copyright 2020 Google LLC
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# set -x
+target="$1"
+symbol="$2"
+base="$3"
+
+test -z "$target" -o -z "$symbol" -o '!' -e "$target" && exit 0
+
+test $(uname -s) = "Darwin" && symbol=_"$symbol"
+
+file "$target" | grep -q executable && {
+  nm "$target" | grep -i "T $symbol" | awk '{print"0x"$1}'
+  exit 0
+}
+
+hex_base=$(echo "$3" | awk '{sub("^0x","");print $0}' | tr a-f A-F )
+nm "$target" | grep -i "T $symbol" | awk '{print$1}' | tr a-f A-F | \
+  xargs echo "ibase=16;obase=10;$hex_base + " | bc | tr A-F a-f | awk '{print "0x"$0}'
+exit 0