1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
  | 
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include "frida-gumjs.h"
#include "output.h"
#include "util.h"
char *output_stdout = NULL;
char *output_stderr = NULL;
static void output_redirect(int fd, char *filename) {
  char *path = NULL;
  if (filename == NULL) { return; }
  path = g_canonicalize_filename(filename, g_get_current_dir());
  FVERBOSE("Redirect %d -> '%s'", fd, path);
  int output_fd = open(path, O_RDWR | O_CREAT | O_TRUNC,
                       S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
  g_free(path);
  if (output_fd < 0) { FFATAL("Failed to open fd(%d) error %d", fd, errno); }
  if (dup2(output_fd, fd) < 0) {
    FFATAL("Failed to set fd(%d) error %d", fd, errno);
  }
  close(output_fd);
}
void output_config(void) {
  output_stdout = getenv("AFL_FRIDA_OUTPUT_STDOUT");
  output_stderr = getenv("AFL_FRIDA_OUTPUT_STDERR");
}
void output_init(void) {
  FOKF(cBLU "Output" cRST " - " cGRN "stdout:" cYEL " [%s]",
       output_stdout == NULL ? " " : output_stdout);
  FOKF(cBLU "Output" cRST " - " cGRN "stderr:" cYEL " [%s]",
       output_stderr == NULL ? " " : output_stderr);
  output_redirect(STDOUT_FILENO, output_stdout);
  output_redirect(STDERR_FILENO, output_stderr);
}
 
  |