@@ -66,7 +66,149 @@ const UsefulScriptGlobalWebpageContext = {
6666} ;
6767window . UsefulScriptGlobalWebpageContext = UsefulScriptGlobalWebpageContext ;
6868
69+ // Chứa các hàm hỗ trợ việc hack web :))
70+ const UsefulScriptsUtils = {
71+ // Có trang web tự động xoá console để ngăn cản người dùng xem kết quả thực thi câu lệnh trong console
72+ // Ví dụ: https://beta.nhaccuatui.com/
73+ // Hàm này sẽ tắt chức năng tự động clear console đó, giúp hacker dễ hack hơn :)
74+ disableAutoConsoleClear ( ) {
75+ window . console . clear = ( ) => null ;
76+ console . log ( "Auto console.clear DISABLED!" ) ;
77+ } ,
78+
79+ // Hiển thị tất cả các biến toàn cục được tạo ra trong trang web
80+ // https://mmazzarolo.com/blog/2022-02-14-find-what-javascript-variables-are-leaking-into-the-global-scope/
81+ listGlobalVariables ( ) {
82+ let browserGlobals = [ ] ;
83+ const ignoredGlobals = [ "UsefulScriptsUtils" ] ;
84+
85+ function collectBrowserGlobals ( ) {
86+ const iframe = window . document . createElement ( "iframe" ) ;
87+ iframe . src = "about:blank" ;
88+ window . document . body . appendChild ( iframe ) ;
89+ let globals = Object . keys ( iframe . contentWindow ) ;
90+ window . document . body . removeChild ( iframe ) ;
91+ return globals ;
92+ }
93+
94+ function getRuntimeGlobals ( ) {
95+ if ( browserGlobals . length === 0 ) {
96+ browserGlobals = collectBrowserGlobals ( ) ;
97+ }
98+ const runtimeGlobals = Object . keys ( window ) . filter (
99+ ( key ) => ! ignoredGlobals . includes ( key ) && ! browserGlobals . includes ( key )
100+ ) ;
101+ const runtimeGlobalsObj = { } ;
102+ runtimeGlobals . forEach ( ( key , i ) => {
103+ runtimeGlobalsObj [ key ] = window [ key ] ;
104+ } ) ;
105+ return runtimeGlobalsObj ;
106+ }
107+
108+ return getRuntimeGlobals ( ) ;
109+ } ,
110+
111+ // https://mmazzarolo.com/blog/2022-07-30-checking-if-a-javascript-native-function-was-monkey-patched/
112+ // Kiểm tra xem function nào đó có bị override hay chưa
113+ isNativeFunction ( f ) {
114+ return f . toString ( ) . toString ( ) . includes ( "[native code]" ) ;
115+ } ,
116+
117+ // https://mmazzarolo.com/blog/2022-06-26-filling-local-storage-programmatically/
118+ // Làm đầy localStorage
119+ fillLocalStorage ( ) {
120+ const key = "__filling_localstorage__" ;
121+ let max = 1 ;
122+ let data = "x" ;
123+ try {
124+ while ( true ) {
125+ data = data + data ;
126+ localStorage . setItem ( key , data ) ;
127+ max <<= 1 ;
128+ }
129+ } catch { }
130+ for ( let bit = max >> 1 ; bit > 0 ; bit >>= 1 ) {
131+ try {
132+ localStorage . setItem ( key , data . substring ( 0 , max | bit ) ) ;
133+ max |= bit ;
134+ } catch {
135+ console . success ( "Storage is now completely full 🍟" ) ;
136+ }
137+ }
138+ return function cleanup ( ) {
139+ localStorage . removeItem ( key ) ;
140+ console . success ( "Storage is cleaned" ) ;
141+ } ;
142+ } ,
143+
144+ // https://mmazzarolo.com/blog/2022-02-16-track-down-the-javascript-code-responsible-for-polluting-the-global-scope/
145+ globalsDebugger ( varName = "" ) {
146+ // https://stackoverflow.com/a/56933091/11898496
147+ const urlParams = new URLSearchParams ( window . location . search ) ;
148+ urlParams . set ( "globalsToInspect" , varName ) ;
149+ window . location . search = urlParams . toString ( ) ;
150+ } ,
151+
152+ // Tìm chuỗi xung quanh chuỗi bất kỳ
153+ // Ví dụ fullString = "abcd1234567890abcd" targetString = "6" bound = 3
154+ // => Kết quả around = 3456789
155+ getTextAround ( fullString , targetString , bound = 10 ) {
156+ let curIndex = 0 ;
157+ let arounds = [ ] ;
158+ let limit = 100 ;
159+
160+ while ( limit ) {
161+ let index = fullString . indexOf ( targetString , curIndex ) ;
162+ if ( index === - 1 ) break ;
163+
164+ let around = fullString . slice (
165+ Math . max ( index - Math . floor ( bound / 2 ) - 1 , 0 ) ,
166+ Math . min (
167+ index + targetString . length + Math . floor ( bound / 2 ) ,
168+ fullString . length
169+ )
170+ ) ;
171+ arounds . push ( { index, around } ) ;
172+ curIndex = index + ( targetString . length || 1 ) ;
173+ limit -- ;
174+ }
175+ return arounds ;
176+ } ,
177+
178+ // https://stackoverflow.com/a/40410744/11898496
179+ // Giải mã từ dạng 'http\\u00253A\\u00252F\\u00252Fexample.com' về 'http://example.com'
180+ decodeEscapedUnicodeString ( str ) {
181+ return decodeURIComponent (
182+ JSON . parse ( '"' + str . replace ( / \" / g, '\\"' ) + '"' )
183+ ) ;
184+ } ,
185+
186+ // https://stackoverflow.com/a/8649003
187+ searchParamsToObject ( search ) {
188+ // let d = {};
189+ // decodeURI(search)
190+ // .split("&")
191+ // .map((_) => _.split("="))
192+ // .forEach((_) => (d[_[0]] = _[1]));
193+ // return d;
194+
195+ search = search || location . search . substring ( 1 ) ;
196+ return JSON . parse (
197+ '{"' + search . replace ( / & / g, '","' ) . replace ( / = / g, '":"' ) + '"}' ,
198+ function ( key , value ) {
199+ return key === "" ? value : decodeURIComponent ( value ) ;
200+ }
201+ ) ;
202+ } ,
203+ } ;
204+ window . UsefulScriptsUtils = UsefulScriptsUtils ;
205+
69206// ================================= Polyfill =================================
70207// Chrome pre-34
71208if ( ! Element . prototype . matches )
72209 Element . prototype . matches = Element . prototype . webkitMatchesSelector ;
210+
211+ // https://mmazzarolo.com/blog/2022-08-25-simple-colored-logging-for-javascript-clis/
212+ window . console . success = ( ...args ) => console . log ( "\x1b[32m✔\x1b[0m" , ...args ) ;
213+ window . console . failure = ( ...args ) =>
214+ console . error ( "\x1b[31mx\x1b[0m" , ...args ) ;
0 commit comments