get-phishing-domains.ts 6.0 KB

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