get-phishing-domains.ts 7.1 KB

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