get-phishing-domains.ts 6.7 KB

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