get-phishing-domains.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import { getGorhillPublicSuffixPromise } from './get-gorhill-publicsuffix';
  2. import { processDomainLists } from './parse-filter';
  3. import * as tldts from 'tldts';
  4. import { createTrie } from './trie';
  5. import { createCachedGorhillGetDomain } from './cached-tld-parse';
  6. import { processLine } from './process-line';
  7. import { TTL } from './cache-filesystem';
  8. import { isCI } from 'ci-info';
  9. import { add as SetAdd } from 'mnemonist/set';
  10. import type { Span } from '../trace';
  11. const WHITELIST_DOMAIN = [
  12. 'w3s.link',
  13. 'dweb.link',
  14. 'nftstorage.link',
  15. 'square.site',
  16. 'business.site',
  17. 'page.link', // Firebase URL Shortener
  18. 'fleek.cool',
  19. 'notion.site'
  20. ];
  21. const BLACK_TLD = new Set([
  22. 'autos',
  23. 'bar',
  24. 'biz',
  25. 'bond',
  26. 'business',
  27. 'buzz',
  28. 'cc',
  29. 'cf',
  30. 'cfd',
  31. 'click',
  32. 'cloud',
  33. 'club',
  34. 'cn',
  35. 'codes',
  36. 'co.uk',
  37. 'co.in',
  38. 'com.br',
  39. 'com.cn',
  40. 'com.pl',
  41. 'com.vn',
  42. 'cool',
  43. 'cyou',
  44. 'fit',
  45. 'fun',
  46. 'ga',
  47. 'gd',
  48. 'gq',
  49. 'group',
  50. 'host',
  51. 'icu',
  52. 'id',
  53. 'info',
  54. 'ink',
  55. 'life',
  56. 'live',
  57. 'link',
  58. 'ltd',
  59. 'ml',
  60. 'mobi',
  61. 'net.pl',
  62. 'one',
  63. 'online',
  64. 'pro',
  65. 'pl',
  66. 'pw',
  67. 'rest',
  68. 'rf.gd',
  69. 'sa.com',
  70. 'sbs',
  71. 'shop',
  72. 'site',
  73. 'space',
  74. 'store',
  75. 'tech',
  76. 'tk',
  77. 'tokyo',
  78. 'top',
  79. 'vip',
  80. 'vn',
  81. 'website',
  82. 'win',
  83. 'xyz',
  84. 'za.com'
  85. ]);
  86. export const getPhishingDomains = (parentSpan: Span) => parentSpan.traceChild('get phishing domains').traceAsyncFn(async (span) => {
  87. const [domainSet, domainSet2, gorhill] = await Promise.all([
  88. processDomainLists(span, 'https://curbengh.github.io/phishing-filter/phishing-filter-domains.txt', true, TTL.THREE_HOURS()),
  89. isCI
  90. ? processDomainLists(span, 'https://phishing.army/download/phishing_army_blocklist.txt', true, TTL.THREE_HOURS())
  91. : null,
  92. getGorhillPublicSuffixPromise()
  93. ]);
  94. if (domainSet2) {
  95. SetAdd(domainSet, domainSet2);
  96. }
  97. span.traceChild('whitelisting phishing domains').traceSyncFn((parentSpan) => {
  98. const trieForRemovingWhiteListed = parentSpan.traceChild('create trie for whitelisting').traceSyncFn(() => createTrie(domainSet));
  99. return parentSpan.traceChild('delete whitelisted from domainset').traceSyncFn(() => {
  100. for (let i = 0, len = WHITELIST_DOMAIN.length; i < len; i++) {
  101. const white = WHITELIST_DOMAIN[i];
  102. domainSet.delete(white);
  103. domainSet.delete(`.${white}`);
  104. trieForRemovingWhiteListed.substractSetInPlaceFromFound(`.${white}`, domainSet);
  105. }
  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. // Only when tld is black will this 1 weight be added
  136. domainCountMap[apexDomain] += 1;
  137. const lineLen = line.length;
  138. if (lineLen > 19) {
  139. // Add more weight if the domain is long enough
  140. if (lineLen > 44) {
  141. domainCountMap[apexDomain] += 3.5;
  142. } else if (lineLen > 34) {
  143. domainCountMap[apexDomain] += 2.5;
  144. } else if (lineLen > 29) {
  145. domainCountMap[apexDomain] += 1.5;
  146. } else if (lineLen > 24) {
  147. domainCountMap[apexDomain] += 0.75;
  148. } else {
  149. domainCountMap[apexDomain] += 0.25;
  150. }
  151. if (domainCountMap[apexDomain] < 5) {
  152. const subdomain = tldts.getSubdomain(line, { detectIp: false });
  153. if (subdomain?.includes('.')) {
  154. domainCountMap[apexDomain] += 1.5;
  155. }
  156. }
  157. }
  158. }
  159. });
  160. const results = span.traceChild('get final phishing results').traceSyncFn(() => {
  161. const results: string[] = [];
  162. for (const domain in domainCountMap) {
  163. if (domainCountMap[domain] >= 5) {
  164. results.push(`.${domain}`);
  165. }
  166. }
  167. return results;
  168. });
  169. return [results, domainSet] as const;
  170. });