build-phishing-domainset.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import { processDomainLists, processHosts } from './lib/parse-filter';
  2. import path from 'path';
  3. import { createRuleset } from './lib/create-file';
  4. import { processLine } from './lib/process-line';
  5. import { createDomainSorter } from './lib/stable-sort-domain';
  6. import { traceSync, task } from './lib/trace-runner';
  7. import { createTrie } from './lib/trie';
  8. import { getGorhillPublicSuffixPromise } from './lib/get-gorhill-publicsuffix';
  9. import { createCachedGorhillGetDomain } from './lib/cached-tld-parse';
  10. import * as tldts from 'tldts';
  11. import { SHARED_DESCRIPTION } from './lib/constants';
  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. 'autos',
  24. 'bar',
  25. 'biz',
  26. 'bond',
  27. 'business',
  28. 'buzz',
  29. 'cc',
  30. 'cf',
  31. 'cfd',
  32. 'click',
  33. 'cloud',
  34. 'club',
  35. 'cn',
  36. 'codes',
  37. 'com.cn',
  38. 'cool',
  39. 'cyou',
  40. 'fit',
  41. 'fun',
  42. 'ga',
  43. 'gd',
  44. 'gq',
  45. 'group',
  46. 'host',
  47. 'icu',
  48. 'id',
  49. 'info',
  50. 'ink',
  51. 'life',
  52. 'live',
  53. 'link',
  54. 'ltd',
  55. 'ml',
  56. 'mobi',
  57. 'one',
  58. 'online',
  59. 'pro',
  60. 'pl',
  61. 'pw',
  62. 'rest',
  63. 'rf.gd',
  64. 'sa.com',
  65. 'sbs',
  66. 'shop',
  67. 'site',
  68. 'space',
  69. 'store',
  70. 'tech',
  71. 'tk',
  72. 'tokyo',
  73. 'top',
  74. 'vip',
  75. 'vn',
  76. 'website',
  77. 'win',
  78. 'xyz',
  79. 'za.com'
  80. ]);
  81. export const buildPhishingDomainSet = task(import.meta.path, async () => {
  82. const [domainSet, gorhill] = await Promise.all([
  83. processHosts('https://curbengh.github.io/phishing-filter/phishing-filter-hosts.txt', true, true),
  84. // processDomainLists('https://phishing.army/download/phishing_army_blocklist.txt', true),
  85. // processFilterRules(
  86. // 'https://curbengh.github.io/phishing-filter/phishing-filter-agh.txt',
  87. // [
  88. // 'https://phishing-filter.pages.dev/phishing-filter-agh.txt'
  89. // // Prefer mirror, since malware-filter.gitlab.io has not been updated for a while
  90. // // 'https://malware-filter.gitlab.io/malware-filter/phishing-filter-agh.txt'
  91. // ]
  92. // ),
  93. getGorhillPublicSuffixPromise()
  94. ]);
  95. // _domainSet2.forEach(i => domainSet.add(i));
  96. traceSync('* whitelist', () => {
  97. const trieForRemovingWhiteListed = createTrie(domainSet);
  98. WHITELIST_DOMAIN.forEach(white => {
  99. trieForRemovingWhiteListed.find(`.${white}`, false).forEach(f => domainSet.delete(f));
  100. if (trieForRemovingWhiteListed.has(white)) {
  101. domainSet.delete(white);
  102. }
  103. });
  104. });
  105. const domainCountMap: Record<string, number> = {};
  106. const getDomain = createCachedGorhillGetDomain(gorhill);
  107. traceSync('* process domain set', () => {
  108. const domainArr = Array.from(domainSet);
  109. for (let i = 0, len = domainArr.length; i < len; i++) {
  110. const line = processLine(domainArr[i]);
  111. if (!line) continue;
  112. const apexDomain = getDomain(line);
  113. if (!apexDomain) continue;
  114. domainCountMap[apexDomain] ||= 0;
  115. const isPhishingDomainMockingCoJp = line.includes('-co-jp');
  116. if (isPhishingDomainMockingCoJp) {
  117. domainCountMap[apexDomain] += 0.5;
  118. }
  119. if (line.startsWith('.amaz')) {
  120. domainCountMap[apexDomain] += 0.5;
  121. if (line.startsWith('.amazon-')) {
  122. domainCountMap[apexDomain] += 4.5;
  123. }
  124. if (isPhishingDomainMockingCoJp) {
  125. domainCountMap[apexDomain] += 4;
  126. }
  127. } else if (line.startsWith('.customer')) {
  128. domainCountMap[apexDomain] += 0.25;
  129. }
  130. const tld = gorhill.getPublicSuffix(line[0] === '.' ? line.slice(1) : line);
  131. if (!tld || !BLACK_TLD.has(tld)) continue;
  132. domainCountMap[apexDomain] += 1;
  133. const lineLen = line.length;
  134. if (lineLen > 19) {
  135. // Add more weight if the domain is long enough
  136. if (lineLen > 44) {
  137. domainCountMap[apexDomain] += 3.5;
  138. } else if (lineLen > 34) {
  139. domainCountMap[apexDomain] += 2.5;
  140. } else if (lineLen > 29) {
  141. domainCountMap[apexDomain] += 1.5;
  142. } else if (lineLen > 24) {
  143. domainCountMap[apexDomain] += 0.75;
  144. } else {
  145. domainCountMap[apexDomain] += 0.25;
  146. }
  147. if (domainCountMap[apexDomain] < 5) {
  148. const subdomain = tldts.getSubdomain(line, { detectIp: false });
  149. if (subdomain?.includes('.')) {
  150. domainCountMap[apexDomain] += 1.5;
  151. }
  152. }
  153. }
  154. }
  155. });
  156. const domainSorter = createDomainSorter(gorhill);
  157. const results = traceSync('* get final results', () => Object.entries(domainCountMap)
  158. .reduce<string[]>((acc, [apexDomain, count]) => {
  159. if (count >= 5) {
  160. acc.push(`.${apexDomain}`);
  161. }
  162. return acc;
  163. }, [])
  164. .sort(domainSorter));
  165. const description = [
  166. ...SHARED_DESCRIPTION,
  167. '',
  168. 'The domainset supports enhanced phishing protection',
  169. 'Build from:',
  170. ' - https://gitlab.com/malware-filter/phishing-filter'
  171. ];
  172. return Promise.all(createRuleset(
  173. 'Sukka\'s Ruleset - Reject Phishing',
  174. description,
  175. new Date(),
  176. results,
  177. 'domainset',
  178. path.resolve(import.meta.dir, '../List/domainset/reject_phishing.conf'),
  179. path.resolve(import.meta.dir, '../Clash/domainset/reject_phishing.txt')
  180. ));
  181. });
  182. if (import.meta.main) {
  183. buildPhishingDomainSet();
  184. }