get-phishing-domains.ts 4.1 KB

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