From 54d195724f2c8ee3d79f02a6af7737ceaa152a83 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 25 May 2023 19:07:18 +0200 Subject: [PATCH] =?UTF-8?q?Inital=20Commit=20f=C3=BCr=20pwned-handler?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LICENSE | 25 ++++++++-- README.md | 22 ++++++++- csharp/App.config | 6 +++ csharp/Properties/AssemblyInfo.cs | 36 ++++++++++++++ csharp/README.md | 1 + csharp/pwned-search.cs | 49 +++++++++++++++++++ csharp/pwned-search.csproj | 79 +++++++++++++++++++++++++++++++ csharp/pwned-search.sln | 25 ++++++++++ pwned-search.ps1 | 22 +++++++++ pwned.py | 62 ++++++++++++++++++++++++ 10 files changed, 320 insertions(+), 7 deletions(-) create mode 100644 csharp/App.config create mode 100644 csharp/Properties/AssemblyInfo.cs create mode 100644 csharp/README.md create mode 100644 csharp/pwned-search.cs create mode 100644 csharp/pwned-search.csproj create mode 100644 csharp/pwned-search.sln create mode 100644 pwned-search.ps1 create mode 100644 pwned.py diff --git a/LICENSE b/LICENSE index 2071b23..cf1ab25 100644 --- a/LICENSE +++ b/LICENSE @@ -1,9 +1,24 @@ -MIT License +This is free and unencumbered software released into the public domain. -Copyright (c) +Anyone is free to copy, modify, publish, use, compile, sell, or +distribute this software, either in source code form or as a compiled +binary, for any purpose, commercial or non-commercial, and by any +means. -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +In jurisdictions that recognize copyright laws, the author or authors +of this software dedicate any and all copyright interest in the +software to the public domain. We make this dedication for the benefit +of the public at large and to the detriment of our heirs and +successors. We intend this dedication to be an overt act of +relinquishment in perpetuity of all present and future rights to this +software under copyright law. -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +For more information, please refer to diff --git a/README.md b/README.md index 6c7fa79..772b1e7 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,21 @@ -# pwnd-search-master +# pwned-search +Pwned Password API lookup -Client-Side Api Handler für have-i-been-pwned Anfragen. \ No newline at end of file +- Required libaries: + +* requests: `pip install requests` + +Usage: + +* `python pwned.py` – reads passwords from standard input; +* `python pwned.py <[file-with-passwords]` – reads passwords from + a file; +* `another-command | python pwned.py` – reads + passwords written to standard output by another command; +* `python pwned.py [password]` – checks passwords given as command line + arguments (beware the password may be saved in shell history and that + other users on the system ma be able to observe the command line). + +Thanks to those who fixed my dodgy code :) + +Have fun! Oh, and if you find one of your own passwords, change it asap! diff --git a/csharp/App.config b/csharp/App.config new file mode 100644 index 0000000..4d4e0f5 --- /dev/null +++ b/csharp/App.config @@ -0,0 +1,6 @@ + + + + + + diff --git a/csharp/Properties/AssemblyInfo.cs b/csharp/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..327fed4 --- /dev/null +++ b/csharp/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("pwned_search")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("pwned_search")] +[assembly: AssemblyCopyright("")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("16bc2ac9-dcda-44b2-aa3c-3537ce769319")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/csharp/README.md b/csharp/README.md new file mode 100644 index 0000000..72ea7c1 --- /dev/null +++ b/csharp/README.md @@ -0,0 +1 @@ +C# version diff --git a/csharp/pwned-search.cs b/csharp/pwned-search.cs new file mode 100644 index 0000000..2fdfbea --- /dev/null +++ b/csharp/pwned-search.cs @@ -0,0 +1,49 @@ +using System; +using System.Security.Cryptography; +using System.Text; +using System.Net; +using System.IO; + +namespace pwned_search { + class Program { + static void Main(string[] args) { + Console.Write("Enter the password to check: "); + string plaintext = Console.ReadLine(); + + SHA1 sha = new SHA1CryptoServiceProvider(); + byte[] data = sha.ComputeHash(Encoding.ASCII.GetBytes(plaintext)); + + // Loop through each byte of the hashed data + // and format each one as a hexadecimal string. + var sBuilder = new StringBuilder(); + for (int i = 0; i < data.Length; i++) { + sBuilder.Append(data[i].ToString("x2")); + } + string result = sBuilder.ToString().ToUpper(); + Console.WriteLine($"The SHA-1 hash of {plaintext} is: {result}"); + + // get a list of all the possible passwords where the first 5 digits of the hash are the same + string url = "https://api.pwnedpasswords.com/range/" + result.Substring(0, 5); + WebRequest request= WebRequest.Create(url); + Stream response = request.GetResponse().GetResponseStream(); + StreamReader reader = new StreamReader(response); + + // look at each possibility and compare the rest of the hash to see if there is a match + string hashToCheck = result.Substring(5); + while (true) { + string line = reader.ReadLine(); + if (line == null) { + Console.WriteLine("That password was not found."); + break; + } + string[] parts = line.Split(':'); + if (parts[0] == hashToCheck) { + Console.WriteLine("Password has been compromised -- DO NOT USE!"); + break; + } + } + + Console.ReadKey(); // pause until key press + } + } +} diff --git a/csharp/pwned-search.csproj b/csharp/pwned-search.csproj new file mode 100644 index 0000000..9403d87 --- /dev/null +++ b/csharp/pwned-search.csproj @@ -0,0 +1,79 @@ + + + + + Debug + AnyCPU + {16BC2AC9-DCDA-44B2-AA3C-3537CE769319} + Exe + pwned_search + pwned-search + v4.6.1 + 512 + true + publish\ + true + Disk + false + Foreground + 7 + Days + false + false + true + 0 + 1.0.0.%2a + false + false + true + + + AnyCPU + true + full + false + Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + False + Microsoft .NET Framework 4.6.1 %28x86 and x64%29 + true + + + False + .NET Framework 3.5 SP1 + false + + + + \ No newline at end of file diff --git a/csharp/pwned-search.sln b/csharp/pwned-search.sln new file mode 100644 index 0000000..270d9f7 --- /dev/null +++ b/csharp/pwned-search.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28307.421 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "pwned-search", "pwned-search.csproj", "{16BC2AC9-DCDA-44B2-AA3C-3537CE769319}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {16BC2AC9-DCDA-44B2-AA3C-3537CE769319}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {16BC2AC9-DCDA-44B2-AA3C-3537CE769319}.Debug|Any CPU.Build.0 = Debug|Any CPU + {16BC2AC9-DCDA-44B2-AA3C-3537CE769319}.Release|Any CPU.ActiveCfg = Release|Any CPU + {16BC2AC9-DCDA-44B2-AA3C-3537CE769319}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {8A16037A-221E-4C67-9C1A-348C35C8DA98} + EndGlobalSection +EndGlobal diff --git a/pwned-search.ps1 b/pwned-search.ps1 new file mode 100644 index 0000000..5a25afa --- /dev/null +++ b/pwned-search.ps1 @@ -0,0 +1,22 @@ +$string = Read-Host -Prompt 'Password to check' +$bytes = [System.Text.Encoding]::UTF8.GetBytes($string) +$sha1 = New-Object System.Security.Cryptography.SHA1CryptoServiceProvider +$data = $sha1.ComputeHash($bytes) +$result = ($data | ForEach-Object ToString X2) -join '' +$result = $result.ToUpper() +$head = $result.Substring(0,5) +$tail = $result.Substring(5) + +[Net.ServicePointManager]::SecurityProtocol = "TLS12, TLS11, TLS, SSL3" +$request = [System.Net.WebRequest]::Create("https://api.pwnedpasswords.com/range/" + $head) +$reader = New-Object System.IO.StreamReader(($request.GetResponse()).GetResponseStream()) + +$found = 0 +while (($line = $reader.ReadLine()) -ne $null) { + if ($line.Split(':')[0] -eq $tail) { + Write-Host "That password has been compromised." + $found = 1 + break + } +} +if ($found -eq 0) { Write-Host "That password was not found." } diff --git a/pwned.py b/pwned.py new file mode 100644 index 0000000..716b931 --- /dev/null +++ b/pwned.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python +import hashlib +import sys + +try: + import requests +except ModuleNotFoundError: + print("### pip install requests ###") + raise + + +def lookup_pwned_api(pwd): + """Returns hash and number of times password was seen in pwned database. + + Args: + pwd: password to check + + Returns: + A (sha1, count) tuple where sha1 is SHA-1 hash of pwd and count is number + of times the password was seen in the pwned database. count equal zero + indicates that password has not been found. + + Raises: + RuntimeError: if there was an error trying to fetch data from pwned + database. + UnicodeError: if there was an error UTF_encoding the password. + """ + sha1pwd = hashlib.sha1(pwd.encode('utf-8')).hexdigest().upper() + head, tail = sha1pwd[:5], sha1pwd[5:] + url = 'https://api.pwnedpasswords.com/range/' + head + res = requests.get(url) + if res.status_code != 200: + raise RuntimeError('Error fetching "{}": {}'.format( + url, res.status_code)) + hashes = (line.split(':') for line in res.text.splitlines()) + count = next((int(count) for t, count in hashes if t == tail), 0) + return sha1pwd, count + + +def main(args): + ec = 0 + for pwd in args or sys.stdin: + pwd = pwd.strip() + try: + sha1pwd, count = lookup_pwned_api(pwd) + + if count: + foundmsg = "{0} was found with {1} occurrences (hash: {2})" + print(foundmsg.format(pwd, count, sha1pwd)) + ec = 1 + else: + print("{} was not found".format(pwd)) + except UnicodeError: + errormsg = sys.exc_info()[1] + print("{0} could not be checked: {1}".format(pwd, errormsg)) + ec = 1 + continue + return ec + + +if __name__ == '__main__': + sys.exit(main(sys.argv[1:]))