get-phishing-domains.ts 6.8 KB

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