build-phishing-domainset.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 { runner, traceSync } = 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 = async () => {
  62. const domainSet = Array.from((await processFilterRules('https://curbengh.github.io/phishing-filter/phishing-filter-agh.txt')).black);
  63. const domainCountMap = {};
  64. for (let i = 0, len = domainSet.length; i < len; i++) {
  65. const line = processLine(domainSet[i]);
  66. if (!line) continue;
  67. const parsed = tldts.parse(line, { allowPrivateDomains: true });
  68. const apexDomain = parsed.domain;
  69. if (apexDomain) {
  70. if (WHITELIST_DOMAIN.has(apexDomain)) {
  71. continue;
  72. }
  73. domainCountMap[apexDomain] ||= 0;
  74. let isPhishingDomainMockingAmazon = false;
  75. if (line.startsWith('.amaz')) {
  76. domainCountMap[apexDomain] += 0.5;
  77. isPhishingDomainMockingAmazon = true;
  78. if (line.startsWith('.amazon-')) {
  79. domainCountMap[apexDomain] += 4.5;
  80. }
  81. } else if (line.startsWith('.customer')) {
  82. domainCountMap[apexDomain] += 0.25;
  83. }
  84. if (line.includes('-co-jp')) {
  85. domainCountMap[apexDomain] += (isPhishingDomainMockingAmazon ? 4.5 : 0.5);
  86. }
  87. const tld = parsed.publicSuffix;
  88. if (!tld || !BLACK_TLD.has(tld)) continue;
  89. domainCountMap[apexDomain] += 1;
  90. if (line.length > 19) {
  91. // Add more weight if the domain is long enough
  92. if (line.length > 44) {
  93. domainCountMap[apexDomain] += 3.5;
  94. } else if (line.length > 34) {
  95. domainCountMap[apexDomain] += 2.5;
  96. } else if (line.length > 29) {
  97. domainCountMap[apexDomain] += 1.5;
  98. } else if (line.length > 24) {
  99. domainCountMap[apexDomain] += 0.75;
  100. } else if (line.length > 19) {
  101. domainCountMap[apexDomain] += 0.25;
  102. }
  103. if (domainCountMap[apexDomain] < 5) {
  104. const subdomain = parsed.subdomain;
  105. if (subdomain && subdomain.includes('.')) {
  106. domainCountMap[apexDomain] += 1.5;
  107. }
  108. }
  109. }
  110. }
  111. }
  112. const results = traceSync('* get final results', () => Object.entries(domainCountMap)
  113. .reduce((acc, [apexDomain, count]) => {
  114. if (count >= 5) {
  115. acc.push(`.${apexDomain}`);
  116. }
  117. return acc;
  118. }, /** @type {string[]} */([]))
  119. .sort(domainSorter));
  120. const description = [
  121. 'License: AGPL 3.0',
  122. 'Homepage: https://ruleset.skk.moe',
  123. 'GitHub: https://github.com/SukkaW/Surge',
  124. '',
  125. 'The domainset supports enhanced phishing protection',
  126. 'Build from:',
  127. ' - https://gitlab.com/malware-filter/phishing-filter'
  128. ];
  129. await Promise.all(createRuleset(
  130. 'Sukka\'s Ruleset - Reject Phishing',
  131. description,
  132. new Date(),
  133. results,
  134. 'domainset',
  135. path.resolve(__dirname, '../List/domainset/reject_phishing.conf'),
  136. path.resolve(__dirname, '../Clash/domainset/reject_phishing.txt')
  137. ));
  138. };
  139. module.exports.buildPhishingDomainSet = buildPhishingDomainSet;
  140. if (require.main === module) {
  141. runner(__filename, buildPhishingDomainSet);
  142. }