comparison target-utils/tf-breakin/mkembed.c @ 357:22c6e39e1789

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