build-phishing-domainset.js 4.9 KB

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