-
Notifications
You must be signed in to change notification settings - Fork 39
/
shadow_hash.cpp
121 lines (84 loc) · 2.95 KB
/
shadow_hash.cpp
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
#include "shadow_hash.h"
int ShadowData::loadUsers(){
#ifdef __APPLE__
#endif // __APPLE__
return 0;
}
int hashDataForUser(std::string user, HashData *someHashData){
memset(someHashData, 0, sizeof(HashData));
#ifdef __APPLE__
char filename[256] = "/var/db/dslocal/nodes/Default/users/";
strcat(filename, user.c_str());
strcat(filename, ".plist");
if (0 != hashDataFromPlist(filename, someHashData)){
printf("Could not open shadow file.\n");
exit(-1);
}
#else // Not __APPLE__
char line[2048];
FILE* file = fopen("/etc/shadow", "r");
if (!file) {
fprintf(stderr, "error: could not open /etc/shadow\n");
return -1;
}
while (fgets(line, sizeof(line), file)) {
char* pch;
int x = 0;
pch = strtok (line,":");
while (pch != NULL)
{
if(x == 0){
if(0 == strcmp(user.c_str(), pch)){
pch = strtok (NULL, ":");
strcpy(someHashData->cryptHash, pch);
for (int x = strlen(someHashData->cryptHash); x >= 0; x--) {
if (someHashData->cryptHash[x] == '$'){
memcpy(someHashData->cryptSalt, &someHashData->cryptHash, x+1);
someHashData->cryptSalt[x+1] = '\0';
return 0;
}
}
}
}
pch = strtok (NULL, ":");
x++;
}
}
fclose(file);
#endif
return -1;
}
void dumpHashData(std::string user){
#ifdef __APPLE__
std::vector<HashData> theHashes;
std::string filename = "/var/db/dslocal/nodes/Default/users/" + user + ".plist";
if (-1 == hashDataFromPlist(filename, &theHashes)){
std::cout << "I could not find that user." << std::endl;
return;
}
for(int i=0; i < theHashes.size(); i++){
dumpHashData(&theHashes[i]);
}
#endif
}
void dumpHashcat(std::string user){
#ifdef __APPLE__
std::vector<HashData> theHashes;
std::string filename = "/var/db/dslocal/nodes/Default/users/" + user + ".plist";
if (-1 == hashDataFromPlist(filename, &theHashes)){
std::cout << "I could not find that user." << std::endl;
return;
}
for(int i=0; i < theHashes.size(); i++){
if (strncmp(theHashes[i].appleLabel, "SALTED-SHA512-PBKDF2", strlen(theHashes[i].appleLabel)) == 0){
std::cout << "$ml$" << theHashes[i].rounds;
std::cout << "$" << dumpHex(theHashes[i].salt, theHashes[i].saltLength);
// Hashcat only needs half of the hash... or the hash I'm showing is half garbage.... :(
std::cout << "$" << dumpHex(theHashes[i].hash, theHashes[i].hashLength/2);
std::cout << std::endl;
return;
}
}
std::cout << "I found the user but I couldn't find a compatable hash." << std::endl;
#endif
}