get-phishing-domains.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 { processLine } from './process-line';
  6. import { TTL } from './cache-filesystem';
  7. import { isCI } from 'ci-info';
  8. import { add as SetAdd } from 'mnemonist/set';
  9. import type { Span } from '../trace';
  10. const WHITELIST_DOMAIN = [
  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. 'accountant',
  22. 'autos',
  23. 'bar',
  24. 'bid',
  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. 'cricket',
  45. 'cyou',
  46. 'date',
  47. 'download',
  48. 'faith',
  49. 'fit',
  50. 'fun',
  51. 'ga',
  52. 'gd',
  53. 'gq',
  54. 'group',
  55. 'host',
  56. 'icu',
  57. 'id',
  58. 'info',
  59. 'ink',
  60. 'life',
  61. 'live',
  62. 'link',
  63. 'loan',
  64. 'ltd',
  65. 'men',
  66. 'ml',
  67. 'mobi',
  68. 'net.pl',
  69. 'one',
  70. 'online',
  71. 'party',
  72. 'pro',
  73. 'pl',
  74. 'pw',
  75. 'racing',
  76. 'rest',
  77. 'review',
  78. 'rf.gd',
  79. 'sa.com',
  80. 'sbs',
  81. 'science',
  82. 'shop',
  83. 'site',
  84. 'space',
  85. 'store',
  86. 'stream',
  87. 'tech',
  88. 'tk',
  89. 'tokyo',
  90. 'top',
  91. 'trade',
  92. 'vip',
  93. 'vn',
  94. 'webcam',
  95. 'website',
  96. 'win',
  97. 'xyz',
  98. 'za.com',
  99. 'lat',
  100. 'design'
  101. ]);
  102. export const getPhishingDomains = (parentSpan: Span) => parentSpan.traceChild('get phishing domains').traceAsyncFn(async (span) => {
  103. const [domainSet, domainSet2, gorhill] = await Promise.all([
  104. processDomainLists(span, 'https://curbengh.github.io/phishing-filter/phishing-filter-domains.txt', true, TTL.THREE_HOURS()),
  105. isCI
  106. ? processDomainLists(span, 'https://phishing.army/download/phishing_army_blocklist.txt', true, TTL.THREE_HOURS())
  107. : null,
  108. getGorhillPublicSuffixPromise()
  109. ]);
  110. if (domainSet2) {
  111. SetAdd(domainSet, domainSet2);
  112. }
  113. span.traceChildSync('whitelisting phishing domains', (parentSpan) => {
  114. const trieForRemovingWhiteListed = parentSpan.traceChildSync('create trie for whitelisting', () => createTrie(domainSet));
  115. return parentSpan.traceChild('delete whitelisted from domainset').traceSyncFn(() => {
  116. for (let i = 0, len = WHITELIST_DOMAIN.length; i < len; i++) {
  117. const white = WHITELIST_DOMAIN[i];
  118. domainSet.delete(white);
  119. domainSet.delete(`.${white}`);
  120. trieForRemovingWhiteListed.substractSetInPlaceFromFound(`.${white}`, domainSet);
  121. }
  122. });
  123. });
  124. const domainCountMap: Record<string, number> = {};
  125. span.traceChildSync('process phishing domain set', () => {
  126. const domainArr = Array.from(domainSet);
  127. for (let i = 0, len = domainArr.length; i < len; i++) {
  128. const line = processLine(domainArr[i]);
  129. if (!line) continue;
  130. const apexDomain = gorhill.getDomain(line);
  131. if (!apexDomain) continue;
  132. domainCountMap[apexDomain] ||= 0;
  133. const isPhishingDomainMockingCoJp = line.includes('-co-jp');
  134. if (isPhishingDomainMockingCoJp) {
  135. domainCountMap[apexDomain] += 0.5;
  136. }
  137. if (line.startsWith('.amaz')) {
  138. domainCountMap[apexDomain] += 0.5;
  139. if (line.startsWith('.amazon-')) {
  140. domainCountMap[apexDomain] += 4.5;
  141. }
  142. if (isPhishingDomainMockingCoJp) {
  143. domainCountMap[apexDomain] += 4;
  144. }
  145. } else if (line.startsWith('.customer')) {
  146. domainCountMap[apexDomain] += 0.25;
  147. }
  148. const tld = gorhill.getPublicSuffix(line[0] === '.' ? line.slice(1) : line);
  149. if (!tld || !BLACK_TLD.has(tld)) continue;
  150. // Only when tld is black will this 1 weight be added
  151. domainCountMap[apexDomain] += 1;
  152. const lineLen = line.length;
  153. if (lineLen > 19) {
  154. // Add more weight if the domain is long enough
  155. if (lineLen > 44) {
  156. domainCountMap[apexDomain] += 3.5;
  157. } else if (lineLen > 34) {
  158. domainCountMap[apexDomain] += 2.5;
  159. } else if (lineLen > 29) {
  160. domainCountMap[apexDomain] += 1.5;
  161. } else if (lineLen > 24) {
  162. domainCountMap[apexDomain] += 0.75;
  163. } else {
  164. domainCountMap[apexDomain] += 0.25;
  165. }
  166. if (domainCountMap[apexDomain] < 5) {
  167. const subdomain = tldts.getSubdomain(line, { detectIp: false });
  168. if (subdomain?.includes('.')) {
  169. domainCountMap[apexDomain] += 1.5;
  170. }
  171. }
  172. }
  173. }
  174. });
  175. const results = span.traceChildSync('get final phishing results', () => {
  176. const res: string[] = [];
  177. for (const domain in domainCountMap) {
  178. if (domainCountMap[domain] >= 5) {
  179. res.push(`.${domain}`);
  180. }
  181. }
  182. return res;
  183. });
  184. return [results, domainSet] as const;
  185. });