Skip to content
This repository has been archived by the owner on Sep 2, 2021. It is now read-only.

Commit

Permalink
Getting machine hash(Win and Mac)
Browse files Browse the repository at this point in the history
  • Loading branch information
nethip committed Jun 2, 2017
1 parent 9cf19d6 commit 9d562ec
Show file tree
Hide file tree
Showing 5 changed files with 328 additions and 4 deletions.
8 changes: 7 additions & 1 deletion appshell/appshell_extensions.cpp
Expand Up @@ -752,7 +752,13 @@ class ProcessMessageDelegate : public ClientHandler::ProcessMessageDelegate {
error = InstallCommandLineTools();
}

} else {
} else if (message_name == "GetMachineHash") {
// Parameters:
// 0: int32 - callback id

responseArgs->SetString(2, GetSystemUniqueID());
}
else {
fprintf(stderr, "Native function not implemented yet: %s\n", message_name.c_str());
return false;
}
Expand Down
17 changes: 15 additions & 2 deletions appshell/appshell_extensions.js
Expand Up @@ -863,13 +863,26 @@ if (!appshell.app) {
*
* @param {number}
*
* @return int. The remote debugging port used by the appshell.
* @return none.
*/
native function InstallCommandLineTools();
appshell.app.installCommandLine = function (callback) {
InstallCommandLineTools(callback);
};


/**
* Get hash of the machine based on various
*
*
* @param {number}
*
* @return none.
*/
native function GetMachineHash();
appshell.app.getMachineHash = function (callback) {
GetMachineHash(callback || _dummyCallback);
};


// Alias the appshell object to brackets. This is temporary and should be removed.
brackets = appshell;
Expand Down
172 changes: 172 additions & 0 deletions appshell/appshell_extensions_mac.mm
Expand Up @@ -33,6 +33,26 @@

#include <sstream>

#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if_dl.h>
#include <ifaddrs.h>
#include <net/if_types.h>
#include <sys/resource.h>
#include <sys/utsname.h>
#include <mach-o/arch.h>

NSMutableArray* pendingOpenFiles;

@interface ChromeWindowsTerminatedObserver : NSObject
Expand Down Expand Up @@ -1675,3 +1695,155 @@ int32 GetArgvFromProcessID(int pid, NSString **argv)
ERROR_A:
return ERR_UNKNOWN;
}

//---------------------------------get MAC addresses ---------------------------------
// we just need this for purposes of unique machine id. So any one or two
// mac's is fine.
u16 HashMacAddress( u8* mac )
{
u16 hash = 0;

for ( u32 i = 0; i < 6; i++ )
{
hash += ( mac[i] << (( i & 1 ) * 8 ));
}
return hash;
}

void GetMacHash( u16& mac1, u16& mac2 )
{
mac1 = 0;
mac2 = 0;


struct ifaddrs* ifaphead;
if ( getifaddrs( &ifaphead ) != 0 )
return;

// iterate over the net interfaces
bool foundMac1 = false;
struct ifaddrs* ifap;
for ( ifap = ifaphead; ifap; ifap = ifap->ifa_next )
{
struct sockaddr_dl* sdl = (struct sockaddr_dl*)ifap->ifa_addr;
if ( sdl && ( sdl->sdl_family == AF_LINK ) && ( sdl->sdl_type == IFT_ETHER ))
{
if ( !foundMac1 )
{
foundMac1 = true;
mac1 = HashMacAddress( (u8*)(LLADDR(sdl))); //sdl->sdl_data) + sdl->sdl_nlen) );
} else {
mac2 = HashMacAddress( (u8*)(LLADDR(sdl))); //sdl->sdl_data) + sdl->sdl_nlen) );
break;
}
}
}

freeifaddrs( ifaphead );


// sort the mac addresses. We don't want to invalidate
// both macs if they just change order.
if ( mac1 > mac2 )
{
u16 tmp = mac2;
mac2 = mac1;
mac1 = tmp;
}
}

u16 GetVolumeHash()
{
// we don't have a 'volume serial number' like on windows.
// Lets hash the system name instead.
u8* sysname = (u8*)GetMachineName();
u16 hash = 0;

for ( u32 i = 0; sysname[i]; i++ )
hash += ( sysname[i] << (( i & 1 ) * 8 ));

return hash;
}

u16 GetCPUHash()
{
const NXArchInfo* info = NXGetLocalArchInfo();
u16 val = 0;
val += (u16)info->cputype;
val += (u16)info->cpusubtype;
return val;
}

u16 mask[5] = { 0x4e25, 0xf4a1, 0x5437, 0xab41, 0x0000 };

static void Smear(u16* id)
{
for (u32 i = 0; i < 5; i++)
for (u32 j = i; j < 5; j++)
if (i != j)
id[i] ^= id[j];

for (u32 i = 0; i < 5; i++)
id[i] ^= mask[i];
}

u16 mask[5] = { 0x4e25, 0xf4a1, 0x5437, 0xab41, 0x0000 };

static void Smear(u16* id)
{
for (u32 i = 0; i < 5; i++)
for (u32 j = i; j < 5; j++)
if (i != j)
id[i] ^= id[j];

for (u32 i = 0; i < 5; i++)
id[i] ^= mask[i];
}

static u16* ComputeSystemUniqueID()
{
static u16 id[5];
static bool computed = false;

if (computed) return id;

// produce a number that uniquely identifies this system.
id[0] = GetCPUHash();
id[1] = GetVolumeHash();
GetMacHash(id[2], id[3]);

// fifth block is some check digits
id[4] = 0;
for (u32 i = 0; i < 4; i++)
id[4] += id[i];

Smear(id);

computed = true;
return id;
}

std::string GetSystemUniqueID()
{
// get the name of the computer
std::string buf;

u16* id = ComputeSystemUniqueID();
for (u32 i = 0; i < 5; i++)
{
char num[16];
snprintf(num, 16, "%x", id[i]);
if (i > 0) {
buf = buf + "-";
}
switch (strlen(num))
{
case 1: buf = buf + "000"; break;
case 2: buf = buf + "00"; break;
case 3: buf = buf + "0"; break;
}
buf = buf + num;
}

return buf;
}
6 changes: 6 additions & 0 deletions appshell/appshell_extensions_platform.h
Expand Up @@ -59,6 +59,10 @@ static const int ERR_CL_TOOLS_NOTSUPPORTED = 17;

static const int ERR_PID_NOT_FOUND = -9999; // negative int to avoid confusion with real PIDs

typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;

#if defined(OS_WIN)
typedef std::wstring ExtensionString;
inline void* getMenuParent(CefRefPtr<CefBrowser>browser) {
Expand Down Expand Up @@ -176,3 +180,5 @@ int32 SetMenuItemShortcut(CefRefPtr<CefBrowser> browser, ExtensionString command
int32 GetMenuPosition(CefRefPtr<CefBrowser> browser, const ExtensionString& commandId, ExtensionString& parentId, int& index);

void DragWindow(CefRefPtr<CefBrowser> browser);

std::string GetSystemUniqueID();
129 changes: 128 additions & 1 deletion appshell/appshell_extensions_win.cpp
Expand Up @@ -35,6 +35,10 @@
#include <Shobjidl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <windows.h>
#include <intrin.h>
#include <iphlpapi.h>

#include "config.h"
#define CLOSING_PROP L"CLOSING"
#define UNICODE_MINUS 0x2212
Expand Down Expand Up @@ -2005,6 +2009,129 @@ void DragWindow(CefRefPtr<CefBrowser> browser) {
HWND browserHwnd = (HWND)getMenuParent(browser);
SendMessage(browserHwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0);
}


// Courtesy: Rafael.
// https://oroboro.com/unique-machine-fingerprint/

// we just need this for purposes of unique machine id.
// So any one or two mac's is fine.
u16 HasMacAddress(PIP_ADAPTER_INFO info)
{
u16 hash = 0;
for (u32 i = 0; i < info->AddressLength; i++)
{
hash += (info->Address[i] << ((i & 1) * 8));
}
return hash;
}

void GetMacHash(u16& mac1, u16& mac2)
{
IP_ADAPTER_INFO AdapterInfo[32];
DWORD dwBufLen = sizeof(AdapterInfo);

DWORD dwStatus = GetAdaptersInfo(AdapterInfo, &dwBufLen);
if (dwStatus != ERROR_SUCCESS)
return; // no adapters.

PIP_ADAPTER_INFO pAdapterInfo = AdapterInfo;
mac1 = HasMacAddress(pAdapterInfo);
if (pAdapterInfo->Next)
mac2 = HasMacAddress(pAdapterInfo->Next);

// sort the mac addresses. We don't want to invalidate
// both macs if they just change order.
if (mac1 > mac2)
{
u16 tmp = mac2;
mac2 = mac1;
mac1 = tmp;
}
}

u16 GetVolumeHash()
{
DWORD serialNum = 0;

char buffer[1024] = { 0 };
GetWindowsDirectoryA(buffer, 1024);

// Determine if this volume uses an NTFS file system.
GetVolumeInformation(NULL, NULL, 0, &serialNum, NULL, NULL, NULL, 0);
u16 hash = (u16)((serialNum + (serialNum >> 16)) & 0xFFFF);

return hash;
}

u16 GetCPUHash()
{
int cpuinfo[4] = { 0, 0, 0, 0 };
__cpuid(cpuinfo, 0);
u16 hash = 0;
u16* ptr = (u16*)(&cpuinfo[0]);
for (u32 i = 0; i < 8; i++)
hash += ptr[i];

return hash;
}

u16 mask[5] = { 0x4e25, 0xf4a1, 0x5437, 0xab41, 0x0000 };

static void Smear(u16* id)
{
for (u32 i = 0; i < 5; i++)
for (u32 j = i; j < 5; j++)
if (i != j)
id[i] ^= id[j];

for (u32 i = 0; i < 5; i++)
id[i] ^= mask[i];
}

static u16* ComputeSystemUniqueID()
{
static u16 id[5];
static bool computed = false;

if (computed) return id;

// produce a number that uniquely identifies this system.
id[0] = GetCPUHash();
id[1] = GetVolumeHash();
GetMacHash(id[2], id[3]);

// fifth block is some check digits
id[4] = 0;
for (u32 i = 0; i < 4; i++)
id[4] += id[i];

Smear(id);

computed = true;
return id;
}

std::string GetSystemUniqueID()
{
// get the name of the computer
std::string buf;

u16* id = ComputeSystemUniqueID();
for (u32 i = 0; i < 5; i++)
{
char num[16];
snprintf(num, 16, "%x", id[i]);
if (i > 0) {
buf = buf + "-";
}
switch (strlen(num))
{
case 1: buf = buf + "000"; break;
case 2: buf = buf + "00"; break;
case 3: buf = buf + "0"; break;
}
buf = buf + num;
}

return buf;
}

0 comments on commit 9d562ec

Please sign in to comment.