-
Notifications
You must be signed in to change notification settings - Fork 0
/
5_2.c
113 lines (107 loc) · 2.83 KB
/
5_2.c
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <errno.h>
#define BUF_SIZE 256
#define sha "objcopy -j.sha "
#define restore "objcopy -R.sha "
#define ver1 "openssl dgst -sha256 -verify "
#define ver2 " -signature digest"
#define digest "digest"
#define safe " safe"
#define ok "OK"
#define failure "Failure"
int main(int argc, char **argv)
{
FILE *binary;
FILE *public_key;
bool legit = false;
char path[100];
if(argc < 3)
{
fprintf(stderr, "Usage: loader <secured binary> <public key>\n");
exit(1);
}
else if(argc == 3)
{
//Open binary
binary = fopen(argv[1], "rb");
if (!binary)
{
fprintf(stderr, "Unable to open binary %s\n", argv[1]);
return 1;
}
fclose(binary);
//Open key
public_key = fopen(argv[2], "rb");
if (!public_key)
{
fprintf(stderr, "Unable to open public_key %s\n", argv[2]);
return 1;
}
fclose(public_key);
FILE *fp;
char extract_sign[80] = sha;
strcat(extract_sign, argv[1]);
strcat(strcat(extract_sign, " "), digest);
char verify[80] = ver1;
strcat(strcat(verify, argv[2]), ver2);
strcat(verify, safe);
char exec[80] = restore;
strcat(strcat(exec, argv[1]), safe);
fp = popen(extract_sign, "r");
if (fp == NULL)
{
fprintf(stderr, "Failed to run command : %s\n", extract_sign);
perror("Error with popen");
exit(1);
}
/* close */
pclose(fp);
fp = popen(exec, "r");
if (fp == NULL)
{
fprintf(stderr, "Failed to run command : %s\n", extract_sign);
perror("Error with popen");
exit(1);
}
/* close */
pclose(fp);
printf("%s\n\n", verify);
fp = popen(verify, "r");
if (fp == NULL)
{
fprintf(stderr, "Failed to run command : %s\n", verify);
perror("Error with popen");
exit(1);
}
/* Read the output a line at a time - output it. */
while (fgets(path, sizeof(path)-1, fp) != NULL)
{
if(strstr(path, ok) != NULL)
{
legit = true;
break;
}
else if(strstr(path, failure) != NULL)
{
fprintf(stderr, "Failed while verificating the digest\n");
exit(1);
}
}
/* close */
pclose(fp);
if(legit)
{
system(safe);
}
}
else
{
fprintf(stderr, "Usage: loader <secured binary> <public key>\n");
exit(1);
}
return 0;
}