get-phishing-domains.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 > 6) {
  88. score += 2;
  89. }
  90. if (apexDomain.length >= 18) {
  91. score += 0.5;
  92. }
  93. }
  94. subdomain = parsed.subdomain;
  95. if (subdomain) {
  96. score += calcDomainAbuseScore(subdomain, line);
  97. }
  98. domainScoreMap[apexDomain] = score;
  99. }
  100. domainCountMap.forEach((count, apexDomain) => {
  101. const score = domainScoreMap[apexDomain];
  102. if (
  103. // !WHITELIST_MAIN_DOMAINS.has(apexDomain)
  104. (score >= 24)
  105. || (score >= 16 && count >= 7)
  106. || (score >= 13 && count >= 11)
  107. || (score >= 5 && count >= 14)
  108. || (score >= 3 && count >= 21)
  109. || (score >= 1 && count >= 60)
  110. ) {
  111. domainArr.push('.' + apexDomain);
  112. }
  113. });
  114. if (isDebug) {
  115. console.log({
  116. v: 1,
  117. score: domainScoreMap['com-ticketry.world'],
  118. count: domainCountMap.get('com-ticketry.world'),
  119. domainArrLen: domainArr.length
  120. });
  121. }
  122. return domainArr;
  123. function calcDomainAbuseScore(subdomain: string, fullDomain: string = subdomain) {
  124. if (leathalKeywords(fullDomain)) {
  125. return 100;
  126. }
  127. let weight = 0;
  128. const hitLowKeywords = lowKeywords(fullDomain);
  129. const sensitiveKeywordsHit = sensitiveKeywords(fullDomain);
  130. if (sensitiveKeywordsHit) {
  131. weight += 15;
  132. if (hitLowKeywords) {
  133. weight += 10;
  134. }
  135. } else if (hitLowKeywords) {
  136. weight += 2;
  137. }
  138. const subdomainLength = subdomain.length;
  139. if (subdomainLength > 6) {
  140. weight += 0.015;
  141. if (subdomainLength > 13) {
  142. weight += 0.2;
  143. if (subdomainLength > 20) {
  144. weight += 1;
  145. if (subdomainLength > 30) {
  146. weight += 5;
  147. if (subdomainLength > 40) {
  148. weight += 10;
  149. }
  150. }
  151. }
  152. if (subdomain.indexOf('.', 1) > 1) {
  153. weight += 1;
  154. }
  155. }
  156. }
  157. return weight;
  158. }
  159. }
  160. }
  161. }
  162. });
  163. export function getPhishingDomains(parentSpan: Span) {
  164. return parentSpan.traceChild('get phishing domains').traceAsyncFn(async (span) => span.traceChildAsync(
  165. 'process phishing domain set',
  166. async () => {
  167. const phishingDomains = await pool.exec(
  168. 'getPhishingDomains',
  169. [
  170. __filename,
  171. require.main === module
  172. ]
  173. );
  174. pool.terminate();
  175. return phishingDomains;
  176. }
  177. ));
  178. }
  179. if (require.main === module) {
  180. getPhishingDomains(dummySpan)
  181. .catch(console.error)
  182. .finally(() => {
  183. dummySpan.stop();
  184. printTraceResult(dummySpan.traceResult);
  185. });
  186. }