get-phishing-domains.ts 5.3 KB

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