build-phishing-domainset.ts 4.8 KB

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