難読化シェル芸 » 履歴 » バージョン 2
kanata, 2025/04/13 09:52
1 | 1 | kanata | # 難読化シェル芸 |
---|---|---|---|
2 | |||
3 | Obfuscation Shell Script One Liner |
||
4 | |||
5 | {{toc}} |
||
6 | |||
7 | # 難読化シェル芸とは |
||
8 | |||
9 | {{rawhtml(<iframe src="//www.slideshare.net/slideshow/embed_code/key/VRc39vc7cTcDx" width="595" height="485" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" style="border:1px solid #CCC; border-width:1px; margin-bottom:5px; max-width: 100%;" allowfullscreen> </iframe> <div style="margin-bottom:5px"> <strong> <a href="//www.slideshare.net/kanata1/ss-75315943" title="難読化シェル芸" target="_blank">難読化シェル芸</a> </strong> from <strong><a target="_blank" href="//www.slideshare.net/kanata1">kanata -</a></strong> </div>)}} |
||
10 | |||
11 | **内部的な動作の手続き内容・構造・データなどを人間が理解しにくい、あるいはそのようになるよう加工されたシェル芸のこと。** |
||
12 | |||
13 | 例えばこんなの |
||
14 | |||
15 | 普通のdateコマンド |
||
16 | |||
17 | ~~~bash |
||
18 | date |
||
19 | 2016年 12月 16日 金曜日 21:19:58 JST |
||
20 | ~~~ |
||
21 | |||
22 | 難読化dateコマンド |
||
23 | |||
24 | ~~~bash |
||
25 | $'\x64\x61\x74\x65' |
||
26 | 2017年 3月 4日 土曜日 13:36:21 JST |
||
27 | ~~~ |
||
28 | |||
29 | ~~~bash |
||
30 | $(printf "%b" $(printf '%s%x' '\x' $((0x83 ^ 0xe7))))$(ls --help|grep ^G|cut -c53)$(ls --help|grep ^G|cut -c10)$(ls --help|grep ^G|cut -c8) |
||
31 | 2016年 12月 16日 金曜日 21:19:52 JST |
||
32 | ~~~ |
||
33 | |||
34 | # 難読化の基本 |
||
35 | |||
36 | ## アスキーコードを記述して実行 |
||
37 | |||
38 | |||
39 | |||
40 | [俺的備忘録 〜なんかいろいろ〜 bashでコマンドをアスキーコードで記述して実行させる](https://orebibou.com/2017/03/bash%E3%81%A7%E3%82%B3%E3%83%9E%E3%83%B3%E3%83%89%E3%82%92%E3%82%A2%E3%82%B9%E3%82%AD%E3%83%BC%E3%82%B3%E3%83%BC%E3%83%89%E3%81%A7%E8%A8%98%E8%BF%B0%E3%81%97%E3%81%A6%E5%AE%9F%E8%A1%8C%E3%81%95/) |
||
41 | |||
42 | ~~~bash |
||
43 | $ echo -n 'date'|xxd |
||
44 | 0000000: 6461 7465 date |
||
45 | $ $'\x64\x61\x74\x65' # date 16進数表記 |
||
46 | 2017年 3月 4日 土曜日 13:36:21 JST |
||
47 | $ $'\144\141\164\145' # date 8進数 |
||
48 | 2017年 3月 4日 土曜日 13:47:22 JST |
||
49 | $ $'\u0064\u0061\u0074\u0065' # date Unicodeの表記 |
||
50 | 2017年 3月 4日 土曜日 13:40:16 JST |
||
51 | ~~~ |
||
52 | |||
53 | ## コマンド置換で文字を生成して実行 |
||
54 | |||
55 | ``` |
||
56 | `example` または $(example) で文字を出力する。 |
||
57 | ``` |
||
58 | |||
59 | exampleの部分を複雑にすることで、難読化を図る |
||
60 | |||
61 | ```bash |
||
62 | `echo d``echo a`$(echo t)$(echo e) |
||
63 | 2017年 3月 4日 土曜日 13:44:33 JST |
||
64 | ``` |
||
65 | |||
66 | ## コマンドの間に無視される文字を挟み込む |
||
67 | |||
68 | ```bash |
||
69 | ''''''''''''''''''''''''''''''''''''''''''''''''''d''''''''''''''''''''''''''''''''''''''''''''''''''a''''''''''''''''''''''''''''''''''''''''''''''''''t''''''''''''''''''''''''''''''''''''''''''''''''''e'''''''''''''''''''''''''''''''''''''''''''''''''' |
||
70 | Sat Sep 21 20:00:59 JST 2019 |
||
71 | ``` |
||
72 | |||
73 | ```bash |
||
74 | d""a$()t``e |
||
75 | Sun Apr 19 10:43:58 JST 2020 |
||
76 | ``` |
||
77 | |||
78 | ## 無駄なブレース展開 |
||
79 | |||
80 | ```bash |
||
81 | $ echo {{{{{{{{{a..c},},},},},},},},} |
||
82 | a b c |
||
83 | ``` |
||
84 | |||
85 | ## evalを使って実行 |
||
86 | |||
87 | コマンド置換と同様。まとめ中 |
||
88 | |||
89 | ## manに載っていないsedのeオプションを使って実行 |
||
90 | |||
91 | sedのeオプションは、引数のコマンドを実行してくれるのでこれを利用する。 |
||
92 | eオプションは通常、manに載っていない。sedを熟知していないとやはり何しているか解りにくい。 |
||
93 | |||
94 | gnu.org の説明 |
||
95 | https://www.gnu.org/software/sed/manual/sed.html#sed-commands-list |
||
96 | |||
97 | ```bash |
||
98 | echo|sed 'e date' |
||
99 | 2017年 10月 29日 日曜日 10:50:50 JST |
||
100 | ``` |
||
101 | |||
102 | # 難読化技法 |
||
103 | |||
104 | ## フェイク(無駄な命令) |
||
105 | |||
106 | 例えば、以下は全て無視される。コードの間に入れる事でより複雑な難読化を図ることができる。 |
||
107 | |||
108 | ~~~bash |
||
109 | $(:;:;:;:;:;:;) |
||
110 | $(: poweroff :;) |
||
111 | $(: rm -rf /* ;) |
||
112 | ~~~ |
||
113 | |||
114 | 以下の条件で後続のコマンドが実行されるため、さらに難読化に利用できる |
||
115 | |||
116 | ```bash |
||
117 | : date # dateは実行されない |
||
118 | |||
119 | $(:) date # dateは実行される |
||
120 | 2017年 10月 2日 月曜日 20:06:55 JST |
||
121 | |||
122 | `:` date # dateは実行される |
||
123 | 2017年 10月 2日 月曜日 20:07:03 JST |
||
124 | ``` |
||
125 | |||
126 | ## フェイク(前半処理の無視) |
||
127 | |||
128 | 処理結果をパイプで渡しても、それを使わなければ実質、無視できる処理となる。 |
||
129 | 以下は、前半処理を無視して、dateを実行する例 |
||
130 | |||
131 | ```bash |
||
132 | $ echo hoge |grep hoge|date |
||
133 | 2018年 11月 4日 日曜日 17:20:12 JST |
||
134 | ``` |
||
135 | |||
136 | より複雑な難読化を考えるなら、処理の後半も無駄な処理にして、間に本当にやりたい処理を間に入れる方法が考えられますね。 |
||
137 | |||
138 | |||
139 | |||
140 | ## base64によるエンコード |
||
141 | |||
142 | base64にエンコードすることで、どのような命令かわからないようにする。 |
||
143 | |||
144 | dateの難読化 |
||
145 | |||
146 | ~~~bash |
||
147 | $(echo ZGF0ZQ==|base64 -d) |
||
148 | ~~~ |
||
149 | |||
150 | ちなみに例のアレ(:(){ :|:& };:)は、こんな感じ(実行厳禁!!) |
||
151 | |||
152 | ~~~bash |
||
153 | $(echo OigpeyA6fDomIH07Og==|base64 -d) |
||
154 | ~~~ |
||
155 | |||
156 | ### 日本語base64難読化シェル芸 |
||
157 | |||
158 | |||
159 | [たいちょー](https://twitter.com/xztaityozx_001)氏の難読化シェル芸LTスライド |
||
160 | https://www.slideshare.net/xztaityozx/ss-76402430 |
||
161 | |||
162 | {{rawhtml(<iframe src="//www.slideshare.net/slideshow/embed_code/key/pv85nUZVpObynk" width="595" height="485" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" style="border:1px solid #CCC; border-width:1px; margin-bottom:5px; max-width: 100%;" allowfullscreen> </iframe> <div style="margin-bottom:5px"> <strong> <a href="//www.slideshare.net/xztaityozx/ss-76402430" title="難読化シェル芸" target="_blank">難読化シェル芸</a> </strong> from <strong><a target="_blank" href="https://www.slideshare.net/xztaityozx">xztaityozx</a></strong> </div>)}} |
||
163 | |||
164 | > "うんこ"からコマンドを錬成するのが最近のトレンド |
||
165 | |||
166 | うんこでEmacsを起動するのできた |
||
167 | https://twitter.com/sh_takuma/status/869007508643258368 |
||
168 | |||
169 | うんこでVimを起動するのできた |
||
170 | https://twitter.com/grethlen/status/869162016362987520 |
||
171 | |||
172 | https://www.slideshare.net/xztaityozx/base64-77639861 |
||
173 | {{rawhtml(<iframe src="//www.slideshare.net/slideshow/embed_code/key/x1kQutX15DRATr" width="595" height="485" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" style="border:1px solid #CCC; border-width:1px; margin-bottom:5px; max-width: 100%;" allowfullscreen> </iframe> <div style="margin-bottom:5px"> <strong> <a href="//www.slideshare.net/xztaityozx/base64-77639861" title="みんなで!Base64難読化シェル芸" target="_blank">みんなで!Base64難読化シェル芸</a> </strong> from <strong><a target="_blank" href="https://www.slideshare.net/xztaityozx">xztaityozx</a></strong> </div>)}} |
||
174 | |||
175 | ## 破壊的難読化シェル芸 |
||
176 | |||
177 | https://www.slideshare.net/xztaityozx/ss-79171721 |
||
178 | {{rawhtml(<iframe src="//www.slideshare.net/slideshow/embed_code/key/60PR1Ucw9FkZvP" width="595" height="485" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" style="border:1px solid #CCC; border-width:1px; margin-bottom:5px; max-width: 100%;" allowfullscreen> </iframe> <div style="margin-bottom:5px"> <strong> <a href="//www.slideshare.net/xztaityozx/ss-79171721" title="破壊的難読化シェル芸" target="_blank">破壊的難読化シェル芸</a> </strong> from <strong><a href="https://www.slideshare.net/xztaityozx" target="_blank">xztaityozx</a></strong> </div>)}} |
||
179 | |||
180 | ## ダブルシンク難読化シェル芸 |
||
181 | |||
182 | https://www.slideshare.net/xztaityozx/ss-80555814/ |
||
183 | {{rawhtml(<iframe src="//www.slideshare.net/slideshow/embed_code/key/82xYvGKfmOfwPN" width="595" height="485" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" style="border:1px solid #CCC; border-width:1px; margin-bottom:5px; max-width: 100%;" allowfullscreen> </iframe> <div style="margin-bottom:5px"> <strong> <a href="//www.slideshare.net/xztaityozx/ss-80555814" title="ダブルシンク難読化シェル芸" target="_blank">ダブルシンク難読化シェル芸</a> </strong> from <strong><a href="https://www.slideshare.net/xztaityozx" target="_blank">xztaityozx</a></strong> </div>)}} |
||
184 | |||
185 | ### base64からのシーザー暗号 |
||
186 | |||
187 | 古典暗号(ROT13)を使って難読化 |
||
188 | |||
189 | ```bash |
||
190 | echo 44TT44XG44TGPt== |tr A-Za-z N-ZA-Mn-za-m|base64 -d |
||
191 | うんこ |
||
192 | ``` |
||
193 | |||
194 | ROT47で暗号化すると数字も暗号化の対象になって偽装できるのでなお良い |
||
195 | |||
196 | ```bash |
||
197 | echo "ccvvccz%ccv%r8ll"|tr '!-~' 'P-~!-O'|base64 -d |
||
198 | うんこ |
||
199 | ``` |
||
200 | |||
201 | ## スペースを使わない |
||
202 | |||
203 | IFS変数がスペースの代わりとして使える。 |
||
204 | |||
205 | ``` |
||
206 | $ date$IFS'+%s' |
||
207 | 1546743417 |
||
208 | ``` |
||
209 | |||
210 | ## 計算による文字の難読化 |
||
211 | |||
212 | ASCIIコードを計算して出力することによって難読化する |
||
213 | |||
214 | vの出力 |
||
215 | |||
216 | ~~~bash |
||
217 | echo $(printf "%b" $(printf '%s%x' '\x' $((0x15 ^ 0x63)))) |
||
218 | v |
||
219 | ~~~ |
||
220 | |||
221 | Aの出力 |
||
222 | |||
223 | ```bash |
||
224 | printf '%X\n' $((0x5 * 2)) |
||
225 | A |
||
226 | ``` |
||
227 | |||
228 | ## ハッシュ値から必要な文字を得る |
||
229 | |||
230 | MD5等のハッシュ値を出力することで、0123456789abcdefABCDEFの文字を得ることができる。 |
||
231 | |||
232 | Fの出力 |
||
233 | |||
234 | ~~~bash |
||
235 | $(echo -n |md5sum|cut -c10|tr a-z A-Z) |
||
236 | F |
||
237 | ~~~ |
||
238 | |||
239 | ## コマンドで同じ結果になる部分から文字を得る |
||
240 | |||
241 | 例えばpwdコマンドの最初に1文字目は、必ず'/'になる。 |
||
242 | |||
243 | ~~~bash |
||
244 | echo $(pwd|cut -c1) |
||
245 | / |
||
246 | ~~~ |
||
247 | |||
248 | date '+%s' の最初の1文字目は、必ず1になる(と思って差し支えない。UNIX通算秒なので)。 |
||
249 | |||
250 | ~~~bash |
||
251 | echo $(date '+%s'|cut -c1) |
||
252 | 1 |
||
253 | ~~~ |
||
254 | |||
255 | lsのヘルプには、通常以下の文字列が含まれるため、これを利用できる。 |
||
256 | |||
257 | ~~~bash |
||
258 | ls --help|grep ^G |
||
259 | GNU coreutils online help: <http://www.gnu.org/software/coreutils/> |
||
260 | ~~~ |
||
261 | |||
262 | helpの出力 |
||
263 | |||
264 | ~~~bash |
||
265 | echo $(ls --help|grep ^G|cut -c22-25) |
||
266 | help |
||
267 | ~~~ |
||
268 | |||
269 | ## man asciiから文字列を得る |
||
270 | |||
271 | 調査中 |
||
272 | |||
273 | ## システムが普遍的に抱えている不変な情報から文字を得る |
||
274 | |||
275 | **これが難読化に対しては最も有用では** |
||
276 | 実行してみるまで、どんな文字列になるか非常に解りにくい。 |
||
277 | しかし、どのようなLinuxディストリビューションでも同じ値を抱えているものというのは、意外に見つからない。 |
||
278 | |||
279 | 私が発見したものは以下になる。おそらく、この文字列は、どのLinuxディストリビューションでも変わらない。 |
||
280 | |||
281 | ~~~bash |
||
282 | xxd /bin/ls|head -1 |
||
283 | 0000000: 7f45 4c46 0201 0100 0000 0000 0000 0000 .ELF............ |
||
284 | ~~~ |
||
285 | |||
286 | ここから、.:01456cfELFの文字を得ることができる。 |
||
287 | |||
288 | ### サンプリングシェル芸 |
||
289 | |||
290 | 上記(システムが普遍的に抱えている不変な情報から文字を得る)の発展した形をシェル芸の[上田先生](https://ja.wikipedia.org/wiki/%E4%B8%8A%E7%94%B0%E9%9A%86%E4%B8%80)がまとめてくださいました嬉しい |
||
291 | |||
292 | サンプリングシェル芸 |
||
293 | https://b.ueda.tech/?page=sampling_shellgei |
||
294 | |||
295 | >難読化シェル芸の手法の一つに、思わぬところから文字を拾って(サンプリングして)コマンドの文字列にするというものがあります。音楽のサンプリングみたいなものです。文字を拾う方法にもいろいろあるので、まとめてみました。 |
||
296 | |||
297 | ## パイプレス |
||
298 | |||
299 | パイプを使用しないことで難読性を高める |
||
300 | |||
301 | [A. 割りと死ななかった](https://twitter.com/halie_t/status/832613356393488385) |
||
302 | |||
303 | [パイプをキーで入力せずとも、アスキーコードから呼び出してevalで実行すればよいということか…](https://twitter.com/blacknon_/status/832441330785619969) |
||
304 | |||
305 | ~~~bash |
||
306 | $ seq 100 |head -n 8 |tail -n 5 |sed 2,4d |
||
307 | 4 |
||
308 | 8 |
||
309 | $ sed 2,4d <(tail -n 5 <(head -n8 <(seq 10))) |
||
310 | 4 |
||
311 | 8 |
||
312 | $ eval $(echo -e seq 100 "\0174" head -n 8 "\0174" tail -n 5 "\0174" sed 2,4d) |
||
313 | 4 |
||
314 | 8 |
||
315 | ~~~ |
||
316 | |||
317 | ## SJIS文字コードのダメ文字を利用したパイプ難読化 |
||
318 | |||
319 | 文字コードのSJISには、その文字を構成するバイト(2Byte目)に制御文字を含んているものがある |
||
320 | これらはダメ文字と呼ばれ、Linuxで扱う時は注意が必要でした。大きくは、以下の2種類があります |
||
321 | |||
322 | * 2byte目が0x5c \ のダメ文字 |
||
323 | * ― ソ Ы Ⅸ 噂 浬 欺 圭 構 蚕 十 申 曾 箪 貼 能 表 暴 予 禄 兔 喀 媾 彌 拿 杤 歃 濬 畚 秉 綵 臀 藹 觸 軆 鐔 饅 鷭 纊 犾 偆 砡 |
||
324 | * 2byte目が0x7c | のダメ文字 |
||
325 | * - ポ л 榎 掛 弓 芸 鋼 旨 楯 酢 掃 竹 倒 培 怖 翻 慾 處 嘶 斈 忿 掟 桍 毫 烟 痞 窩 縹 艚 蛞 諫 轎 閖 驂 黥 僴 礰 埈 蒴 |
||
326 | |||
327 | SJISのポは"|"が含まれている[ポ系ダメ文字](https://sites.google.com/site/fudist/Home/grep/sjis-damemoji-jp)であり、難読化に応用できる |
||
328 | |||
329 | ```bash |
||
330 | $ eval $(__=$(nkf -sLux <(echo ポ));echo echo ZGF0ZQo=${__:1}base64 -d${__:1}bash) |
||
331 | ``` |
||
332 | |||
333 | ## awk芸によるコマンド機能の代替 |
||
334 | |||
335 | awkで一般的によく使われるコマンド機能を代替して、読みにくくする。 |
||
336 | ただawkに関しては、注意深く読めば読解できる点に注意が必要。 |
||
337 | |||
338 | [AWK 一行野郎百裂拳 - 日本 GNU AWK ユーザー会 - No-ip.org](http://gauc.no-ip.org/awk-users-jp/material/100_one_liners_20131223.pdf) |
||
339 | |||
340 | ## sed芸によるコマンド機能の代替 |
||
341 | |||
342 | sedで一般的によく使われるコマンド機能を代替して、読みにくくする。 |
||
343 | もぅわからん。 |
||
344 | |||
345 | ### tacの代替 |
||
346 | |||
347 | [第27回sedこわいシェル芸勉強会 Q8](https://blog.ueda.asia/?p=9283) |
||
348 | |||
349 | ~~~bash |
||
350 | $ echo abcd|grep -o .|sed '1!G;h;$!d' |
||
351 | d |
||
352 | c |
||
353 | b |
||
354 | a |
||
355 | ~~~ |
||
356 | |||
357 | ### touchの代替 |
||
358 | |||
359 | [sed 'w a' aa と実行すれば...](https://twitter.com/ryuchi/status/830307834520760322) |
||
360 | |||
361 | ~~~bash |
||
362 | $ sed 'w filename' /dev/null |
||
363 | ~~~ |
||
364 | |||
365 | ### treeの代替 |
||
366 | |||
367 | [Qiita - tree コマンドが無い環境で tree コマンドを実現](http://qiita.com/yone098@github/items/bba8a42de6b06e40983b) |
||
368 | |||
369 | ~~~bash |
||
370 | $ find . | sort | sed '1d;s/^\.//;s/\/\([^/]*\)$/|--\1/;s/\/[^/|]*/| /g' |
||
371 | |--a |
||
372 | | |--b |
||
373 | | | |--c |
||
374 | | | | |--d |
||
375 | ~~~ |
||
376 | |||
377 | ## Vimシェル芸によるコマンド機能の代替 |
||
378 | |||
379 | 深淵 |
||
380 | |||
381 | ## その他、普通のコマンドの使い方をしない |
||
382 | |||
383 | ### echoの代替 |
||
384 | |||
385 | [Hello Worldコレクション](http://news.mynavi.jp/column/helloworld/002/) |
||
386 | |||
387 | ~~~bash |
||
388 | $ basename 'Hello World' |
||
389 | Hello World |
||
390 | $ date +'Hello World' |
||
391 | Hello World |
||
392 | $ expr 'Hello World' |
||
393 | Hello World |
||
394 | ~~~ |
||
395 | |||
396 | ### catの代替 |
||
397 | |||
398 | ~~~bash |
||
399 | $ echo "$(<hoge.txt)" |
||
400 | ~~~ |
||
401 | |||
402 | ```bash |
||
403 | $ curl file:/%65%74%63/%70%61%73%73%77%64 # cat /etc/passwd と同じ |
||
404 | ``` |
||
405 | |||
406 | >curlは、更に[URLエンコード](https://ja.wikipedia.org/wiki/%E3%83%91%E3%83%BC%E3%82%BB%E3%83%B3%E3%83%88%E3%82%A8%E3%83%B3%E3%82%B3%E3%83%BC%E3%83%87%E3%82%A3%E3%83%B3%E3%82%B0)により難読化できる |
||
407 | |||
408 | ### lsの代替 |
||
409 | |||
410 | ~~~bash |
||
411 | echo * |
||
412 | printf %s * |
||
413 | ~~~ |
||
414 | |||
415 | ### tacの代替 |
||
416 | |||
417 | ```bash |
||
418 | seq 10 | dc -f- -ef |
||
419 | ``` |
||
420 | |||
421 | >dcコマンドを読める人は少ない |
||
422 | |||
423 | # 記号難読化 |
||
424 | |||
425 | 以下、参照 |
||
426 | |||
427 | news#114 |
||
428 | |||
429 | >ここから[隊長](https://twitter.com/xztaityozx_001)さんが究極の難読化に昇華してくれた。いまやJavaScriptやPowerShellの最先端の難読化手法に追いついている。以下 |
||
430 | |||
431 | ## 超・記号オンリー難読化シェル芸 |
||
432 | |||
433 | {{rawhtml(<iframe src="//www.slideshare.net/slideshow/embed_code/key/c2ddOGlZep3b8F" width="595" height="485" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" style="border:1px solid #CCC; border-width:1px; margin-bottom:5px; max-width: 100%;" allowfullscreen> </iframe> <div style="margin-bottom:5px"> <strong> <a href="//www.slideshare.net/xztaityozx/ss-93177781" title="超・記号オンリー難読化シェル芸" target="_blank">超・記号オンリー難読化シェル芸</a> </strong> from <strong><a href="https://www.slideshare.net/xztaityozx" target="_blank">xztaityozx</a></strong> </div>)}} |
||
434 | |||
435 | > これにより難読化シェル芸の、難読化手法の一つが完成するに至った。歴史的瞬間。 |
||
436 | |||
437 | ## 数字・英字を全く使わない記号難読化 |
||
438 | |||
439 | > 不可能と思われていた完全記号化がなんと完成…!! |
||
440 | |||
441 | 記号と英字2文字だけでbash - Ryoto Saito's Blog |
||
442 | https://www.ryotosaito.com/blog/?p=178 |
||
443 | |||
444 | 記号だけでシェルは操れた - Ryoto Saito's Blog |
||
445 | https://www.ryotosaito.com/blog/?p=194 |
||
446 | |||
447 | ## 全く別のアプローチによる完全記号難読化 |
||
448 | |||
449 | > 新たなる刺客 |
||
450 | |||
451 | 34C3 CTF: minbashmaxfun - writeup |
||
452 | https://hack.more.systems/writeup/2017/12/30/34c3ctf-minbashmaxfun/ |
||
453 | |||
454 | その驚くべき手法 |
||
455 | |||
456 | ``` |
||
457 | $ ${!#}<<<${!#}\<\<\<\$\'\\${##}$#${##}\' |
||
458 | ``` |
||
459 | |||
460 | これは、以下と等価 |
||
461 | |||
462 | ``` |
||
463 | $ bash <<< bash \<\<\< \$\'\\${##}$#${##}\' |
||
464 | ``` |
||
465 | |||
466 | そして、以下とも等価 |
||
467 | |||
468 | ``` |
||
469 | $ bash <<< $'\101' |
||
470 | ``` |
||
471 | |||
472 | これは最終的にAを実行することと等価 |
||
473 | |||
474 | ``` |
||
475 | $ A |
||
476 | -bash: A: コマンドが見つかりません |
||
477 | ``` |
||
478 | |||
479 | この考えに基づけば、全ての数字・アルファベットを生成することができる |
||
480 | |||
481 | ポイントは ${!#} と ${##} の特殊変数 |
||
482 | |||
483 | 以下の内容となっている |
||
484 | |||
485 | ``` |
||
486 | $ echo ${!#} # 34C3 CTFの環境では"bash"が返ってきたようです。若干の環境依存があります。 |
||
487 | -bash |
||
488 | $ echo ${##} |
||
489 | 1 |
||
490 | ``` |
||
491 | |||
492 | これは、以下のように解釈できます。 |
||
493 | |||
494 | * ${#変数名}は、格納されている文字列の長さを返す。 |
||
495 | * ${#} は引数の数(ワンライナーだと0になります) |
||
496 | * よって、${##}は、"0"という文字の長さ1を返す。 |
||
497 | * ${!} は最後にバックグラウンドで行ったコマンドのプロセス番号(バックグラウンドで実行していなければ空) |
||
498 | * よって ${!#} は ${0} と等価となる。 |
||
499 | |||
500 | 驚くべき手法です。これまでに考えてきた手法と組み合わせることもできます。 |
||
501 | この点はまだまだ研究の余地がありそうです! |
||
502 | |||
503 | ## 難読化シェル芸に使えそうなテクニック集 |
||
504 | |||
505 | 難読化シェル芸に使えそうなテクニック集 |
||
506 | https://scrapbox.io/jiro4989/%E9%9B%A3%E8%AA%AD%E5%8C%96%E3%82%B7%E3%82%A7%E3%83%AB%E8%8A%B8%E3%81%AB%E4%BD%BF%E3%81%88%E3%81%9D%E3%81%86%E3%81%AA%E3%83%86%E3%82%AF%E3%83%8B%E3%83%83%E3%82%AF%E9%9B%86 |
||
507 | |||
508 | >神 of 神 |
||
509 | |||
510 | hiro氏作の難読化シェル芸の解読 |
||
511 | https://hackmd.io/@EjC746Q1REWEpEC57LOAXA/rJ4niQJfr |
||
512 | |||
513 | たいちょーの雑記 - 難読化dateコレクション-3-$[]利用 |
||
514 | https://xztaityozx.hatenablog.com/entry/2020/01/16/224643 |
||
515 | |||
516 | 難読化シェル芸の解読 |
||
517 | https://qiita.com/eggplants/items/3994a502ce2659b4a17f |
||
518 | |||
519 | |||
520 | |||
521 | |||
522 | # Unicode結合文字難読化 |
||
523 | |||
524 | ```bash |
||
525 | echo "d͜͜͏̘̣͔͙͎͎̘̜̫̗͍͚͓͜͜͏̘̣͔͙͎͎a͜͜͏̘̣͔͙͎͎t̜ͪ̅̍̅͂͊e " |tr -cd 'a-z'|bash |
||
526 | 2018年 3月 18日 日曜日 17:06:41 JST |
||
527 | ``` |
||
528 | |||
529 | {{rawhtml(<iframe src="//www.slideshare.net/slideshow/embed_code/key/Ce6NmAUBoWMlAd" width="595" height="485" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" style="border:1px solid #CCC; border-width:1px; margin-bottom:5px; max-width: 100%;" allowfullscreen> </iframe> <div style="margin-bottom:5px"> <strong> <a href="//www.slideshare.net/kanata1/unicode-112630484" title="Unicode結合文字 難読化シェル芸" target="_blank">Unicode結合文字 難読化シェル芸</a> </strong> from <strong><a href="https://www.slideshare.net/kanata1" target="_blank">kanata -</a></strong> </div>)}} |
||
530 | |||
531 | # Unicodeゼロ幅スペース難読化 |
||
532 | |||
533 | {{rawhtml(<iframe src="//www.slideshare.net/slideshow/embed_code/key/xsLdg6etgkh9K6" width="595" height="485" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" style="border:1px solid #CCC; border-width:1px; margin-bottom:5px; max-width: 100%;" allowfullscreen> </iframe> <div style="margin-bottom:5px"> <strong> <a href="//www.slideshare.net/kanata1/unicode-104771309" title="Unicodeゼロ幅文字 難読化シェル芸" target="_blank">Unicodeゼロ幅文字 難読化シェル芸</a> </strong> from <strong><a href="https://www.slideshare.net/kanata1" target="_blank">kanata -</a></strong> </div>)}} |
||
534 | |||
535 | # アンチデバック技法(耐タンパー性の確保) |
||
536 | |||
537 | 思いついたんだけど、公開していいか、悩んでいる。 |
||
538 | |||
539 | |||
540 | |||
541 | |||
542 | |||
543 | |||
544 | |||
545 | # 難読化シェル芸ツール |
||
546 | |||
547 | 手動により難読化しても良いが、ツールを作ってみた。 |
||
548 | |||
549 | ## インストール |
||
550 | |||
551 | attachment:NandokukaShellGei.sh |
||
552 | |||
553 | シェルスクリプトなのでダウンロード&実行権限付与でOK |
||
554 | |||
555 | ~~~ |
||
556 | wget https://raintrees.net/attachments/download/391/NandokukaShellGei.sh |
||
557 | chmod u+x NandokukaShellGei.sh |
||
558 | ~~~ |
||
559 | |||
560 | ### 動作環境 |
||
561 | |||
562 | 一般的なLinuxディストリビューションで、だいたい動くと思う。 |
||
563 | 以下で動くことを確認した。 |
||
564 | |||
565 | * CentOS7 |
||
566 | * KaliLinux |
||
567 | |||
568 | >ただコマンドの組み合わせによっては、なんか不具合がおきたりする。原因は調べてない。 |
||
569 | |||
570 | Macで動くかは、ちょっと自信ない。 |
||
571 | |||
572 | ## 実行例 |
||
573 | |||
574 | ~~~bash |
||
575 | $ ./20161216_NandokukaShellGei.sh "echo HelloWorld" |
||
576 | e ls --help|grep ^G|cut -c8 |
||
577 | c ls --help|grep ^G|cut -c5 |
||
578 | h ls --help|grep ^G|cut -c22 |
||
579 | o ls --help|grep ^G|cut -c6 |
||
580 | xxd /bin/ls|head -1|cut -c9 |
||
581 | H ls --help|grep ^G|cut -c22|tr a-z A-Z |
||
582 | e ls --help|grep ^G|cut -c8 |
||
583 | l ls --help|grep ^G|cut -c12 |
||
584 | l ls --help|grep ^G|cut -c12 |
||
585 | o ls --help|grep ^G|cut -c6 |
||
586 | W ls --help|grep ^G|cut -c36|tr a-z A-Z |
||
587 | o ls --help|grep ^G|cut -c6 |
||
588 | r ls --help|grep ^G|cut -c7 |
||
589 | l ls --help|grep ^G|cut -c12 |
||
590 | d echo -n|md5sum|cut -c1 |
||
591 | |||
592 | $(ls --help|grep ^G|cut -c8)$(ls --help|grep ^G|cut -c5)$(ls --help|grep ^G|cut -c22)$(ls --help|grep ^G|cut -c6)$(xxd /bin/ls|head -1|cut -c9)$(ls --help|grep ^G|cut -c22|tr a-z A-Z)$(ls --help|grep ^G|cut -c8)$(ls --help|grep ^G|cut -c12)$(ls --help|grep ^G|cut -c12)$(ls --help|grep ^G|cut -c6)$(ls --help|grep ^G|cut -c36|tr a-z A-Z)$(ls --help|grep ^G|cut -c6)$(ls --help|grep ^G|cut -c7)$(ls --help|grep ^G|cut -c12)$(echo -n|md5sum|cut -c1) |
||
593 | $ |
||
594 | $ #以下、出力されたコードをコピペ |
||
595 | $ $(ls --help|grep ^G|cut -c8)$(ls --help|grep ^G|cut -c5)$(ls --help|grep ^G|cut -c22)$(ls --help|grep ^G|cut -c6)$(xxd /bin/ls|head -1|cut -c9)$(ls --help|grep ^G|cut -c22|tr a-z A-Z)$(ls --help|grep ^G|cut -c8)$(ls --help|grep ^G|cut -c12)$(ls --help|grep ^G|cut -c12)$(ls --help|grep ^G|cut -c6)$(ls --help|grep ^G|cut -c36|tr a-z A-Z)$(ls --help|grep ^G|cut -c6)$(ls --help|grep ^G|cut -c7)$(ls --help|grep ^G|cut -c12)$(echo -n|md5sum|cut -c1) |
||
596 | HelloWorld |
||
597 | ~~~ |
||
598 | {{rawhtml(<span style="display:none" >HTJSA{DGFDIHJKH}ASQQMXBKUXXSdfgeOGCYTSAADSGUFTu}IUYTPDGHSGJUOUO}ASDGFSGJKHLQZSN}</span>)}} |
||
599 | |||
600 | # もっとすげぇ難読化シェル芸ツール |
||
601 | |||
602 | [隊長](https://twitter.com/xztaityozx_001)さん作成のツール |
||
603 | |||
604 | xztaityozx/nandokuka 難読化シェル芸は砕けない |
||
605 | https://github.com/xztaityozx/nandokuka |
||
606 | |||
607 | [0x6d61](https://twitter.com/0x6d61)さん作成のツール |
||
608 | |||
609 | 0x6d61/obfsh.rb 難読化shell芸を手助けするscript |
||
610 | https://gist.github.com/0x6d61/7ebfe4393ba3a3564313b48684e7b7fb |
||
611 | |||
612 | node-bash-obfuscate |
||
613 | https://www.npmjs.com/package/bash-obfuscate |
||
614 | |||
615 | # 難読化解除ツール |
||
616 | |||
617 | Flerken |
||
618 | https://github.com/We5ter/Flerken/blob/master/README.md |
||
619 | |||
620 | >Windows (CMD and Powershell) and Linux (Bash) commandsが対象。WebUIもある。 |
||
621 | |||
622 | # Memo |
||
623 | |||
624 | PowerShellとJavaScriptは別ページにした方がいいかもなー。情報量が多い。 |
||
625 | |||
626 | PORTSWIGGER - Unearthing Z͌̈́̾a͊̈́l͊̿g̏̉͆o̾̚̚S̝̬ͅc̬r̯̼͇ͅi̼͖̜̭͔p̲̘̘̹͖t̠͖̟̹͓͇ͅ with visual fuzzing |
||
627 | https://portswigger.net/blog/unearthing-zalgoscript-with-visual-fuzzing |
||
628 | |||
629 | IPアドレスの難読化 - おふとん |
||
630 | https://ohuton.hatenadiary.jp/entry/2018/09/24/232419 |
||
631 | |||
632 | 自己追記によるFizzBuzz |
||
633 | https://zenn.dev/yamaya/articles/da9131540dc12a |
||
634 | |||
635 | ## JavaScriptの難読化 |
||
636 | |||
637 | Qiita - JS記号プログラミング入門 |
||
638 | https://qiita.com/acid_chicken/items/eeb0b42a1ecbba0c49e3 |
||
639 | |||
640 | Qiita - ウェブアプリをソースごとパクる業者に対する対策 |
||
641 | https://qiita.com/kacchan6@github/items/d8576ab6b3c16cf670ca |
||
642 | |||
643 | Qiita - 本格JavaScript記号プログラミング(1) 6種類の記号だけでJavaScriptを書こう |
||
644 | https://qiita.com/Tatamo/items/24099958b90cbed61d67 |
||
645 | |||
646 | JavaScriptの難読化技術を探る |
||
647 | https://monpoke1.hatenablog.com/entry/2018/12/06/020115#参考 |
||
648 | |||
649 | プログラミングLT 記号プログラミングのスライド |
||
650 | https://docs.google.com/presentation/d/1Bqu_si3o0Lol8k9MvVx443R9zaJF7Qtq-_jutw2wYrc/edit?usp=sharing |
||
651 | |||
652 | ## PowerShellの難読化 |
||
653 | |||
654 | セキュリティコンサルタントの日誌から - PowerShellの難読化について |
||
655 | http://www.scientia-security.org/entry/2017/11/13/224035 |
||
656 | |||
657 | PowerShell難読化の基礎 (1) |
||
658 | http://binary-pulsar.azurewebsites.net/2018/09/01/ps-obfuscate-pt1/ |
||
659 | |||
660 | PowerShell難読化の基礎 (2) |
||
661 | http://binary-pulsar.azurewebsites.net/2018/09/08/ps-obfuscate-pt2/ |
||
662 | |||
663 | PowerShell難読化の基礎 (3) |
||
664 | http://binary-pulsar.azurewebsites.net/2018/09/14/ps-obfuscate-pt3/ |
||
665 | |||
666 | PowerShellの難読化解除 - Binary Pulsar |
||
667 | http://binary-pulsar.azurewebsites.net/2018/09/19/ps-deobfuscate/ |
||
668 | |||
669 | DbgShell - A PowerShell Front-End For The Windows Debugger Engine |
||
670 | https://www.kitploit.com/2018/10/dbgshell-powershell-front-end-for.html |
||
671 | |||
672 | PowerShellの難読化解除 |
||
673 | https://binary-pulsar.hatenablog.jp/entry/2018/09/19/000000 |
||
674 | |||
675 | 難読化PowerShell芸入門 |
||
676 | https://www.slideshare.net/xztaityozx/powershell-162881333 |
||
677 | |||
678 | 『PowerShellを使用したファイルレス・マルウェアが届いたので、コミケの原稿書きながら、難読化スクリプトの読解手順を動画にしてみました。』 |
||
679 | |||
680 | 一進一退!じあんちゃん じあんちゃん対PowerShellの巻 (ギャル語でマルウェア解説してみた) |
||
681 | https://www.youtube.com/watch?v=Z6j_bz1Lo8U |
||
682 | |||
683 | >『PowerShellを使用したファイルレス・マルウェアが届いたので、コミケの原稿書きながら、難読化スクリプトの読解手順を動画にしてみました。』 |
||
684 | |||
685 | Powershellスクリプトの難読化解除ツール |
||
686 | https://github.com/R3MRUM/PSDecode |
||
687 | |||
688 | |||
689 | |||
690 | |||
691 | |||
692 | ## その他の言語 |
||
693 | |||
694 | Ruby とすてきな難読化 |
||
695 | http://d.hatena.ne.jp/ku-ma-me/touch/20091215/p1 |
||
696 | |||
697 | sh で brainfuck |
||
698 | http://ya.maya.st/d/201208a.html#s20120809_1 |
||
699 | |||
700 | 難読化の話(超!?入門編)その1 |
||
701 | https://www.netagent.co.jp/study/blog/hard/20180726.html |
||
702 | |||
703 | 難読化の話(超!?入門編)その2 |
||
704 | https://www.netagent.co.jp/study/blog/hard/20181122.html |
||
705 | |||
706 | 難読化の話(超!?入門編)その3 前編 |
||
707 | https://www.netagent.co.jp/study/blog/hard/20190214.html |
||
708 | |||
709 | えあーの雑記録(仮) - 【C/C++】#defineで日本語も使えるので「#define 斉藤」してみた |
||
710 | https://airscarlet.com/programming_cpp_japanese_define/ |