Browse Source

Initial commit

Piotr Czajkowski 6 years ago
commit
6dfd4df247
8 changed files with 222 additions and 0 deletions
  1. 11 0
      LICENSE.md
  2. 9 0
      README.md
  3. 71 0
      curl.c
  4. 10 0
      curl.h
  5. 11 0
      makefile
  6. 77 0
      password.c
  7. 27 0
      sha.c
  8. 6 0
      sha.h

+ 11 - 0
LICENSE.md

@@ -0,0 +1,11 @@
+Copyright 2018- Piotr Czajkowski
+
+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+
+1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+
+2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
+
+3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

+ 9 - 0
README.md

@@ -0,0 +1,9 @@
+# Pwned Password 
+
+Fun project to utilize awesome [';--have i been pwned?](https://haveibeenpwned.com/Passwords) password API. It generates SHA1 hash for the password you provide as argument and then grabs the range, basing on first 5 characters of the hash, from the service and checks if your hash is there. So your password, nor its hash, never leaves your computer.
+
+Usage is simple:
+	password <password>
+
+It'll print ***Your password is well known!*** if your password is in ';--have i been pwned? DB. Enjoy!
+

+ 71 - 0
curl.c

@@ -0,0 +1,71 @@
+#include "curl.h"
+
+// Borrowed from https://curl.haxx.se/libcurl/c/getinmemory.html
+
+static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp) {
+  size_t realsize = size * nmemb;
+  struct MemoryStruct *mem = (struct MemoryStruct *)userp;
+ 
+  mem->memory = realloc(mem->memory, mem->size + realsize + 1);
+  if(mem->memory == NULL) 
+       return 0;
+ 
+  memcpy(&(mem->memory[mem->size]), contents, realsize);
+  mem->size += realsize;
+  mem->memory[mem->size] = 0;
+ 
+  return realsize;
+}
+
+char *getData(char *url) {
+  CURL *curl_handle;
+  CURLcode res;
+ 
+  struct MemoryStruct chunk;
+ 
+  chunk.memory = malloc(1);  /* will be grown as needed by the realloc above */ 
+  chunk.size = 0;    /* no data at this point */ 
+ 
+  curl_global_init(CURL_GLOBAL_ALL);
+ 
+  /* init the curl session */ 
+  curl_handle = curl_easy_init();
+ 
+  /* specify URL to get */ 
+  curl_easy_setopt(curl_handle, CURLOPT_URL, url);
+
+  curl_easy_setopt(curl_handle, CURLOPT_NOPROXY, "*");
+ 
+  /* send all data to this function  */ 
+  curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
+ 
+  /* we pass our 'chunk' struct to the callback function */ 
+  curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
+ 
+  /* some servers don't like requests that are made without a user-agent
+     field, so we provide one */ 
+  curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
+ 
+  /* get it! */ 
+  res = curl_easy_perform(curl_handle);
+ 
+  /* check for errors */ 
+  if(res != CURLE_OK) {
+    fprintf(stderr, "curl_easy_perform() failed: %s\n",
+            curl_easy_strerror(res));
+  }
+
+  size_t dataSize = strlen(chunk.memory);
+  char *data = calloc(dataSize + 1, 1);
+  data = strncpy(data, chunk.memory, dataSize);  
+ 
+  /* cleanup curl stuff */ 
+  curl_easy_cleanup(curl_handle);
+ 
+  free(chunk.memory);
+ 
+  /* we're done with libcurl, so clean it up */ 
+  curl_global_cleanup();
+ 
+  return data;
+}

+ 10 - 0
curl.h

@@ -0,0 +1,10 @@
+#include <stdlib.h>
+#include <string.h>
+#include <curl/curl.h>
+ 
+struct MemoryStruct {
+  char *memory;
+  size_t size;
+};
+
+char *getData();

+ 11 - 0
makefile

@@ -0,0 +1,11 @@
+CFLAGS=-g -Wall -Wextra -O3 -std=gnu99
+LDLIBS=-lcurl -lssl -lcrypto
+objects=curl.o sha.o
+
+password: $(objects)
+
+.PHONY: clean
+clean:
+	rm *.o
+
+default: password

+ 77 - 0
password.c

@@ -0,0 +1,77 @@
+#include <ctype.h>
+#include "curl.h"
+#include "sha.h"
+
+// https://haveibeenpwned.com/API/v2#SearchingPwnedPasswordsByRange
+
+#define HASH_PREFIX_SIZE 5
+#define HASH_SUFFIX_SIZE 35
+#define URL_SIZE 43
+
+char *getURL(char* hash) {
+	const char *baseURL = "https://api.pwnedpasswords.com/range/";
+	char *url = calloc(URL_SIZE, 1);
+	strncpy(url, baseURL, strlen(baseURL));
+	strncat(url, hash, HASH_PREFIX_SIZE);
+
+	return url;
+}
+
+char *getSuffixUppercase(char *hash) {
+	char hashSuffix[HASH_SUFFIX_SIZE + 1];
+	strncpy(hashSuffix, hash+HASH_PREFIX_SIZE, HASH_SUFFIX_SIZE);
+
+	char *suffixUpper = malloc(HASH_SUFFIX_SIZE + 1);
+	for(int i = 0; i < HASH_SUFFIX_SIZE; i++) {
+		int c = hashSuffix[i];
+		if (islower(c)) c = toupper(c);
+		sprintf(suffixUpper+i, "%c", c);
+	}
+
+	return suffixUpper;
+}
+
+int findSuffix(char *suffix, char *data) {
+	int found = 0;
+	int check = 1;
+	
+	int suffixCount = 0;
+	for(unsigned long i = 0; i < strlen(data); i++) {
+		if (found) return found;
+		
+		if (check) {
+			if (data[i] == ':') found = 1;
+			if (suffix[suffixCount] != data[i]) check = 0;
+			suffixCount++;
+		}
+		
+		if (data[i] == '\n') {
+			check = 1;
+			suffixCount = 0;
+		}
+	}
+	return found;
+}
+
+void usage(char *app) {
+	printf("Usage:\n%s <password>\n", app);
+}
+
+int main(int argc, char **argv) {
+	if (argc < 2) {
+		usage(argv[0]);
+		return 1;
+	}
+
+	char *hash = getHash(argv[1]);
+	char *url = getURL(hash);
+	char *suffix = getSuffixUppercase(hash);
+	free(hash);
+
+	char *data = getData(url);
+	free(url);
+	
+	if (findSuffix(suffix, data)) puts("Your password is well known!");
+	free(data);
+	free(suffix);
+}

+ 27 - 0
sha.c

@@ -0,0 +1,27 @@
+#include "sha.h"
+
+static unsigned char *generateHash(char *string, size_t length) {
+	unsigned char *hash = malloc(SHA_DIGEST_LENGTH);
+	SHA1((unsigned char*)string, length, hash);
+
+	return hash;
+}
+
+static char *hashToString(unsigned char *hash) {
+	char *string = malloc(2 * SHA_DIGEST_LENGTH + 1);
+
+	for(int i = 0; i < SHA_DIGEST_LENGTH; i++)
+		sprintf(string+(i*2), "%02x", hash[i]);
+
+	return string;
+}
+
+char *getHash(char *string) {
+	size_t length = strlen(string);
+	unsigned char *hash = generateHash(string, length);
+	char *hashString = hashToString(hash);
+	free(hash);
+
+	return hashString;
+}
+

+ 6 - 0
sha.h

@@ -0,0 +1,6 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <openssl/sha.h>
+
+char *getHash(char *string);