build-phishing-domainset.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // @ts-check
  2. const { processFilterRules } = require('./lib/parse-filter.js');
  3. const path = require('path');
  4. const { createRuleset } = require('./lib/create-file');
  5. const { processLine } = require('./lib/process-line.js');
  6. const { createDomainSorter } = require('./lib/stable-sort-domain');
  7. const { traceSync, task } = require('./lib/trace-runner.js');
  8. const createTrie = require('./lib/trie.js');
  9. const { getGorhillPublicSuffixPromise } = require('./lib/get-gorhill-publicsuffix.js');
  10. const { createCachedGorhillGetDomain } = require('./lib/cached-tld-parse.js');
  11. const tldts = require('tldts');
  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. const buildPhishingDomainSet = task(__filename, async () => {
  66. const [{ black: domainSet }, gorhill] = await Promise.all([
  67. processFilterRules(
  68. 'https://curbengh.github.io/phishing-filter/phishing-filter-agh.txt',
  69. [
  70. 'https://phishing-filter.pages.dev/phishing-filter-agh.txt'
  71. // Prefer mirror, since malware-filter.gitlab.io has not been updated for a while
  72. // 'https://malware-filter.gitlab.io/malware-filter/phishing-filter-agh.txt'
  73. ]
  74. ),
  75. getGorhillPublicSuffixPromise()
  76. ]);
  77. traceSync('* whitelist', () => {
  78. const trieForRemovingWhiteListed = createTrie(domainSet);
  79. WHITELIST_DOMAIN.forEach(white => {
  80. trieForRemovingWhiteListed.find(`.${white}`, false).forEach(f => domainSet.delete(f));
  81. if (trieForRemovingWhiteListed.has(white)) {
  82. domainSet.delete(white);
  83. }
  84. });
  85. });
  86. const domainCountMap = {};
  87. const getDomain = createCachedGorhillGetDomain(gorhill);
  88. traceSync('* process domain set', () => {
  89. const domainArr = Array.from(domainSet);
  90. for (let i = 0, len = domainArr.length; i < len; i++) {
  91. const line = processLine(domainArr[i]);
  92. if (!line) continue;
  93. const apexDomain = getDomain(line);
  94. if (!apexDomain) continue;
  95. domainCountMap[apexDomain] ||= 0;
  96. const isPhishingDomainMockingCoJp = line.includes('-co-jp');
  97. if (isPhishingDomainMockingCoJp) {
  98. domainCountMap[apexDomain] += 0.5;
  99. }
  100. if (line.startsWith('.amaz')) {
  101. domainCountMap[apexDomain] += 0.5;
  102. if (line.startsWith('.amazon-')) {
  103. domainCountMap[apexDomain] += 4.5;
  104. }
  105. if (isPhishingDomainMockingCoJp) {
  106. domainCountMap[apexDomain] += 4;
  107. }
  108. } else if (line.startsWith('.customer')) {
  109. domainCountMap[apexDomain] += 0.25;
  110. }
  111. const tld = gorhill.getPublicSuffix(line[0] === '.' ? line.slice(1) : line);
  112. if (!tld || !BLACK_TLD.has(tld)) continue;
  113. domainCountMap[apexDomain] += 1;
  114. const lineLen = line.length;
  115. if (lineLen > 19) {
  116. // Add more weight if the domain is long enough
  117. if (lineLen > 44) {
  118. domainCountMap[apexDomain] += 3.5;
  119. } else if (lineLen > 34) {
  120. domainCountMap[apexDomain] += 2.5;
  121. } else if (lineLen > 29) {
  122. domainCountMap[apexDomain] += 1.5;
  123. } else if (lineLen > 24) {
  124. domainCountMap[apexDomain] += 0.75;
  125. } else {
  126. domainCountMap[apexDomain] += 0.25;
  127. }
  128. if (domainCountMap[apexDomain] < 5) {
  129. const subdomain = tldts.getSubdomain(line);
  130. if (subdomain?.includes('.')) {
  131. domainCountMap[apexDomain] += 1.5;
  132. }
  133. }
  134. }
  135. }
  136. });
  137. const domainSorter = createDomainSorter(gorhill);
  138. const results = traceSync('* get final results', () => Object.entries(domainCountMap)
  139. .reduce((acc, [apexDomain, count]) => {
  140. if (count >= 5) {
  141. acc.push(`.${apexDomain}`);
  142. }
  143. return acc;
  144. }, /** @type {string[]} */([]))
  145. .sort(domainSorter));
  146. const description = [
  147. 'License: AGPL 3.0',
  148. 'Homepage: https://ruleset.skk.moe',
  149. 'GitHub: https://github.com/SukkaW/Surge',
  150. '',
  151. 'The domainset supports enhanced phishing protection',
  152. 'Build from:',
  153. ' - https://gitlab.com/malware-filter/phishing-filter'
  154. ];
  155. return Promise.all(createRuleset(
  156. 'Sukka\'s Ruleset - Reject Phishing',
  157. description,
  158. new Date(),
  159. results,
  160. 'domainset',
  161. path.resolve(__dirname, '../List/domainset/reject_phishing.conf'),
  162. path.resolve(__dirname, '../Clash/domainset/reject_phishing.txt')
  163. ));
  164. });
  165. module.exports.buildPhishingDomainSet = buildPhishingDomainSet;
  166. if (require.main === module) {
  167. buildPhishingDomainSet();
  168. }