get-phishing-domains.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import fsp from 'fs/promises';
  2. import path from 'path';
  3. import { getGorhillPublicSuffixPromise } from './get-gorhill-publicsuffix';
  4. import { processDomainLists, processHosts } from './parse-filter';
  5. import { traceAsync, traceSync } from './trace-runner';
  6. import * as tldts from 'tldts';
  7. import { createTrie } from './trie';
  8. import { createCachedGorhillGetDomain } from './cached-tld-parse';
  9. import { processLine } from './process-line';
  10. const WHITELIST_DOMAIN = new Set([
  11. 'w3s.link',
  12. 'dweb.link',
  13. 'nftstorage.link',
  14. 'square.site',
  15. 'business.site',
  16. 'page.link', // Firebase URL Shortener
  17. 'fleek.cool',
  18. 'notion.site'
  19. ]);
  20. const BLACK_TLD = new Set([
  21. 'autos',
  22. 'bar',
  23. 'biz',
  24. 'bond',
  25. 'business',
  26. 'buzz',
  27. 'cc',
  28. 'cf',
  29. 'cfd',
  30. 'click',
  31. 'cloud',
  32. 'club',
  33. 'cn',
  34. 'codes',
  35. 'co.uk',
  36. 'co.in',
  37. 'com.br',
  38. 'com.cn',
  39. 'com.pl',
  40. 'com.vn',
  41. 'cool',
  42. 'cyou',
  43. 'fit',
  44. 'fun',
  45. 'ga',
  46. 'gd',
  47. 'gq',
  48. 'group',
  49. 'host',
  50. 'icu',
  51. 'id',
  52. 'info',
  53. 'ink',
  54. 'life',
  55. 'live',
  56. 'link',
  57. 'ltd',
  58. 'ml',
  59. 'mobi',
  60. 'net.pl',
  61. 'one',
  62. 'online',
  63. 'pro',
  64. 'pl',
  65. 'pw',
  66. 'rest',
  67. 'rf.gd',
  68. 'sa.com',
  69. 'sbs',
  70. 'shop',
  71. 'site',
  72. 'space',
  73. 'store',
  74. 'tech',
  75. 'tk',
  76. 'tokyo',
  77. 'top',
  78. 'vip',
  79. 'vn',
  80. 'website',
  81. 'win',
  82. 'xyz',
  83. 'za.com'
  84. ]);
  85. export const getPhishingDomains = () => traceAsync('get phishing domains', async () => {
  86. const [domainSet, domainSet2, gorhill] = await Promise.all([
  87. processHosts('https://curbengh.github.io/phishing-filter/phishing-filter-hosts.txt', true, true),
  88. processDomainLists('https://phishing.army/download/phishing_army_blocklist.txt', true),
  89. getGorhillPublicSuffixPromise()
  90. ]);
  91. domainSet2.forEach((domain) => domainSet.add(domain));
  92. traceSync.skip('* whitelisting phishing domains', () => {
  93. const trieForRemovingWhiteListed = createTrie(domainSet);
  94. WHITELIST_DOMAIN.forEach(white => {
  95. trieForRemovingWhiteListed.find(`.${white}`, false).forEach(f => domainSet.delete(f));
  96. // if (trieForRemovingWhiteListed.has(white)) {
  97. domainSet.delete(white);
  98. // }
  99. });
  100. });
  101. const domainCountMap: Record<string, number> = {};
  102. const getDomain = createCachedGorhillGetDomain(gorhill);
  103. traceSync.skip('* process phishing domain set', () => {
  104. const domainArr = Array.from(domainSet);
  105. for (let i = 0, len = domainArr.length; i < len; i++) {
  106. const line = processLine(domainArr[i]);
  107. if (!line) continue;
  108. const apexDomain = getDomain(line);
  109. if (!apexDomain) continue;
  110. domainCountMap[apexDomain] ||= 0;
  111. const isPhishingDomainMockingCoJp = line.includes('-co-jp');
  112. if (isPhishingDomainMockingCoJp) {
  113. domainCountMap[apexDomain] += 0.5;
  114. }
  115. if (line.startsWith('.amaz')) {
  116. domainCountMap[apexDomain] += 0.5;
  117. if (line.startsWith('.amazon-')) {
  118. domainCountMap[apexDomain] += 4.5;
  119. }
  120. if (isPhishingDomainMockingCoJp) {
  121. domainCountMap[apexDomain] += 4;
  122. }
  123. } else if (line.startsWith('.customer')) {
  124. domainCountMap[apexDomain] += 0.25;
  125. }
  126. const tld = gorhill.getPublicSuffix(line[0] === '.' ? line.slice(1) : line);
  127. if (!tld || !BLACK_TLD.has(tld)) continue;
  128. domainCountMap[apexDomain] += 1;
  129. const lineLen = line.length;
  130. if (lineLen > 19) {
  131. // Add more weight if the domain is long enough
  132. if (lineLen > 44) {
  133. domainCountMap[apexDomain] += 3.5;
  134. } else if (lineLen > 34) {
  135. domainCountMap[apexDomain] += 2.5;
  136. } else if (lineLen > 29) {
  137. domainCountMap[apexDomain] += 1.5;
  138. } else if (lineLen > 24) {
  139. domainCountMap[apexDomain] += 0.75;
  140. } else {
  141. domainCountMap[apexDomain] += 0.25;
  142. }
  143. if (domainCountMap[apexDomain] < 5) {
  144. const subdomain = tldts.getSubdomain(line, { detectIp: false });
  145. if (subdomain?.includes('.')) {
  146. domainCountMap[apexDomain] += 1.5;
  147. }
  148. }
  149. }
  150. }
  151. });
  152. const results = traceSync.skip('* get final phishing results', () => Object.entries(domainCountMap)
  153. .filter(([, count]) => count >= 5)
  154. .map(([apexDomain]) => apexDomain));
  155. return [results, domainSet] as const;
  156. });