Skip to content

Commit 4eafd93

Browse files
committed
support generating random string with -n flag
1 parent 50d381b commit 4eafd93

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ Usage: ./ssc [-u] [-s] [-r] [-e|-E|-M file] [-0] [-n name] [-d date] [-m msg] [-
9999
linux only, works like AppImage. if a directory is specified, create squashfs from it
100100
-0, --fix-argv0 try to fix $0, may not work
101101
if it doesn't work or causes problems, try -n flag or use $SSC_ARGV0 instead
102-
-n, --ps-name change script path in ps output
102+
-n, --ps-name change script path in ps output, may contain 'XXXXXX' which will be replaced with a random string
103103
upon execution, create a symlink to real path and pass it to the interperter
104104
-d, --expire-date expire date, for example, 11/30/2023
105105
-m, --expire-message expire message, default to 'script has expired!'

README_zh_CN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ ssc本身并不是一个编译器,比如cc,它会生成包含脚本代码的
9797
仅适用于Linux,类似AppImage。如果指定的是目录,从这个目录创建squashfs文件
9898
-0, --fix-argv0 尝试修复$0,可能不起作用
9999
如果不起作用或造成问题,请尝试使用-n选项或使用$SSC_ARGV0代替$0
100-
-n, --ps-name 更改ps输出中的脚本路径
100+
-n, --ps-name 更改ps输出中的脚本路径,可包含XXXXXX,运行时替换为随机字符串
101101
在执行时,创建一个指向真实路径的符号链接并将这个链接传递给解释器
102102
-d, --expire-date 过期日期,例如 11/30/2023
103103
-m, --expire-message 过期提示,默认为 script has expired!

src/main.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <string>
1414
#include <iterator>
1515
#include <algorithm>
16+
#include <random>
1617
#include "obfuscate.h"
1718
#include "utils.h"
1819
#include "embed.h"
@@ -266,14 +267,24 @@ int main(int argc, char* argv[]) {
266267
#endif
267268

268269
#ifdef PS_NAME
269-
if (is_symlink(OBF(STR(PS_NAME)))) {
270-
unlink(OBF(STR(PS_NAME)));
270+
std::string link_name(OBF(STR(PS_NAME)));
271+
pos = link_name.find("XXXXXX");
272+
if (pos != std::string::npos) {
273+
const char *chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
274+
std::mt19937 gen(std::random_device{}());
275+
std::uniform_int_distribution<> dist(0, strlen(chars) - 1);
276+
for (; pos < link_name.size() && link_name[pos] == 'X'; pos++) {
277+
link_name[pos] = chars[dist(gen)];
278+
}
279+
}
280+
if (is_symlink(link_name.c_str())) {
281+
unlink(link_name.c_str());
271282
}
272-
if (symlink(path.c_str(), OBF(STR(PS_NAME))) != 0) {
273-
LOGE("failed to create symlink! path=" STR(PS_NAME) " err=`%s`", strerror(errno));
283+
if (symlink(path.c_str(), link_name.c_str()) != 0) {
284+
LOGE("failed to create symlink! path=%s err=`%s`", link_name.c_str(), strerror(errno));
274285
return 5;
275286
}
276-
path = OBF(STR(PS_NAME));
287+
path = std::move(link_name);
277288
cleaner.add(path);
278289
#endif
279290

0 commit comments

Comments
 (0)