get-phishing-domains.ts 7.7 KB

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