From d30f7e875f0c98848195cbe2c7770aadbfc15638 Mon Sep 17 00:00:00 2001 From: John Klug Date: Tue, 21 Mar 2017 18:22:48 -0500 Subject: Update paths and remove bad comment --- hashpwd.cpp | 278 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/Makefile.am | 27 +++++- ubpasswd.sh | 108 ++++++++++++++++++++++ 3 files changed, 409 insertions(+), 4 deletions(-) create mode 100644 hashpwd.cpp create mode 100755 ubpasswd.sh diff --git a/hashpwd.cpp b/hashpwd.cpp new file mode 100644 index 0000000..79f0667 --- /dev/null +++ b/hashpwd.cpp @@ -0,0 +1,278 @@ +//============================================================================ +// Name : hashpwd.cpp +// Author : +// Version : +// Copyright : Your copyright notice +// Description : Hello World in C++, Ansi-style +//============================================================================ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +int v = 0; + +string magic_comp = "fc1c"; + +static const string base64_chars = + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789+/"; + +void escapeOutput(string &input, string &outstr) +{ + outstr.clear(); + int idx = 0; int lastnl = 0; + for (string::iterator it=input.begin(); it!=input.end(); ++it) { + stringstream conv; + lastnl = 0; + if (isprint(*it)) { + if (idx > 0) { + outstr += ' '; + idx++; + } + outstr += *it; + idx++; + if(idx > 74) { + outstr += '\n'; + lastnl = 1; + } + } else { + conv.str(string()); + conv << "\\x" << hex << setw(2) << ((unsigned int)*it & 0xff); + if (idx > 0) { + outstr += ' '; + idx++; + } + idx+=5; + outstr += conv.str(); + if(idx > 74) { + outstr += '\n'; + lastnl = 1; + } + } + } + if(lastnl) + if (outstr.size () > 0) outstr.resize (outstr.size () - 1); +} + +void binTo64(const unsigned char *data, unsigned int datalen, string &str, unsigned int strLen) +{ + unsigned int idx = 0; + int sex; + unsigned char incoming[3]; + int last; + + str.clear(); + (v == 1) && cout << "binTo64 datalen: " << datalen << endl; + (v == 1) && cout << "binTo64 strLen: " << strLen << endl; + for(unsigned int i=0; i= datalen) + break; + if (idx + 3 > datalen) { + memset(incoming,0,3); + memcpy(incoming,data+idx,datalen-idx); + last = datalen - idx - 1; + } else { + memcpy(incoming,data+idx,sizeof incoming); + last = 4; + } + idx += 3; + + (v == 1) && cout << hex << "in[0]: " << (int)incoming[0] << " in[1]: " << (int)incoming[1]<< " in[2] " << (int)incoming[2] << endl; + + sex = (incoming[0] >> 2); + str += base64_chars[sex]; + (v == 1) && cout << "1st, 4th inp char: " << hex << sex << " to: " << str << endl; + + sex = (incoming[1] >> 4); + (v == 1) && cout << "2nd, 5th inp char: " << hex << sex << endl; + sex += (incoming[0] << 4) & 0x30; + str += base64_chars[sex]; + (v == 1) && cout << "3rd, 6th inp char: " << hex << sex << " to: " << str << endl; + (v == 1) && cout << "last: " << last << endl; + if (last == 0) + continue; + sex = (incoming[2] >> 6); + sex += ((incoming[1] << 2) & 0x3c); + str += base64_chars[sex]; + + if(last == 1) + continue; + + sex = (incoming[2] & 0x3f); + str += base64_chars[sex]; + + (v == 1) && cout << "Converted " << idx << "chars: " << str << endl; + } + while (str.length() < strLen) + str += '='; + return; +} + +int badPassword(string &passwd) +{ + unsigned char a[3]; + int i = 0; + int lower=0, upper=0, numeric=0, special=0; + a[0] = a[1] = a[2] = 0; + for (string::iterator it=passwd.begin(); it!=passwd.end(); ++it) { + a[0] = *it; + if (i > 1) { + if ((a[0] == a[1]) && (a[1] == a[2])) + return 1; + if ((a[0] == a[1]+1) && (a[1] == a[2]+1)) + return 1; + if ((a[0] == a[1]-1) && (a[1] == a[2]-1)) + return 1; + } + a[2] = a[1]; + a[1] = a[0]; + if(islower(*it)) + lower=1; + if(isupper(*it)) + upper=1; + if(isdigit(*it)) + numeric=1; + if((*it == '/') || (*it == '+')) + special=1; + i++; + } + if (lower + upper + numeric + special < 3) + return 1; + return 0; +} + +void usage(void) +{ + cout << "usage:" << endl << + " mts-hashpwd [-v] [[-d did] [-m mac] | [-p password]] salt" << endl << + " -v verbose" << endl << + " -d did Device ID (serial #)" << endl << + " -m mac Ethernet mac address" << endl << + " -p password" << endl << + "Either password must be supplied, or Device-ID and Ethernet Mac address" << endl << + "salt is required." </dev/null 2>&1; then + echo "U-Boot does not support password protection." +fi + +if ((hassalt == 0)) ; then + salt="$(/bin/dd if=/dev/urandom count=1 bs=128 2>/dev/null | /bin/base64 | tr -d '\n' | cut -c1-${saltlen})" +fi + +echo "salt: $salt" + +((v == 1)) && echo upwd is $upwd +((v == 1)) && echo did length is ${#did} +if ((upwd == 0)) ; then + if ((${#did} == 0)) ; then + echo "${mts}/device-id must have a non-zero length value" + usage + fi + if ((${#mac} == 0)) ; then + echo "${mts}/mac-eth must have a non-zero length value" + usage + fi + if ((v == 1)) ; then + echo Try this: + echo "/sbin/hashpwd -d ${did} -m ${mac} ${salt}" + fi + result=$(/sbin/hashpwd -d ${did} -m ${mac} ${salt}) +else + result=$(/sbin/hashpwd -p "${pass}" ${salt}) +fi +if ! [[ $result =~ ^pass=([^[:space:]]+)[[:space:]]+password_hash=([^[:space:]]+) ]] ; then + echo "/sbin/hashpwd failed: ${result}" + exit 1 +fi +if ((v == 1)) ; then + echo result is: + echo "$result" +fi +pass="${BASH_REMATCH[1]}" +password_hash="${BASH_REMATCH[2]}" +echo "uboot password hash: \"$password_hash\"" +if ((ubonly == 0)) ; then + echo "setting root password to ${pass}" + echo -e "${pass}\n${pass}" | /usr/bin/passwd >/dev/null 2>&1 +fi +set -e +echo "u-boot password is ${pass}" +/usr/bin/u-boot setenv mtss "$salt" +/usr/bin/u-boot setenv mtsp "$password_hash" -- cgit v1.2.3