build-phishing-domainset.js 4.0 KB

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