annotate target-utils/tf-breakin/mkembed.c @ 983:7166c8311b0d

tfc139 reworked to support both ARM and Thumb entry
author Mychaela Falconia <falcon@ivan.Harhan.ORG>
date Thu, 10 Dec 2015 08:07:47 +0000
parents 22c6e39e1789
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
357
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
1 #include <sys/types.h>
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
2 #include <sys/file.h>
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
3 #include <sys/stat.h>
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
4 #include <stdio.h>
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
5 #include <stdlib.h>
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
6 #include <unistd.h>
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
7
983
7166c8311b0d tfc139 reworked to support both ARM and Thumb entry
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents: 357
diff changeset
8 #define PAYLOAD_SIZE 116
357
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
9 u_char payload_buf[PAYLOAD_SIZE];
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
10
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
11 read_binary(filename)
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
12 char *filename;
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
13 {
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
14 int fd;
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
15 struct stat st;
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
16
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
17 fd = open(filename, O_RDONLY);
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
18 if (fd < 0) {
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
19 perror(filename);
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
20 exit(1);
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
21 }
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
22 fstat(fd, &st);
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
23 if (!S_ISREG(st.st_mode)) {
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
24 fprintf(stderr, "error: %s is not a regular file\n", filename);
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
25 exit(1);
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
26 }
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
27 if (st.st_size != PAYLOAD_SIZE) {
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
28 fprintf(stderr, "error: %s size mismatch\n", filename);
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
29 exit(1);
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
30 }
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
31 if (read(fd, payload_buf, PAYLOAD_SIZE) != PAYLOAD_SIZE) {
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
32 perror("read error");
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
33 exit(1);
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
34 }
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
35 close(fd);
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
36 }
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
37
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
38 write_output(filename)
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
39 char *filename;
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
40 {
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
41 FILE *of;
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
42 int i, j, idx;
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
43
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
44 of = fopen(filename, "w");
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
45 if (!of) {
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
46 perror(filename);
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
47 exit(1);
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
48 }
983
7166c8311b0d tfc139 reworked to support both ARM and Thumb entry
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents: 357
diff changeset
49 fprintf(of, "u_char shellcode[%d] = {\n", PAYLOAD_SIZE);
357
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
50 idx = 0;
983
7166c8311b0d tfc139 reworked to support both ARM and Thumb entry
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents: 357
diff changeset
51 for (i = 0; i < 15; i++) {
357
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
52 for (j = 0; j < 8; j++) {
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
53 if (j)
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
54 putc(' ', of);
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
55 else
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
56 putc('\t', of);
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
57 fprintf(of, "0x%02X,", payload_buf[idx++]);
983
7166c8311b0d tfc139 reworked to support both ARM and Thumb entry
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents: 357
diff changeset
58 if (idx >= PAYLOAD_SIZE)
7166c8311b0d tfc139 reworked to support both ARM and Thumb entry
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents: 357
diff changeset
59 break;
357
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
60 }
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
61 putc('\n', of);
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
62 }
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
63 fputs("};\n", of);
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
64 fclose(of);
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
65 }
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
66
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
67 main(argc, argv)
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
68 char **argv;
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
69 {
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
70 if (argc != 3) {
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
71 fprintf(stderr, "usage: %s payload.bin output.c\n", argv[0]);
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
72 exit(1);
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
73 }
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
74 read_binary(argv[1]);
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
75 write_output(argv[2]);
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
76 exit(0);
22c6e39e1789 target-utils/tf-breakin: build embeddable form of the payload
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
77 }