get-phishing-domains.ts 6.5 KB

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