build-phishing-domainset.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import { processFilterRules, processHosts } from './lib/parse-filter';
  2. import path from 'path';
  3. import { createRuleset } from './lib/create-file';
  4. import { processLine } from './lib/process-line';
  5. import { createDomainSorter } from './lib/stable-sort-domain';
  6. import { traceSync, task } from './lib/trace-runner';
  7. import createTrie from './lib/trie';
  8. import { getGorhillPublicSuffixPromise } from './lib/get-gorhill-publicsuffix';
  9. import { createCachedGorhillGetDomain } from './lib/cached-tld-parse';
  10. import * as tldts from 'tldts';
  11. import { SHARED_DESCRIPTION } from './lib/constants';
  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. 'xyz',
  24. 'top',
  25. 'win',
  26. 'vip',
  27. 'site',
  28. 'space',
  29. 'online',
  30. 'icu',
  31. 'fun',
  32. 'shop',
  33. 'cool',
  34. 'cyou',
  35. 'id',
  36. 'pro',
  37. 'za.com',
  38. 'sa.com',
  39. 'ltd',
  40. 'group',
  41. 'rest',
  42. 'tech',
  43. 'link',
  44. 'ink',
  45. 'bar',
  46. 'tokyo',
  47. 'tk',
  48. 'cf',
  49. 'gq',
  50. 'ga',
  51. 'ml',
  52. 'cc',
  53. 'cn',
  54. 'codes',
  55. 'cloud',
  56. 'club',
  57. 'click',
  58. 'cfd',
  59. 'fit',
  60. 'mobi',
  61. 'buzz',
  62. 'one',
  63. 'com.cn'
  64. ]);
  65. export const buildPhishingDomainSet = task(import.meta.path, async () => {
  66. const [domainSet, gorhill] = await Promise.all([
  67. processHosts('https://curbengh.github.io/phishing-filter/phishing-filter-hosts.txt', true, true),
  68. // processFilterRules(
  69. // 'https://curbengh.github.io/phishing-filter/phishing-filter-agh.txt',
  70. // [
  71. // 'https://phishing-filter.pages.dev/phishing-filter-agh.txt'
  72. // // Prefer mirror, since malware-filter.gitlab.io has not been updated for a while
  73. // // 'https://malware-filter.gitlab.io/malware-filter/phishing-filter-agh.txt'
  74. // ]
  75. // ),
  76. getGorhillPublicSuffixPromise()
  77. ]);
  78. traceSync('* whitelist', () => {
  79. const trieForRemovingWhiteListed = createTrie(domainSet);
  80. WHITELIST_DOMAIN.forEach(white => {
  81. trieForRemovingWhiteListed.find(`.${white}`, false).forEach(f => domainSet.delete(f));
  82. if (trieForRemovingWhiteListed.has(white)) {
  83. domainSet.delete(white);
  84. }
  85. });
  86. });
  87. const domainCountMap: Record<string, number> = {};
  88. const getDomain = createCachedGorhillGetDomain(gorhill);
  89. traceSync('* process domain set', () => {
  90. const domainArr = Array.from(domainSet);
  91. for (let i = 0, len = domainArr.length; i < len; i++) {
  92. const line = processLine(domainArr[i]);
  93. if (!line) continue;
  94. const apexDomain = getDomain(line);
  95. if (!apexDomain) continue;
  96. domainCountMap[apexDomain] ||= 0;
  97. const isPhishingDomainMockingCoJp = line.includes('-co-jp');
  98. if (isPhishingDomainMockingCoJp) {
  99. domainCountMap[apexDomain] += 0.5;
  100. }
  101. if (line.startsWith('.amaz')) {
  102. domainCountMap[apexDomain] += 0.5;
  103. if (line.startsWith('.amazon-')) {
  104. domainCountMap[apexDomain] += 4.5;
  105. }
  106. if (isPhishingDomainMockingCoJp) {
  107. domainCountMap[apexDomain] += 4;
  108. }
  109. } else if (line.startsWith('.customer')) {
  110. domainCountMap[apexDomain] += 0.25;
  111. }
  112. const tld = gorhill.getPublicSuffix(line[0] === '.' ? line.slice(1) : line);
  113. if (!tld || !BLACK_TLD.has(tld)) continue;
  114. domainCountMap[apexDomain] += 1;
  115. const lineLen = line.length;
  116. if (lineLen > 19) {
  117. // Add more weight if the domain is long enough
  118. if (lineLen > 44) {
  119. domainCountMap[apexDomain] += 3.5;
  120. } else if (lineLen > 34) {
  121. domainCountMap[apexDomain] += 2.5;
  122. } else if (lineLen > 29) {
  123. domainCountMap[apexDomain] += 1.5;
  124. } else if (lineLen > 24) {
  125. domainCountMap[apexDomain] += 0.75;
  126. } else {
  127. domainCountMap[apexDomain] += 0.25;
  128. }
  129. if (domainCountMap[apexDomain] < 5) {
  130. const subdomain = tldts.getSubdomain(line, { detectIp: false });
  131. if (subdomain?.includes('.')) {
  132. domainCountMap[apexDomain] += 1.5;
  133. }
  134. }
  135. }
  136. }
  137. });
  138. const domainSorter = createDomainSorter(gorhill);
  139. const results = traceSync('* get final results', () => Object.entries(domainCountMap)
  140. .reduce<string[]>((acc, [apexDomain, count]) => {
  141. if (count >= 5) {
  142. acc.push(`.${apexDomain}`);
  143. }
  144. return acc;
  145. }, [])
  146. .sort(domainSorter));
  147. const description = [
  148. ...SHARED_DESCRIPTION,
  149. '',
  150. 'The domainset supports enhanced phishing protection',
  151. 'Build from:',
  152. ' - https://gitlab.com/malware-filter/phishing-filter'
  153. ];
  154. return Promise.all(createRuleset(
  155. 'Sukka\'s Ruleset - Reject Phishing',
  156. description,
  157. new Date(),
  158. results,
  159. 'domainset',
  160. path.resolve(import.meta.dir, '../List/domainset/reject_phishing.conf'),
  161. path.resolve(import.meta.dir, '../Clash/domainset/reject_phishing.txt')
  162. ));
  163. });
  164. if (import.meta.main) {
  165. buildPhishingDomainSet();
  166. }