Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix machine_id on windows to be like the steam client #1167

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion SteamKit2/SteamKit2/Util/HardwareUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,37 @@ sealed class WindowsMachineInfoProvider : IMachineInfoProvider
return Encoding.UTF8.GetBytes( guid.ToString()! );
}

public byte[]? GetMacAddress() => null;
// On windows, the steam client hashes a 16 bytes struct
// containing the mac address of the first *Physical* network adapter padded to 8 bytes (mac addresses are 6 bytes)
// and the mac address of the second *Physical* network adapter also padded to 8 bytes.
// So the hashed data ends up being (6bytes of mac address, 10 bytes of zeroes)
public byte[] GetMacAddress()
{
// This part of the code finds *Physical* network interfaces
// based on : https://social.msdn.microsoft.com/Forums/en-US/46c86903-3698-41bc-b081-fcf444e8a127/get-the-ip-address-of-the-physical-network-card-?forum=winforms
return NetworkInterface.GetAllNetworkInterfaces()
.Where( adapter =>
{
//Accessing the registry key corresponding to each adapter
string fRegistryKey =
$@"SYSTEM\CurrentControlSet\Control\Network\{{4D36E972-E325-11CE-BFC1-08002BE10318}}\{adapter.Id}\Connection";
RegistryKey? rk = Registry.LocalMachine.OpenSubKey( fRegistryKey, false );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remember right, registry opens need to be disposed of.

if ( rk == null ) return false;

return rk.GetValue( "PnpInstanceID", "" )?.ToString()?.Length > 3 && (rk.GetValue( "PnpInstanceID", "" )?.ToString()).StartsWith( "PCI" );
} )
.Select( networkInterface => networkInterface.GetPhysicalAddress().GetAddressBytes()
//pad all found mac addresses to 8 bytes
.Append( ( byte )0 )
.Append( ( byte )0 )
)
//add fallbacks in case less than 2 adapters are found
.Append( Enumerable.Repeat( ( byte )0, 8 ))
.Append( Enumerable.Repeat( ( byte )0, 8 ))
.Take( 2 )
.SelectMany( b => b )
.ToArray();
}

public byte[]? GetDiskId()
{
Expand Down