get-phishing-domains.ts 4.7 KB

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