get-phishing-domains.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. 'business.site', // Drag'n'Drop site building platform
  41. 'page.link', // Firebase URL Shortener
  42. // 'notion.site',
  43. // 'vercel.app',
  44. 'gitbook.io',
  45. 'zendesk.com'
  46. ]);
  47. const leathalKeywords = createKeywordFilter([
  48. 'vinted-',
  49. 'inpost-pl',
  50. 'vlnted-'
  51. ]);
  52. const sensitiveKeywords = createKeywordFilter([
  53. '.amazon-',
  54. '-amazon',
  55. 'fb-com',
  56. 'facebook-com',
  57. '-facebook',
  58. 'facebook-',
  59. 'focebaak',
  60. '.facebook.',
  61. 'metamask',
  62. 'www.apple',
  63. '-coinbase',
  64. 'coinbase-',
  65. 'booking-com',
  66. 'booking.com-',
  67. 'booking-eu',
  68. 'vinted-',
  69. 'inpost-pl',
  70. 'login.microsoft',
  71. 'login-microsoft',
  72. 'microsoftonline',
  73. 'google.com-',
  74. 'minecraft',
  75. 'staemco',
  76. 'oferta'
  77. ]);
  78. const lowKeywords = createKeywordFilter([
  79. 'transactions-',
  80. 'payment',
  81. 'wallet',
  82. '-transactions',
  83. '-faceb', // facebook fake
  84. '.faceb', // facebook fake
  85. 'facebook',
  86. 'virus-',
  87. 'icloud-',
  88. 'apple-',
  89. '-roblox',
  90. '-co-jp',
  91. 'customer.',
  92. 'customer-',
  93. '.www-',
  94. '.www.',
  95. '.www2',
  96. 'instagram',
  97. 'microsof',
  98. 'passwordreset',
  99. '.google-',
  100. 'recover',
  101. 'banking'
  102. ]);
  103. const cacheKey = createCacheKey(__filename);
  104. const processPhihsingDomains = cache(function processPhihsingDomains(domainArr: string[]): Promise<string[]> {
  105. const domainCountMap: Record<string, number> = {};
  106. const domainScoreMap: Record<string, number> = {};
  107. for (let i = 0, len = domainArr.length; i < len; i++) {
  108. const line = domainArr[i];
  109. const {
  110. publicSuffix: tld,
  111. domain: apexDomain,
  112. subdomain,
  113. isPrivate
  114. } = tldts.parse(line, loosTldOptWithPrivateDomains);
  115. if (isPrivate) {
  116. continue;
  117. }
  118. if (!tld) {
  119. console.log(picocolors.yellow('[phishing domains] E0001'), 'missing tld', { line, tld });
  120. continue;
  121. }
  122. if (!apexDomain) {
  123. console.log(picocolors.yellow('[phishing domains] E0002'), 'missing domain', { line, apexDomain });
  124. continue;
  125. }
  126. domainCountMap[apexDomain] ||= 0;
  127. domainCountMap[apexDomain] += 1;
  128. if (!(apexDomain in domainScoreMap)) {
  129. domainScoreMap[apexDomain] = 0;
  130. if (BLACK_TLD.has(tld)) {
  131. domainScoreMap[apexDomain] += 4;
  132. } else if (tld.length > 6) {
  133. domainScoreMap[apexDomain] += 2;
  134. }
  135. if (apexDomain.length >= 18) {
  136. domainScoreMap[apexDomain] += 0.5;
  137. }
  138. }
  139. if (
  140. subdomain
  141. && !WHITELIST_MAIN_DOMAINS.has(apexDomain)
  142. ) {
  143. domainScoreMap[apexDomain] += calcDomainAbuseScore(subdomain, line);
  144. }
  145. }
  146. for (const apexDomain in domainCountMap) {
  147. if (
  148. // !WHITELIST_MAIN_DOMAINS.has(apexDomain)
  149. (domainScoreMap[apexDomain] >= 24)
  150. || (domainScoreMap[apexDomain] >= 16 && domainCountMap[apexDomain] >= 7)
  151. || (domainScoreMap[apexDomain] >= 13 && domainCountMap[apexDomain] >= 11)
  152. || (domainScoreMap[apexDomain] >= 5 && domainCountMap[apexDomain] >= 14)
  153. || (domainScoreMap[apexDomain] >= 3 && domainCountMap[apexDomain] >= 20)
  154. ) {
  155. domainArr.push('.' + apexDomain);
  156. }
  157. }
  158. // console.log({
  159. // score: domainScoreMap['shoppy.gg'],
  160. // count: domainCountMap['shoppy.gg']
  161. // });
  162. return Promise.resolve(domainArr);
  163. }, {
  164. serializer: serializeArray,
  165. deserializer: deserializeArray
  166. });
  167. export function getPhishingDomains(parentSpan: Span) {
  168. return parentSpan.traceChild('get phishing domains').traceAsyncFn(async (span) => {
  169. const domainArr = await span.traceChildAsync('download/parse/merge phishing domains', async (curSpan) => {
  170. const domainArr: string[] = [];
  171. await Promise.all([
  172. ...PHISHING_DOMAIN_LISTS_EXTRA.map(entry => processDomainLists(curSpan, ...entry, cacheKey)),
  173. ...PHISHING_HOSTS_EXTRA.map(entry => processHosts(curSpan, ...entry, cacheKey))
  174. ]).then(domainGroups => domainGroups.forEach(appendArrayInPlaceCurried(domainArr)));
  175. return domainArr;
  176. });
  177. return span.traceChildAsync(
  178. 'process phishing domain set',
  179. () => processPhihsingDomains(domainArr)
  180. );
  181. });
  182. }
  183. export function calcDomainAbuseScore(subdomain: string, fullDomain: string = subdomain) {
  184. if (leathalKeywords(fullDomain)) {
  185. return 100;
  186. }
  187. let weight = 0;
  188. const hitLowKeywords = lowKeywords(fullDomain);
  189. const sensitiveKeywordsHit = sensitiveKeywords(fullDomain);
  190. if (sensitiveKeywordsHit) {
  191. weight += 10;
  192. if (hitLowKeywords) {
  193. weight += 6;
  194. }
  195. } else if (hitLowKeywords) {
  196. weight += 1.5;
  197. }
  198. const subdomainLength = subdomain.length;
  199. if (subdomainLength > 13) {
  200. weight += 0.2;
  201. if (subdomainLength > 20) {
  202. weight += 1;
  203. if (subdomainLength > 30) {
  204. weight += 5;
  205. if (subdomainLength > 40) {
  206. weight += 10;
  207. }
  208. }
  209. }
  210. if (subdomain.slice(1).includes('.')) {
  211. weight += 1;
  212. if (subdomain.includes('www.')) {
  213. weight += 1;
  214. }
  215. }
  216. }
  217. return weight;
  218. }
  219. if (require.main === module) {
  220. getPhishingDomains(dummySpan)
  221. .catch(console.error);
  222. }