get-phishing-domains.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 = [
  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((parentSpan) => {
  99. const trieForRemovingWhiteListed = parentSpan.traceChild('create trie for whitelisting').traceSyncFn(() => createTrie(domainSet));
  100. return parentSpan.traceChild('delete whitelisted from domainset').traceSyncFn(() => {
  101. for (let i = 0, len = WHITELIST_DOMAIN.length; i < len; i++) {
  102. const white = WHITELIST_DOMAIN[i];
  103. const found = trieForRemovingWhiteListed.find(`.${white}`, true);
  104. for (let j = 0, len2 = found.length; j < len2; j++) {
  105. domainSet.delete(found[j]);
  106. }
  107. domainSet.delete(white);
  108. }
  109. });
  110. });
  111. const domainCountMap: Record<string, number> = {};
  112. const getDomain = createCachedGorhillGetDomain(gorhill);
  113. span.traceChild('process phishing domain set').traceSyncFn(() => {
  114. const domainArr = Array.from(domainSet);
  115. for (let i = 0, len = domainArr.length; i < len; i++) {
  116. const line = processLine(domainArr[i]);
  117. if (!line) continue;
  118. const apexDomain = getDomain(line);
  119. if (!apexDomain) continue;
  120. domainCountMap[apexDomain] ||= 0;
  121. const isPhishingDomainMockingCoJp = line.includes('-co-jp');
  122. if (isPhishingDomainMockingCoJp) {
  123. domainCountMap[apexDomain] += 0.5;
  124. }
  125. if (line.startsWith('.amaz')) {
  126. domainCountMap[apexDomain] += 0.5;
  127. if (line.startsWith('.amazon-')) {
  128. domainCountMap[apexDomain] += 4.5;
  129. }
  130. if (isPhishingDomainMockingCoJp) {
  131. domainCountMap[apexDomain] += 4;
  132. }
  133. } else if (line.startsWith('.customer')) {
  134. domainCountMap[apexDomain] += 0.25;
  135. }
  136. const tld = gorhill.getPublicSuffix(line[0] === '.' ? line.slice(1) : line);
  137. if (!tld || !BLACK_TLD.has(tld)) continue;
  138. // Only when tld is black will this 1 weight be added
  139. domainCountMap[apexDomain] += 1;
  140. const lineLen = line.length;
  141. if (lineLen > 19) {
  142. // Add more weight if the domain is long enough
  143. if (lineLen > 44) {
  144. domainCountMap[apexDomain] += 3.5;
  145. } else if (lineLen > 34) {
  146. domainCountMap[apexDomain] += 2.5;
  147. } else if (lineLen > 29) {
  148. domainCountMap[apexDomain] += 1.5;
  149. } else if (lineLen > 24) {
  150. domainCountMap[apexDomain] += 0.75;
  151. } else {
  152. domainCountMap[apexDomain] += 0.25;
  153. }
  154. if (domainCountMap[apexDomain] < 5) {
  155. const subdomain = tldts.getSubdomain(line, { detectIp: false });
  156. if (subdomain?.includes('.')) {
  157. domainCountMap[apexDomain] += 1.5;
  158. }
  159. }
  160. }
  161. }
  162. });
  163. const results = span.traceChild('get final phishing results').traceSyncFn(() => {
  164. const results: string[] = [];
  165. for (const domain in domainCountMap) {
  166. if (domainCountMap[domain] > 5) {
  167. results.push(domain);
  168. }
  169. }
  170. return results;
  171. });
  172. return [results, domainSet] as const;
  173. });