get-phishing-domains.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. import { processDomainLists } from './parse-filter';
  2. import * as tldts from 'tldts-experimental';
  3. import { dummySpan, type Span } from '../trace';
  4. import { appendArrayInPlaceCurried } from './append-array-in-place';
  5. import { PHISHING_DOMAIN_LISTS_EXTRA } from '../constants/reject-data-source';
  6. import { loosTldOptWithPrivateDomains } from '../constants/loose-tldts-opt';
  7. import picocolors from 'picocolors';
  8. import createKeywordFilter from './aho-corasick';
  9. import { createCacheKey, deserializeArray, fsFetchCache, serializeArray } from './cache-filesystem';
  10. import { fastStringArrayJoin } from './misc';
  11. import { sha256 } from 'hash-wasm';
  12. const BLACK_TLD = new Set([
  13. 'accountant',
  14. 'autos',
  15. 'bar',
  16. 'beauty',
  17. 'bid',
  18. 'biz',
  19. 'bond',
  20. 'business',
  21. 'buzz',
  22. 'cc',
  23. 'cf',
  24. 'cfd',
  25. 'click',
  26. 'cloud',
  27. 'club',
  28. 'cn',
  29. 'codes',
  30. 'co.uk',
  31. 'co.in',
  32. 'com.br',
  33. 'com.cn',
  34. 'com.pl',
  35. 'com.vn',
  36. 'cool',
  37. 'cricket',
  38. 'cyou',
  39. 'date',
  40. 'digital',
  41. 'download',
  42. 'faith',
  43. 'fit',
  44. 'fun',
  45. 'ga',
  46. 'gd',
  47. 'gives',
  48. 'gq',
  49. 'group',
  50. 'host',
  51. 'icu',
  52. 'id',
  53. 'info',
  54. 'ink',
  55. 'life',
  56. 'live',
  57. 'link',
  58. 'loan',
  59. 'lol',
  60. 'ltd',
  61. 'me',
  62. 'men',
  63. 'ml',
  64. 'mobi',
  65. 'mom',
  66. 'net.pl',
  67. 'one',
  68. 'online',
  69. 'party',
  70. 'pro',
  71. 'pl',
  72. 'pw',
  73. 'racing',
  74. 'rest',
  75. 'review',
  76. 'rf.gd',
  77. 'sa.com',
  78. 'sbs',
  79. 'science',
  80. 'shop',
  81. 'site',
  82. 'space',
  83. 'store',
  84. 'stream',
  85. 'surf',
  86. 'tech',
  87. 'tk',
  88. 'tokyo',
  89. 'top',
  90. 'trade',
  91. 'vip',
  92. 'vn',
  93. 'webcam',
  94. 'website',
  95. 'win',
  96. 'xyz',
  97. 'za.com',
  98. 'lat',
  99. 'design'
  100. ]);
  101. const WHITELIST_MAIN_DOMAINS = new Set([
  102. // 'w3s.link', // ipfs gateway
  103. // 'dweb.link', // ipfs gateway
  104. // 'nftstorage.link', // ipfs gateway
  105. 'fleek.cool', // ipfs gateway
  106. 'business.site', // Drag'n'Drop site building platform
  107. 'page.link', // Firebase URL Shortener
  108. // 'notion.site',
  109. // 'vercel.app',
  110. 'gitbook.io'
  111. ]);
  112. const sensitiveKeywords = createKeywordFilter([
  113. '-roblox',
  114. '.amazon-',
  115. '-amazon',
  116. 'fb-com',
  117. 'facebook.',
  118. 'facebook-',
  119. '.facebook',
  120. '-facebook',
  121. 'coinbase',
  122. 'metamask-',
  123. '-metamask',
  124. 'virus-',
  125. 'icloud-',
  126. 'apple-',
  127. 'www.apple',
  128. '-coinbase',
  129. 'coinbase-',
  130. 'lcloud.',
  131. 'lcloud-',
  132. 'booking-com',
  133. 'booking.com-',
  134. 'booking-eu',
  135. 'vinted-cz',
  136. 'inpost-pl'
  137. ]);
  138. const lowKeywords = createKeywordFilter([
  139. '-co-jp',
  140. 'customer.',
  141. 'customer-',
  142. '.www-',
  143. 'instagram',
  144. 'microsoft'
  145. ]);
  146. const cacheKey = createCacheKey(__filename);
  147. export const getPhishingDomains = (parentSpan: Span) => parentSpan.traceChild('get phishing domains').traceAsyncFn(async (span) => {
  148. const domainArr = await span.traceChildAsync('download/parse/merge phishing domains', async (curSpan) => {
  149. const domainArr: string[] = [];
  150. (await Promise.all(PHISHING_DOMAIN_LISTS_EXTRA.map(entry => processDomainLists(curSpan, ...entry, cacheKey))))
  151. .forEach(appendArrayInPlaceCurried(domainArr));
  152. return domainArr;
  153. });
  154. return span.traceChildAsync(
  155. 'process phishing domain set',
  156. () => processPhihsingDomains(domainArr)
  157. );
  158. });
  159. async function processPhihsingDomains(domainArr: string[]) {
  160. const hash = await sha256(fastStringArrayJoin(domainArr, '|'));
  161. return fsFetchCache.apply(
  162. cacheKey('processPhihsingDomains|' + hash),
  163. () => {
  164. const domainCountMap: Record<string, number> = {};
  165. const domainScoreMap: Record<string, number> = {};
  166. for (let i = 0, len = domainArr.length; i < len; i++) {
  167. const line = domainArr[i];
  168. const {
  169. publicSuffix: tld,
  170. domain: apexDomain,
  171. subdomain,
  172. isPrivate
  173. } = tldts.parse(line, loosTldOptWithPrivateDomains);
  174. if (isPrivate) {
  175. continue;
  176. }
  177. if (!tld) {
  178. console.log(picocolors.yellow('[phishing domains] E0001'), 'missing tld', { line, tld });
  179. continue;
  180. }
  181. if (!apexDomain) {
  182. console.log(picocolors.yellow('[phishing domains] E0002'), 'missing domain', { line, apexDomain });
  183. continue;
  184. }
  185. domainCountMap[apexDomain] ||= 0;
  186. domainCountMap[apexDomain] += 1;
  187. if (!(apexDomain in domainScoreMap)) {
  188. domainScoreMap[apexDomain] = 0;
  189. if (BLACK_TLD.has(tld)) {
  190. domainScoreMap[apexDomain] += 4;
  191. } else if (tld.length > 6) {
  192. domainScoreMap[apexDomain] += 2;
  193. }
  194. }
  195. if (
  196. subdomain
  197. && !WHITELIST_MAIN_DOMAINS.has(apexDomain)
  198. ) {
  199. domainScoreMap[apexDomain] += calcDomainAbuseScore(subdomain);
  200. }
  201. }
  202. for (const apexDomain in domainCountMap) {
  203. if (
  204. // !WHITELIST_MAIN_DOMAINS.has(apexDomain)
  205. domainScoreMap[apexDomain] >= 12
  206. || (domainScoreMap[apexDomain] >= 5 && domainCountMap[apexDomain] >= 4)
  207. ) {
  208. domainArr.push(`.${apexDomain}`);
  209. }
  210. }
  211. return Promise.resolve(domainArr);
  212. },
  213. {
  214. ttl: 2 * 86400,
  215. serializer: serializeArray,
  216. deserializer: deserializeArray,
  217. incrementTtlWhenHit: true
  218. }
  219. );
  220. }
  221. export function calcDomainAbuseScore(subdomain: string) {
  222. let weight = 0;
  223. const hitLowKeywords = lowKeywords(subdomain);
  224. const sensitiveKeywordsHit = sensitiveKeywords(subdomain);
  225. if (sensitiveKeywordsHit) {
  226. weight += 8;
  227. if (hitLowKeywords) {
  228. weight += 4;
  229. }
  230. } else if (hitLowKeywords) {
  231. weight += 1;
  232. }
  233. const subdomainLength = subdomain.length;
  234. if (subdomainLength > 4) {
  235. weight += 0.5;
  236. if (subdomainLength > 10) {
  237. weight += 0.5;
  238. if (subdomainLength > 20) {
  239. weight += 1;
  240. if (subdomainLength > 30) {
  241. weight += 2;
  242. if (subdomainLength > 40) {
  243. weight += 4;
  244. }
  245. }
  246. }
  247. }
  248. if (subdomain.startsWith('www.')) {
  249. weight += 4;
  250. } else if (subdomain.slice(1).includes('.')) {
  251. weight += 1;
  252. if (subdomain.includes('www.')) {
  253. weight += 4;
  254. }
  255. }
  256. }
  257. return weight;
  258. }
  259. if (require.main === module) {
  260. getPhishingDomains(dummySpan)
  261. .catch(console.error);
  262. }