get-phishing-domains.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import fsp from 'fs/promises';
  2. import path from 'path';
  3. import { getGorhillPublicSuffixPromise } from './get-gorhill-publicsuffix';
  4. import { 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. 'com.cn',
  36. 'cool',
  37. 'cyou',
  38. 'fit',
  39. 'fun',
  40. 'ga',
  41. 'gd',
  42. 'gq',
  43. 'group',
  44. 'host',
  45. 'icu',
  46. 'id',
  47. 'info',
  48. 'ink',
  49. 'life',
  50. 'live',
  51. 'link',
  52. 'ltd',
  53. 'ml',
  54. 'mobi',
  55. 'one',
  56. 'online',
  57. 'pro',
  58. 'pl',
  59. 'pw',
  60. 'rest',
  61. 'rf.gd',
  62. 'sa.com',
  63. 'sbs',
  64. 'shop',
  65. 'site',
  66. 'space',
  67. 'store',
  68. 'tech',
  69. 'tk',
  70. 'tokyo',
  71. 'top',
  72. 'vip',
  73. 'vn',
  74. 'website',
  75. 'win',
  76. 'xyz',
  77. 'za.com'
  78. ]);
  79. export const getPhishingDomains = () => traceAsync('get phishing domains', async () => {
  80. const [domainSet, gorhill] = await Promise.all([
  81. processHosts('https://curbengh.github.io/phishing-filter/phishing-filter-hosts.txt', true, true),
  82. // processDomainLists('https://phishing.army/download/phishing_army_blocklist.txt', true),
  83. // processFilterRules(
  84. // 'https://curbengh.github.io/phishing-filter/phishing-filter-agh.txt',
  85. // [
  86. // 'https://phishing-filter.pages.dev/phishing-filter-agh.txt'
  87. // // Prefer mirror, since malware-filter.gitlab.io has not been updated for a while
  88. // // 'https://malware-filter.gitlab.io/malware-filter/phishing-filter-agh.txt'
  89. // ]
  90. // ),
  91. getGorhillPublicSuffixPromise(),
  92. // Remove old files
  93. fsp.rm(path.resolve(import.meta.dir, '../../List/domainset/reject_phishing.conf'), { force: true }),
  94. fsp.rm(path.resolve(import.meta.dir, '../../Clash/domainset/reject_phishing.txt'), { force: true })
  95. ]);
  96. traceSync.skip('* whitelisting phishing domains', () => {
  97. const trieForRemovingWhiteListed = createTrie(domainSet);
  98. WHITELIST_DOMAIN.forEach(white => {
  99. trieForRemovingWhiteListed.find(`.${white}`, false).forEach(f => domainSet.delete(f));
  100. // if (trieForRemovingWhiteListed.has(white)) {
  101. domainSet.delete(white);
  102. // }
  103. });
  104. });
  105. const domainCountMap: Record<string, number> = {};
  106. const getDomain = createCachedGorhillGetDomain(gorhill);
  107. traceSync.skip('* process phishing domain set', () => {
  108. const domainArr = Array.from(domainSet);
  109. for (let i = 0, len = domainArr.length; i < len; i++) {
  110. const line = processLine(domainArr[i]);
  111. if (!line) continue;
  112. const apexDomain = getDomain(line);
  113. if (!apexDomain) continue;
  114. domainCountMap[apexDomain] ||= 0;
  115. const isPhishingDomainMockingCoJp = line.includes('-co-jp');
  116. if (isPhishingDomainMockingCoJp) {
  117. domainCountMap[apexDomain] += 0.5;
  118. }
  119. if (line.startsWith('.amaz')) {
  120. domainCountMap[apexDomain] += 0.5;
  121. if (line.startsWith('.amazon-')) {
  122. domainCountMap[apexDomain] += 4.5;
  123. }
  124. if (isPhishingDomainMockingCoJp) {
  125. domainCountMap[apexDomain] += 4;
  126. }
  127. } else if (line.startsWith('.customer')) {
  128. domainCountMap[apexDomain] += 0.25;
  129. }
  130. const tld = gorhill.getPublicSuffix(line[0] === '.' ? line.slice(1) : line);
  131. if (!tld || !BLACK_TLD.has(tld)) continue;
  132. domainCountMap[apexDomain] += 1;
  133. const lineLen = line.length;
  134. if (lineLen > 19) {
  135. // Add more weight if the domain is long enough
  136. if (lineLen > 44) {
  137. domainCountMap[apexDomain] += 3.5;
  138. } else if (lineLen > 34) {
  139. domainCountMap[apexDomain] += 2.5;
  140. } else if (lineLen > 29) {
  141. domainCountMap[apexDomain] += 1.5;
  142. } else if (lineLen > 24) {
  143. domainCountMap[apexDomain] += 0.75;
  144. } else {
  145. domainCountMap[apexDomain] += 0.25;
  146. }
  147. if (domainCountMap[apexDomain] < 5) {
  148. const subdomain = tldts.getSubdomain(line, { detectIp: false });
  149. if (subdomain?.includes('.')) {
  150. domainCountMap[apexDomain] += 1.5;
  151. }
  152. }
  153. }
  154. }
  155. });
  156. const results = traceSync.skip('* get final phishing results', () => Object.entries(domainCountMap)
  157. .filter(([, count]) => count >= 5)
  158. .map(([apexDomain]) => apexDomain));
  159. return [results, domainSet] as const;
  160. });