Slightly improve session fingerprinting (#2560)

* Slightly improve session fingerprinting

* Spelling
This commit is contained in:
Belle Aerni authored and GitHub committed 2025-01-20 16:07:15 -08:00
1 parent 692b7c40d9
commit 672c96cd5f
6 files changed
+63 -11

No files matched your search

+2 -1
View File
@@ -37,7 +37,8 @@
"jhurliman",
"jambroseclarke",
"Egulias",
"ICANN"
"ICANN",
"failovers"
],
"ignoreWords": [
+1 -1
View File
@@ -711,7 +711,7 @@ $di['license_server'] = function () use ($di) {
*
* @return \FOSSBilling\GeoIP\Reader
*/
$di['geoip'] = fn (): FOSSBilling\GeoIP\Reader => new FOSSBilling\GeoIP\Reader(PATH_LIBRARY . '/FOSSBilling/GeoIP/Databases/CC0-Country.mmdb');
$di['geoip'] = fn (): FOSSBilling\GeoIP\Reader => new FOSSBilling\GeoIP\Reader();
/*
* @param void
+34 -3
View File
@@ -76,10 +76,14 @@ class Fingerprint
'source' => $_SERVER['HTTP_SEC_CH_UA_MOBILE'] ?? '',
'weight' => 2,
],
'cloudFlareCountry' => [
'source' => $_SERVER['HTTP_CF_IPCOUNTRY'] ?? '', // "IP Geolocation" must be enabled under Cloudflare's "network" settings
'geoIpCountry' => [
'source' => $this->getIpCountry() ?? '',
'weight' => 4,
],
'asn' => [
'source' => $this->getIpAsn() ?? '',
'weight' => 1, // Things like Cloudflare, VPNs, or failovers might cause the ASN to change. Otherwise this would be a really strong indicator.
]
];
}
@@ -140,7 +144,7 @@ class Fingerprint
* Here we calculate how confident we are in our ability to fingerprint a device without causing issues for legitimate sessions.
* The less confident we are, the more wrong the current fingerprint needs to be when compared against the session's before it is invalidated.
*
* In the event that less that 70% of the possible values are in the fingerprint, we use the percentage off we are to calculate a higher percentage before failure.
* In the event that less that 70% of the possible values are in the fingerprint, we calculate a higher failure point for fingerprints due to reduced confidence.
* For example:
* If we have 13 possible fingerprint properties and only 9 are available, that's only 69% of the possible properties and the failure threshold will be calculated as follows.
* Negative weight: (1 - 0.69) / 1.25 = `0.24`
@@ -222,4 +226,31 @@ class Fingerprint
'userAgent' => $userAgent,
];
}
private function getIpCountry()
{
// Use the CF header if it is set.
if (isset($_SERVER['HTTP_CF_IPCOUNTRY'])) {
return $_SERVER['HTTP_CF_IPCOUNTRY'];
}
// Otherwise, instance the system's GeoIP reader and read the country from there.
try {
$reader = new GeoIP\Reader();
return $reader->country($_SERVER['REMOTE_ADDR'])->name;
} catch (\Exception) {
return '';
}
}
private function getIpAsn()
{
try {
$asnDb = GeoIP\Reader::getAsnDatabase();
$reader = new GeoIP\Reader($asnDb);
return $reader->asn($_SERVER['REMOTE_ADDR'])->asnNumber;
} catch (\Exception) {
return '';
}
}
}
+2 -2
View File
@@ -14,8 +14,8 @@ namespace FOSSBilling\GeoIP;
class ASN implements \JsonSerializable
{
private readonly int|float $asnNumber;
private readonly string $asnOrg;
public readonly int|float $asnNumber;
public readonly string $asnOrg;
public function __construct(array $asnRecord)
{
+21 -3
View File
@@ -23,17 +23,35 @@ class Reader
private readonly MaxMindReader $reader;
private readonly LanguageAlpha2 $language;
/**
* Returns the path to the system's `country` database
*/
public static function getCountryDatabase(): string
{
return PATH_LIBRARY . '/FOSSBilling/GeoIP/Databases/CC0-Country.mmdb';
}
/**
* Returns the path to the system's `asn` database
*/
public static function getAsnDatabase(): string
{
return PATH_LIBRARY . '/FOSSBilling/GeoIP/Databases/PDDL-ASN.mmdb';
}
/**
* Constructs a new GeoIP reader instance.
*
* @param string $database a path to the database to load
* @param string $locale (Optional) the locale to use for country names. Defaults to the locale being used by FOSSBilling.
* @param string|null $database (Optional) a path to the database to load> Will default to the system's country database.
* @param string|null $locale (Optional) the locale to use for country names. Defaults to the locale being used by FOSSBilling.
*
* @throws \InvalidArgumentException for invalid database path or unknown arguments
* @throws InvalidDatabaseException if the database is invalid or there is an error reading from it
*/
public function __construct(string $database, ?string $locale = null)
public function __construct(?string $database = null, ?string $locale = null)
{
$database ??= self::getCountryDatabase();
$this->reader = new MaxMindReader($database);
if ($locale === null) {
+3 -1
View File
@@ -14,6 +14,7 @@ namespace Box\Mod\Security;
use FOSSBilling\GeoIP\IncompleteRecord;
use FOSSBilling\InformationException;
use FOSSBilling\Interfaces\SecurityCheckInterface;
use FOSSBilling\GeoIP\Reader;
class Service
{
@@ -135,7 +136,8 @@ class Service
}
try {
$asnReader = new \FOSSBilling\GeoIP\Reader(PATH_LIBRARY . '/FOSSBilling/GeoIP/Databases/PDDL-ASN.mmdb');
$asnPath = Reader::getAsnDatabase();
$asnReader = new Reader($asnPath);
$asnInfo = $asnReader->asn($ip);
} catch (IncompleteRecord) {
$asnInfo = [];