Bye Bye Moore

PoCソルジャーな零細事業主が作業メモを残すブログ

shcコマンドでシェルスクリプト を難読化(というかバイナリ化)する

まあ、ガチの人に狙われたら一たまりもないので……気休めかもしれません。

実際のところ

myscript.shを加工したい場合、以下のコマンドで"myscript.sh.x"と"myscript.sh.x.c"というのができます。
構造としては、シェルスクリプトC言語に変換し、そいつをビルドすることでバイナリ化しているようです。

$ shc -f myscript.sh

同一処理系(DebianとかmacOSといった単位の)の別マシンで動かす場合、"-r"をつけると良いそうです。

-r Relax security. Make a redistributable binary which
executes on different systems running the same operat-
ing system.

$ shc -r -f myscript

備考

書かれている平文データなどはARC4暗号にて混ぜられているようです。
ja.wikipedia.org

抽出すると、こんな感じ。なんか割とシンプルですね……。

/*
 * Crypt data. 
 */
void arc4(void * str, int len)
{
	unsigned char tmp, * ptr = (unsigned char *)str;
	while (len > 0) {
		indx++;
		tmp = stte[indx];
		jndx += tmp;
		stte[indx] = stte[jndx];
		stte[jndx] = tmp;
		tmp += stte[indx];
		*ptr ^= stte[tmp];
		ptr++;
		len--;
	}
}

/* End of ARC4 */