build-phishing-domainset.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. const tldts = require('tldts');
  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 domainSorter = require('./lib/stable-sort-domain');
  7. const { traceSync, task } = require('./lib/trace-runner.js');
  8. const WHITELIST_DOMAIN = new Set([
  9. 'w3s.link',
  10. 'dweb.link',
  11. 'nftstorage.link',
  12. 'square.site',
  13. 'business.site',
  14. 'page.link', // Firebase URL Shortener
  15. 'fleek.cool',
  16. 'notion.site'
  17. ]);
  18. const BLACK_TLD = new Set([
  19. 'xyz',
  20. 'top',
  21. 'win',
  22. 'vip',
  23. 'site',
  24. 'space',
  25. 'online',
  26. 'icu',
  27. 'fun',
  28. 'shop',
  29. 'cool',
  30. 'cyou',
  31. 'id',
  32. 'pro',
  33. 'za.com',
  34. 'sa.com',
  35. 'ltd',
  36. 'group',
  37. 'rest',
  38. 'tech',
  39. 'link',
  40. 'ink',
  41. 'bar',
  42. 'tokyo',
  43. 'tk',
  44. 'cf',
  45. 'gq',
  46. 'ga',
  47. 'ml',
  48. 'cc',
  49. 'cn',
  50. 'codes',
  51. 'cloud',
  52. 'club',
  53. 'click',
  54. 'cfd',
  55. 'fit',
  56. 'mobi',
  57. 'buzz',
  58. 'one',
  59. 'com.cn'
  60. ]);
  61. const buildPhishingDomainSet = task(__filename, async () => {
  62. const domainSet = Array.from((await processFilterRules(
  63. 'https://phishing-filter.pages.dev/phishing-filter-agh.txt'
  64. // [
  65. // 'https://malware-filter.gitlab.io/phishing-filter/phishing-filter-agh.txt',
  66. // 'https://malware-filter.pages.dev/phishing-filter-agh.txt',
  67. // 'https://phishing-filter.pages.dev/phishing-filter-agh.txt'
  68. // ]
  69. )).black);
  70. const domainCountMap = {};
  71. traceSync('* process domain set', () => {
  72. for (let i = 0, len = domainSet.length; i < len; i++) {
  73. const line = processLine(domainSet[i]);
  74. if (!line) continue;
  75. const parsed = tldts.parse(line, { allowPrivateDomains: true });
  76. const apexDomain = parsed.domain;
  77. if (apexDomain) {
  78. if (WHITELIST_DOMAIN.has(apexDomain)) {
  79. continue;
  80. }
  81. domainCountMap[apexDomain] ||= 0;
  82. let isPhishingDomainMockingAmazon = false;
  83. if (line.startsWith('.amaz')) {
  84. domainCountMap[apexDomain] += 0.5;
  85. isPhishingDomainMockingAmazon = true;
  86. if (line.startsWith('.amazon-')) {
  87. domainCountMap[apexDomain] += 4.5;
  88. }
  89. } else if (line.startsWith('.customer')) {
  90. domainCountMap[apexDomain] += 0.25;
  91. }
  92. if (line.includes('-co-jp')) {
  93. domainCountMap[apexDomain] += (isPhishingDomainMockingAmazon ? 4.5 : 0.5);
  94. }
  95. const tld = parsed.publicSuffix;
  96. if (!tld || !BLACK_TLD.has(tld)) continue;
  97. domainCountMap[apexDomain] += 1;
  98. if (line.length > 19) {
  99. // Add more weight if the domain is long enough
  100. if (line.length > 44) {
  101. domainCountMap[apexDomain] += 3.5;
  102. } else if (line.length > 34) {
  103. domainCountMap[apexDomain] += 2.5;
  104. } else if (line.length > 29) {
  105. domainCountMap[apexDomain] += 1.5;
  106. } else if (line.length > 24) {
  107. domainCountMap[apexDomain] += 0.75;
  108. } else if (line.length > 19) {
  109. domainCountMap[apexDomain] += 0.25;
  110. }
  111. if (domainCountMap[apexDomain] < 5) {
  112. const subdomain = parsed.subdomain;
  113. if (subdomain?.includes('.')) {
  114. domainCountMap[apexDomain] += 1.5;
  115. }
  116. }
  117. }
  118. }
  119. }
  120. });
  121. const results = traceSync('* get final results', () => Object.entries(domainCountMap)
  122. .reduce((acc, [apexDomain, count]) => {
  123. if (count >= 5) {
  124. acc.push(`.${apexDomain}`);
  125. }
  126. return acc;
  127. }, /** @type {string[]} */([]))
  128. .sort(domainSorter));
  129. const description = [
  130. 'License: AGPL 3.0',
  131. 'Homepage: https://ruleset.skk.moe',
  132. 'GitHub: https://github.com/SukkaW/Surge',
  133. '',
  134. 'The domainset supports enhanced phishing protection',
  135. 'Build from:',
  136. ' - https://gitlab.com/malware-filter/phishing-filter'
  137. ];
  138. await Promise.all(createRuleset(
  139. 'Sukka\'s Ruleset - Reject Phishing',
  140. description,
  141. new Date(),
  142. results,
  143. 'domainset',
  144. path.resolve(__dirname, '../List/domainset/reject_phishing.conf'),
  145. path.resolve(__dirname, '../Clash/domainset/reject_phishing.txt')
  146. ));
  147. });
  148. module.exports.buildPhishingDomainSet = buildPhishingDomainSet;
  149. if (require.main === module) {
  150. buildPhishingDomainSet();
  151. }