get-phishing-domains.ts 4.2 KB

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