get-phishing-domains.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import Worktank from 'worktank';
  2. import { dummySpan, printTraceResult } from '../trace';
  3. import type { Span } from '../trace';
  4. import type { TldTsParsed } from './normalize-domain';
  5. const pool = new Worktank({
  6. pool: {
  7. name: 'process-phishing-domains',
  8. size: 1
  9. },
  10. worker: {
  11. autoAbort: 20000, // The maximum number of milliseconds to wait for the result from the worker, if exceeded the worker is terminated and the execution promise rejects
  12. autoInstantiate: true,
  13. autoTerminate: 30000, // The interval of milliseconds at which to check if the pool can be automatically terminated, to free up resources, workers will be spawned up again if needed
  14. env: {},
  15. methods: {
  16. // eslint-disable-next-line object-shorthand -- workertank
  17. getPhishingDomains: async function (
  18. importMetaUrl: string,
  19. /** require.main === module */ isDebug = false
  20. ): Promise<string[]> {
  21. // TODO: createRequire is a temporary workaround for https://github.com/nodejs/node/issues/51956
  22. const { default: module } = await import('node:module');
  23. const __require = module.createRequire(importMetaUrl);
  24. const picocolors = __require('picocolors') as typeof import('picocolors');
  25. const tldts = __require('tldts-experimental') as typeof import('tldts-experimental');
  26. const { appendArrayInPlaceCurried } = __require('foxts/append-array-in-place') as typeof import('foxts/append-array-in-place');
  27. const { loosTldOptWithPrivateDomains } = __require('../constants/loose-tldts-opt') as typeof import('../constants/loose-tldts-opt');
  28. const { BLACK_TLD, WHITELIST_MAIN_DOMAINS, leathalKeywords, lowKeywords, sensitiveKeywords } = __require('../constants/phishing-score-source') as typeof import('../constants/phishing-score-source');
  29. const { PHISHING_DOMAIN_LISTS_EXTRA, PHISHING_HOSTS_EXTRA } = __require('../constants/reject-data-source') as typeof import('../constants/reject-data-source');
  30. const { dummySpan } = __require('../trace') as typeof import('../trace');
  31. const NullPrototypeObject = __require('null-prototype-object') as typeof import('null-prototype-object');
  32. const { processHostsWithPreload } = __require('./parse-filter/hosts') as typeof import('./parse-filter/hosts');
  33. const { processDomainListsWithPreload } = __require('./parse-filter/domainlists') as typeof import('./parse-filter/domainlists');
  34. const downloads = [
  35. ...PHISHING_DOMAIN_LISTS_EXTRA.map(entry => processDomainListsWithPreload(...entry)),
  36. ...PHISHING_HOSTS_EXTRA.map(entry => processHostsWithPreload(...entry))
  37. ];
  38. const domainArr: string[] = [];
  39. const domainGroups = await Promise.all(downloads.map(task => task(dummySpan)));
  40. domainGroups.forEach(appendArrayInPlaceCurried(domainArr));
  41. // return domainArr;
  42. const domainCountMap = new Map<string, number>();
  43. const domainScoreMap: Record<string, number> = new NullPrototypeObject();
  44. let line = '';
  45. let tld: string | null = '';
  46. let apexDomain: string | null = '';
  47. let subdomain: string | null = '';
  48. let parsed: TldTsParsed;
  49. // const set = new Set<string>();
  50. // let duplicateCount = 0;
  51. for (let i = 0, len = domainArr.length; i < len; i++) {
  52. line = domainArr[i];
  53. // if (set.has(line)) {
  54. // duplicateCount++;
  55. // } else {
  56. // set.add(line);
  57. // }
  58. parsed = tldts.parse(line, loosTldOptWithPrivateDomains);
  59. if (parsed.isPrivate) {
  60. continue;
  61. }
  62. tld = parsed.publicSuffix;
  63. apexDomain = parsed.domain;
  64. if (!tld) {
  65. console.log(picocolors.yellow('[phishing domains] E0001'), 'missing tld', { line, tld });
  66. continue;
  67. }
  68. if (!apexDomain) {
  69. console.log(picocolors.yellow('[phishing domains] E0002'), 'missing domain', { line, apexDomain });
  70. continue;
  71. }
  72. if (WHITELIST_MAIN_DOMAINS.has(apexDomain)) {
  73. continue;
  74. }
  75. domainCountMap.set(
  76. apexDomain,
  77. domainCountMap.has(apexDomain)
  78. ? domainCountMap.get(apexDomain)! + 1
  79. : 1
  80. );
  81. let score = 0;
  82. if (apexDomain in domainScoreMap) {
  83. score = domainScoreMap[apexDomain];
  84. } else {
  85. if (BLACK_TLD.has(tld)) {
  86. score += 3;
  87. } else if (tld.length > 4) {
  88. score += 2;
  89. } else if (tld.length > 5) {
  90. score += 4;
  91. }
  92. if (apexDomain.length >= 18) {
  93. score += 0.5;
  94. }
  95. }
  96. subdomain = parsed.subdomain;
  97. if (subdomain) {
  98. score += calcDomainAbuseScore(subdomain, line);
  99. }
  100. domainScoreMap[apexDomain] = score;
  101. }
  102. domainCountMap.forEach((count, apexDomain) => {
  103. const score = domainScoreMap[apexDomain];
  104. if (
  105. // !WHITELIST_MAIN_DOMAINS.has(apexDomain)
  106. (score >= 24)
  107. || (score >= 16 && count >= 7)
  108. || (score >= 13 && count >= 11)
  109. || (score >= 5 && count >= 14)
  110. || (score >= 3 && count >= 21)
  111. || (score >= 1 && count >= 60)
  112. ) {
  113. domainArr.push('.' + apexDomain);
  114. }
  115. });
  116. if (isDebug) {
  117. console.log({
  118. v: 1,
  119. score: domainScoreMap['com-ticketry.world'],
  120. count: domainCountMap.get('com-ticketry.world'),
  121. domainArrLen: domainArr.length
  122. });
  123. }
  124. return domainArr;
  125. function calcDomainAbuseScore(subdomain: string, fullDomain: string = subdomain) {
  126. if (leathalKeywords(fullDomain)) {
  127. return 100;
  128. }
  129. let weight = 0;
  130. const hitLowKeywords = lowKeywords(fullDomain);
  131. const sensitiveKeywordsHit = sensitiveKeywords(fullDomain);
  132. if (sensitiveKeywordsHit) {
  133. weight += 15;
  134. if (hitLowKeywords) {
  135. weight += 10;
  136. }
  137. } else if (hitLowKeywords) {
  138. weight += 2;
  139. }
  140. const subdomainLength = subdomain.length;
  141. if (subdomainLength > 6) {
  142. weight += 0.015;
  143. if (subdomainLength > 13) {
  144. weight += 0.2;
  145. if (subdomainLength > 20) {
  146. weight += 1;
  147. if (subdomainLength > 30) {
  148. weight += 5;
  149. if (subdomainLength > 40) {
  150. weight += 10;
  151. }
  152. }
  153. }
  154. if (subdomain.indexOf('.', 1) > 1) {
  155. weight += 1;
  156. }
  157. }
  158. }
  159. return weight;
  160. }
  161. }
  162. }
  163. }
  164. });
  165. export function getPhishingDomains(parentSpan: Span) {
  166. return parentSpan.traceChild('get phishing domains').traceAsyncFn(async (span) => span.traceChildAsync(
  167. 'process phishing domain set',
  168. () => pool.exec(
  169. 'getPhishingDomains',
  170. [__filename, require.main === module]
  171. ).finally(() => pool.terminate())
  172. ));
  173. }
  174. if (require.main === module) {
  175. getPhishingDomains(dummySpan)
  176. .catch(console.error)
  177. .finally(() => {
  178. dummySpan.stop();
  179. printTraceResult(dummySpan.traceResult);
  180. });
  181. }