get-phishing-domains.ts 4.7 KB

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