get-phishing-domains.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. ]);
  83. const lowKeywords = createKeywordFilter([
  84. 'transactions-',
  85. 'payment',
  86. 'wallet',
  87. '-transactions',
  88. '-faceb', // facebook fake
  89. '.faceb', // facebook fake
  90. 'facebook',
  91. 'virus-',
  92. 'icloud-',
  93. 'apple-',
  94. '-roblox',
  95. '-co-jp',
  96. 'customer.',
  97. 'customer-',
  98. '.www-',
  99. '.www.',
  100. '.www2',
  101. 'instagram',
  102. 'microsof',
  103. 'passwordreset',
  104. '.google-',
  105. 'recover',
  106. 'banking'
  107. ]);
  108. const processPhihsingDomains = cache(function processPhihsingDomains(domainArr: string[]): string[] {
  109. const domainCountMap = new Map<string, number>();
  110. const domainScoreMap: Record<string, number> = {};
  111. let line = '';
  112. let tld: string | null = '';
  113. let apexDomain: string | null = '';
  114. let subdomain: string | null = '';
  115. // const set = new Set<string>();
  116. // let duplicateCount = 0;
  117. for (let i = 0, len = domainArr.length; i < len; i++) {
  118. line = domainArr[i];
  119. // if (set.has(line)) {
  120. // duplicateCount++;
  121. // } else {
  122. // set.add(line);
  123. // }
  124. const parsed = tldts.parse(line, loosTldOptWithPrivateDomains);
  125. if (parsed.isPrivate) {
  126. continue;
  127. }
  128. tld = parsed.publicSuffix;
  129. apexDomain = parsed.domain;
  130. if (!tld) {
  131. console.log(picocolors.yellow('[phishing domains] E0001'), 'missing tld', { line, tld });
  132. continue;
  133. }
  134. if (!apexDomain) {
  135. console.log(picocolors.yellow('[phishing domains] E0002'), 'missing domain', { line, apexDomain });
  136. continue;
  137. }
  138. domainCountMap.set(
  139. apexDomain,
  140. domainCountMap.has(apexDomain)
  141. ? domainCountMap.get(apexDomain)! + 1
  142. : 1
  143. );
  144. if (!(apexDomain in domainScoreMap)) {
  145. domainScoreMap[apexDomain] = 0;
  146. if (BLACK_TLD.has(tld)) {
  147. domainScoreMap[apexDomain] += 3;
  148. } else if (tld.length > 6) {
  149. domainScoreMap[apexDomain] += 2;
  150. }
  151. if (apexDomain.length >= 18) {
  152. domainScoreMap[apexDomain] += 0.5;
  153. }
  154. }
  155. subdomain = parsed.subdomain;
  156. if (
  157. subdomain
  158. && !WHITELIST_MAIN_DOMAINS.has(apexDomain)
  159. ) {
  160. domainScoreMap[apexDomain] += calcDomainAbuseScore(subdomain, line);
  161. }
  162. }
  163. domainCountMap.forEach((count, apexDomain) => {
  164. if (
  165. // !WHITELIST_MAIN_DOMAINS.has(apexDomain)
  166. (domainScoreMap[apexDomain] >= 24)
  167. || (domainScoreMap[apexDomain] >= 16 && count >= 7)
  168. || (domainScoreMap[apexDomain] >= 13 && count >= 11)
  169. || (domainScoreMap[apexDomain] >= 5 && count >= 14)
  170. || (domainScoreMap[apexDomain] >= 3 && count >= 21)
  171. || (domainScoreMap[apexDomain] >= 1 && count >= 60)
  172. ) {
  173. domainArr.push('.' + apexDomain);
  174. }
  175. });
  176. // console.log({
  177. // score: domainScoreMap['awicksin.com'],
  178. // count: domainCountMap.get('awicksin.com')
  179. // });
  180. // console.log({ duplicateCount, domainArrLen: domainArr.length });
  181. return domainArr;
  182. }, {
  183. serializer: serializeArray,
  184. deserializer: deserializeArray,
  185. temporaryBypass: !isCI || DEBUG_DOMAIN_TO_FIND !== null
  186. });
  187. const downloads = [
  188. ...PHISHING_DOMAIN_LISTS_EXTRA.map(entry => processDomainListsWithPreload(...entry)),
  189. ...PHISHING_HOSTS_EXTRA.map(entry => processHostsWithPreload(...entry))
  190. ];
  191. export function getPhishingDomains(parentSpan: Span) {
  192. return parentSpan.traceChild('get phishing domains').traceAsyncFn(async (span) => {
  193. const domainArr = await span.traceChildAsync('download/parse/merge phishing domains', async (curSpan) => {
  194. const domainArr: string[] = [];
  195. const domainGroups = await Promise.all(downloads.map(task => task(curSpan)));
  196. domainGroups.forEach(appendArrayInPlaceCurried(domainArr));
  197. return domainArr;
  198. });
  199. return span.traceChildAsync(
  200. 'process phishing domain set',
  201. () => processPhihsingDomains(domainArr)
  202. );
  203. });
  204. }
  205. export function calcDomainAbuseScore(subdomain: string, fullDomain: string = subdomain) {
  206. if (leathalKeywords(fullDomain)) {
  207. return 100;
  208. }
  209. let weight = 0;
  210. const hitLowKeywords = lowKeywords(fullDomain);
  211. const sensitiveKeywordsHit = sensitiveKeywords(fullDomain);
  212. if (sensitiveKeywordsHit) {
  213. weight += 10;
  214. if (hitLowKeywords) {
  215. weight += 6;
  216. }
  217. } else if (hitLowKeywords) {
  218. weight += 1.7;
  219. }
  220. const subdomainLength = subdomain.length;
  221. if (subdomainLength > 6) {
  222. weight += 0.015;
  223. if (subdomainLength > 13) {
  224. weight += 0.2;
  225. if (subdomainLength > 20) {
  226. weight += 1;
  227. if (subdomainLength > 30) {
  228. weight += 5;
  229. if (subdomainLength > 40) {
  230. weight += 10;
  231. }
  232. }
  233. }
  234. if (subdomain.indexOf('.', 1) > 1) {
  235. weight += 1;
  236. }
  237. }
  238. }
  239. return weight;
  240. }
  241. if (require.main === module) {
  242. getPhishingDomains(dummySpan)
  243. .catch(console.error)
  244. .finally(() => {
  245. dummySpan.stop();
  246. printTraceResult(dummySpan.traceResult);
  247. });
  248. }