aboutsummaryrefslogtreecommitdiffstats
path: root/tools/nv_data-sim-unlock.c
blob: b6eae488c9e42ce89a8a11fd7b6b6b33abcad1ea (plain)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
 * This file is part of libsamsung-ipc.
 *
 * Copyright (C) 2023 Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
 *
 * libsamsung-ipc is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * libsamsung-ipc is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with libsamsung-ipc.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <errno.h>
#include <fcntl.h>
#include <libgen.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>

void usage(char *progname)
{
	printf("Usage:\n");
	printf("\t%s FILE command <index>    # %s\n",
	       basename(progname),
	       "Get bruteforce command.");
	printf("\t%s FILE bruteforce <index> # %s\n",
	       basename(progname),
	       "Get bruteforce command.");
	printf("\t%s list-supported          # %s\n",
	       basename(progname),
	       "List supported devices/EFS.");
	printf("\t%s --help                  # %s\n",
	       basename(progname),
	       "Print this help.");
}

int print_byte(int fd, off_t base, uint8_t offset)
{
	off_t file_offset;
	ssize_t rd;
	char buf[1];
	int rc;

	bzero(&buf, sizeof(buf));

	file_offset = lseek(fd, base + offset, SEEK_SET);
	if (file_offset == -1) {
		int err = errno;
		printf("%s: lseek: %s\n", __func__, strerror(err));
		return EX_OSERR;
	}

	do {
		rd = read(fd, &buf, 1);
		if (rd == -1) {
			int err = errno;
			printf("%s: read: %s\n", __func__, strerror(err));
			return EX_OSERR;
		}
	} while (rd != 1);

	rc = printf("%02x", buf[0] & 0xff);
	if (rc < 0) {
		int err = errno;
		printf("%s: printf: %s\n", __func__, strerror(err));
		return EX_OSERR;
	}

	return 0;
}

off_t field_offset(unsigned field_nr)
{
	if (field_nr == 0)
		return 0x18146e;
	else if (field_nr == 1)
		return 0x18148e;
	else if (field_nr == 2)
		return 0x1814ce;
	else if (field_nr == 3)
		return 0x1814fe;
	else
		return 0;
}

int cmd_list_supported(void)
{

	printf("Supported devices:\n\t%s\n",
	       "Galaxy S II (GT-I9100)");

	return 0;
}


int cmd_command(char* nv_data_path, int field_nr)
{
	int fd;
	int rc;
	int i;
	off_t base;

	base = field_offset(field_nr);
	if (base == 0) {
		printf("%s: invalid field_offset: 0\n", __func__);
		return EX_SOFTWARE;
	}

	fd = open(nv_data_path, O_RDONLY);
	if (fd == -1) {
		printf("%s: open: %s\n", __func__, strerror(errno));
		return EX_NOINPUT;
	}

	printf("hashcat -m 110 --hex-salt -a 3 ");
	for (i=0; i<20 ; i++) {
		rc = print_byte(fd, base, i);
		if (rc)
			return rc;
	}

	rc = printf(":0000000000000000 ?d?d?d?d?d?d?d?d --show"
		    " | "
		    "cut -d : -f 3\n");
	if (rc == -1) {
		printf("%s: printf: %s\n", __func__, strerror(errno));
		return EX_OSERR;
	}

	rc = close (fd);
	if (fd == -1) {
		printf("%s: close: %s\n", __func__, strerror(errno));
		return EX_OSERR;
	}

	return 0;
}

int cmd_bruteforce()
{
	return 0;
}

int main(int argc, char * const argv[])
{
	if (argc == 4 && !strcmp(argv[2], "command")) {
		char *endptr;
		int field_nr = 0;

		field_nr = strtol(argv[3], &endptr, 10);
		if (errno != 0) {
			usage(argv[0]);
			return EX_USAGE;
		}

		if (!(argv[3][0] != '\0' && *endptr == '\0')) {
			usage(argv[0]);
			return EX_USAGE;
		}

		return cmd_command(argv[1], field_nr);
	} else if (argc == 4 && !strcmp(argv[2], "bruteforce")) {
		char *endptr;
		int field_nr = 0;

		field_nr = strtol(argv[3], &endptr, 10);
		if (errno != 0) {
			usage(argv[0]);
			return EX_USAGE;
		}

		if (!(argv[3][0] != '\0' && *endptr == '\0')) {
			usage(argv[0]);
			return EX_USAGE;
		}

		return cmd_bruteforce(argv[1], field_nr);
	} else if (argc == 2 && !strcmp(argv[1], "list-supported")) {
		return cmd_list_supported();
	} else if (argc == 2 && !strcmp(argv[1], "--help")) {
		usage(argv[0]);
		return 0;
	} else {
		usage(argv[0]);
		return EX_USAGE;
	}

}