get-phishing-domains.ts 6.9 KB

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