This repository has been archived by the owner on Dec 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
pwn_chk.c
196 lines (169 loc) · 4.35 KB
/
pwn_chk.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
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
/*
* pwn_chk.c - Test an SHA1 hash against the HaveIBeenPwnd API.
*
* Steve
*
*/
#define _XOPEN_SOURCE 700 /* For getline() */
#include <stdio.h>
#include <stdlib.h>
#include <curl/curl.h>
#include <string.h>
#include <syslog.h>
#include <sys/stat.h>
#include <ctype.h>
/*
* This callback-function is invoked when fresh data has been downloaded
* via libcurl.
*/
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
size_t written = fwrite(ptr, size, nmemb, stream);
return written;
}
/*
* Test to see if the given SHA1-hash is known to the HaveIBeenPwnd site.
*
* Return value:
*
* <0: Error performing the test.
* 0: No leak.
* >0: Leaked.
*
*/
int was_leaked(char *hash)
{
/*
* Sanity-check that our input is valid.
*/
if (hash == NULL || strlen(hash) != 40)
{
openlog("pam_pwnd", 0, 0);
syslog(LOG_ERR, "pam_pwnd: Invalid input for was_leaked(%s).", hash);
closelog();
return -1;
}
/*
* We're going to make a request via Curl, so we need a curl
* object/structure for interfacing with that library.
*/
CURL *curl;
CURLcode res;
/*
* For the HaveIBeenPwnd API we need to make a request with the
* first five characters of the SHA1-hash, and then look for the
* rest of the hash in the response.
*
* Extract the first five characters into one buffer, and the rest
* into another.
*/
char start[6] = {'\0'};
strncpy(start, hash, 5);
char rest[40] = {'\0'};
strncpy(rest, hash + 5, sizeof(rest) - 1);
/*
* Our hash-prefix should be upper-cased.
*/
for (unsigned int i = 0; i < strlen(start); i++)
{
start[i] = toupper(start[i]);
}
/*
* As should our suffix.
*/
for (unsigned int i = 0; i < strlen(rest); i++)
{
rest[i] = toupper(rest[i]);
}
/*
* The URL we're fetching.
*/
char url[100] = {'\0'};
snprintf(url, sizeof(url) - 1,
"https://api.pwnedpasswords.com/range/%s", start);
/*
* Create a temporary file to hold the response - note that we
* setup our umask first such that the file will be readable
* only by the owner.
*/
umask(077);
FILE * fd = tmpfile();
/*
* Initialize curl.
*/
curl = curl_easy_init();
if (!curl)
{
openlog("pam_pwnd", 0, 0);
syslog(LOG_ERR, "pam_pwnd: Failed to setup curl.");
closelog();
fclose(fd);
return -1;
}
/*
* Setup the request.
*/
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fd);
/*
* Perform the request - this is blocking.
*/
long res_code = 0;
res = curl_easy_perform(curl);
/*
* Did we receive an error from the remote call?
*/
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &res_code);
if (!(res_code == 200) && res != CURLE_ABORTED_BY_CALLBACK)
{
/*
* Log the return-code.
*/
openlog("pam_pwnd", 0, 0);
syslog(LOG_ERR, "pam_pwnd: HTTP Response code was not 200: %ld", res_code);
closelog();
curl_easy_cleanup(curl);
fclose(fd);
return -1;
}
/*
* Cleanup the curl request.
*/
curl_easy_cleanup(curl);
/*
* At this point we should have a temporary-file containing the result
* of the request which will be a series of lines of the form:
*
* HASH:COUNT
*
* "HASH" is the SHA1-hash (minus the first five characters).
*
* "COUNT" is the number of times the hash was leaked (we're going to
* ignore that, we just care if the password-hash is present at all).
*
*/
/*
* Rewind the file-handle and scan the contents, line-by-line.
*/
rewind(fd);
char * line = NULL;
size_t len = 0;
ssize_t read;
int found = 0;
while ((read = getline(&line, &len, fd)) != -1)
{
if (strncmp(rest, line, strlen(rest)) == 0)
{
openlog("pam_pwnd", 0, 0);
syslog(LOG_ERR, "pam_pwnd: Password hash is known-leaked: %s", hash);
closelog();
found = 1;
}
}
/*
* Close the file-handle, which should trigger an unlink too.
*/
fclose(fd);
return found;
}