get-phishing-domains.ts 7.3 KB

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