get-phishing-domains.ts 4.4 KB

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