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