get-phishing-domains.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import picocolors from 'picocolors';
  2. import { parse } from 'tldts-experimental';
  3. import { appendArrayInPlaceCurried } from 'foxts/append-array-in-place';
  4. import { dummySpan } from '../trace';
  5. import type { TldTsParsed } from './normalize-domain';
  6. import { loosTldOptWithPrivateDomains } from '../constants/loose-tldts-opt';
  7. import { BLACK_TLD, WHITELIST_MAIN_DOMAINS, leathalKeywords, lowKeywords, sensitiveKeywords } from '../constants/phishing-score-source';
  8. import { PHISHING_DOMAIN_LISTS_EXTRA, PHISHING_HOSTS_EXTRA } from '../constants/reject-data-source';
  9. import { processHostsWithPreload } from './parse-filter/hosts';
  10. import { processDomainListsWithPreload } from './parse-filter/domainlists';
  11. import process from 'node:process';
  12. export function getPhishingDomains(isDebug = false): Promise<string[]> {
  13. return dummySpan.traceChild('get phishing domains').traceAsyncFn(async (span) => span.traceChildAsync(
  14. 'process phishing domain set',
  15. async () => {
  16. const downloads = [
  17. ...PHISHING_DOMAIN_LISTS_EXTRA.map(entry => processDomainListsWithPreload(...entry)),
  18. ...PHISHING_HOSTS_EXTRA.map(entry => processHostsWithPreload(...entry))
  19. ];
  20. const domainArr: string[] = [];
  21. const domainGroups = await Promise.all(downloads.map(task => task(dummySpan)));
  22. domainGroups.forEach(appendArrayInPlaceCurried(domainArr));
  23. const domainCountMap = new Map<string, number>();
  24. const domainScoreMap: Record<string, number> = Object.create(null) as Record<string, number>;
  25. let line: string;
  26. let tld: string | null;
  27. let apexDomain: string | null;
  28. let subdomain: string | null;
  29. let parsed: TldTsParsed;
  30. for (let i = 0, len = domainArr.length; i < len; i++) {
  31. line = domainArr[i];
  32. parsed = parse(line, loosTldOptWithPrivateDomains);
  33. if (parsed.isPrivate) {
  34. continue;
  35. }
  36. tld = parsed.publicSuffix;
  37. apexDomain = parsed.domain;
  38. if (!tld) {
  39. console.log(picocolors.yellow('[phishing domains] E0001'), 'missing tld', { line, tld });
  40. continue;
  41. }
  42. if (!apexDomain) {
  43. console.log(picocolors.yellow('[phishing domains] E0002'), 'missing domain', { line, apexDomain });
  44. continue;
  45. }
  46. if (WHITELIST_MAIN_DOMAINS.has(apexDomain)) {
  47. continue;
  48. }
  49. domainCountMap.set(
  50. apexDomain,
  51. domainCountMap.has(apexDomain)
  52. ? domainCountMap.get(apexDomain)! + 1
  53. : 1
  54. );
  55. let score = 0;
  56. if (apexDomain in domainScoreMap) {
  57. score = domainScoreMap[apexDomain];
  58. } else {
  59. if (BLACK_TLD.has(tld)) {
  60. score += 3;
  61. } else if (tld.length > 4) {
  62. score += 2;
  63. } else if (tld.length > 5) {
  64. score += 4;
  65. }
  66. if (apexDomain.length >= 18) {
  67. score += 0.5;
  68. }
  69. }
  70. subdomain = parsed.subdomain;
  71. if (subdomain) {
  72. score += calcDomainAbuseScore(subdomain, line);
  73. }
  74. domainScoreMap[apexDomain] = score;
  75. }
  76. domainCountMap.forEach((count, apexDomain) => {
  77. const score = domainScoreMap[apexDomain];
  78. if (
  79. (score >= 24)
  80. || (score >= 16 && count >= 7)
  81. || (score >= 13 && count >= 11)
  82. || (score >= 5 && count >= 14)
  83. || (score >= 3 && count >= 21)
  84. || (score >= 1 && count >= 60)
  85. ) {
  86. domainArr.push('.' + apexDomain);
  87. }
  88. });
  89. if (isDebug) {
  90. console.log({
  91. v: 1,
  92. score: domainScoreMap['com-ticketry.world'],
  93. count: domainCountMap.get('com-ticketry.world'),
  94. domainArrLen: domainArr.length
  95. });
  96. }
  97. return domainArr;
  98. }
  99. ));
  100. }
  101. function calcDomainAbuseScore(subdomain: string, fullDomain: string = subdomain) {
  102. if (leathalKeywords(fullDomain)) {
  103. return 100;
  104. }
  105. let weight = 0;
  106. const hitLowKeywords = lowKeywords(fullDomain);
  107. const sensitiveKeywordsHit = sensitiveKeywords(fullDomain);
  108. if (sensitiveKeywordsHit) {
  109. weight += 15;
  110. if (hitLowKeywords) {
  111. weight += 10;
  112. }
  113. } else if (hitLowKeywords) {
  114. weight += 2;
  115. }
  116. const subdomainLength = subdomain.length;
  117. if (subdomainLength > 6) {
  118. weight += 0.015;
  119. if (subdomainLength > 13) {
  120. weight += 0.2;
  121. if (subdomainLength > 20) {
  122. weight += 1;
  123. if (subdomainLength > 30) {
  124. weight += 5;
  125. if (subdomainLength > 40) {
  126. weight += 10;
  127. }
  128. }
  129. }
  130. if (subdomain.indexOf('.', 1) > 1) {
  131. weight += 1;
  132. }
  133. }
  134. }
  135. return weight;
  136. }
  137. if (!process.env.JEST_WORKER_ID && require.main === module) {
  138. getPhishingDomains(true).catch(console.error);
  139. }