get-phishing-domains.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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> = {};
  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. domainCountMap.set(
  63. apexDomain,
  64. domainCountMap.has(apexDomain)
  65. ? domainCountMap.get(apexDomain)! + 1
  66. : 1
  67. );
  68. if (!(apexDomain in domainScoreMap)) {
  69. domainScoreMap[apexDomain] = 0;
  70. if (BLACK_TLD.has(tld)) {
  71. domainScoreMap[apexDomain] += 3;
  72. } else if (tld.length > 6) {
  73. domainScoreMap[apexDomain] += 2;
  74. }
  75. if (apexDomain.length >= 18) {
  76. domainScoreMap[apexDomain] += 0.5;
  77. }
  78. }
  79. subdomain = parsed.subdomain;
  80. if (
  81. subdomain
  82. && !WHITELIST_MAIN_DOMAINS.has(apexDomain)
  83. ) {
  84. domainScoreMap[apexDomain] += calcDomainAbuseScore(subdomain, line);
  85. }
  86. }
  87. domainCountMap.forEach((count, apexDomain) => {
  88. if (
  89. // !WHITELIST_MAIN_DOMAINS.has(apexDomain)
  90. (domainScoreMap[apexDomain] >= 24)
  91. || (domainScoreMap[apexDomain] >= 16 && count >= 7)
  92. || (domainScoreMap[apexDomain] >= 13 && count >= 11)
  93. || (domainScoreMap[apexDomain] >= 5 && count >= 14)
  94. || (domainScoreMap[apexDomain] >= 3 && count >= 21)
  95. || (domainScoreMap[apexDomain] >= 1 && count >= 60)
  96. ) {
  97. domainArr.push('.' + apexDomain);
  98. }
  99. });
  100. if (isDebug) {
  101. console.log({
  102. v: 1,
  103. score: domainScoreMap['com-ticketry.world'],
  104. count: domainCountMap.get('com-ticketry.world'),
  105. domainArrLen: domainArr.length
  106. });
  107. }
  108. return domainArr;
  109. function calcDomainAbuseScore(subdomain: string, fullDomain: string = subdomain) {
  110. if (leathalKeywords(fullDomain)) {
  111. return 100;
  112. }
  113. let weight = 0;
  114. const hitLowKeywords = lowKeywords(fullDomain);
  115. const sensitiveKeywordsHit = sensitiveKeywords(fullDomain);
  116. if (sensitiveKeywordsHit) {
  117. weight += 15;
  118. if (hitLowKeywords) {
  119. weight += 10;
  120. }
  121. } else if (hitLowKeywords) {
  122. weight += 2;
  123. }
  124. const subdomainLength = subdomain.length;
  125. if (subdomainLength > 6) {
  126. weight += 0.015;
  127. if (subdomainLength > 13) {
  128. weight += 0.2;
  129. if (subdomainLength > 20) {
  130. weight += 1;
  131. if (subdomainLength > 30) {
  132. weight += 5;
  133. if (subdomainLength > 40) {
  134. weight += 10;
  135. }
  136. }
  137. }
  138. if (subdomain.indexOf('.', 1) > 1) {
  139. weight += 1;
  140. }
  141. }
  142. }
  143. return weight;
  144. }
  145. }
  146. }
  147. });
  148. export function getPhishingDomains(parentSpan: Span) {
  149. return parentSpan.traceChild('get phishing domains').traceAsyncFn(async (span) => {
  150. const domainArr = await span.traceChildAsync('download/parse/merge phishing domains', async (curSpan) => {
  151. const domainArr: string[] = [];
  152. const domainGroups = await Promise.all(downloads.map(task => task(curSpan)));
  153. domainGroups.forEach(appendArrayInPlaceCurried(domainArr));
  154. return domainArr;
  155. });
  156. return span.traceChildAsync(
  157. 'process phishing domain set',
  158. async () => {
  159. const phishingDomains = await pool.exec(
  160. 'processPhihsingDomains',
  161. [
  162. domainArr,
  163. import.meta.url,
  164. require.main === module
  165. ]
  166. );
  167. pool.terminate();
  168. return phishingDomains;
  169. }
  170. );
  171. });
  172. }
  173. if (require.main === module) {
  174. getPhishingDomains(dummySpan)
  175. .catch(console.error)
  176. .finally(() => {
  177. dummySpan.stop();
  178. printTraceResult(dummySpan.traceResult);
  179. });
  180. }