-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathronin-install.sh
executable file
·560 lines (507 loc) · 11.3 KB
/
ronin-install.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
#!/usr/bin/env bash
ronin_install_version="0.1.0"
gem="gem"
gem_opts=(--no-format-executable)
#
# Prints a log message.
#
function log()
{
if [[ -t 1 ]]; then
echo -e "\x1b[1m\x1b[32m>>>\x1b[0m \x1b[1m$1\x1b[0m"
else
echo ">>> $1"
fi
}
#
# Prints a warn message.
#
function warn()
{
if [[ -t 1 ]]; then
echo -e "\x1b[1m\x1b[33m***\x1b[0m \x1b[1m$1\x1b[0m" >&2
else
echo "*** $1" >&2
fi
}
#
# Prints an error message.
#
function error()
{
if [[ -t 1 ]]; then
echo -e "\x1b[1m\x1b[31m!!!\x1b[0m \x1b[1m$1\x1b[0m" >&2
else
echo "!!! $1" >&2
fi
}
#
# Prints an error message and exits with -1.
#
function fail()
{
error "$@"
exit -1
}
#
# Checks that $LANG is set correctly.
#
function check_lang()
{
if [[ "$LANG" == "C" ]]; then
error "ruby will not work properly with LANG=C"
fail "Please set LANG to en_US.UTF-8 or another UTF-8 language"
fi
}
#
# Sets os_platform and os_arch.
#
function detect_os()
{
os_platform="$(uname -s)"
os_arch="$(uname -m)"
}
#
# Don't use sudo if already root.
#
function detect_sudo()
{
if (( UID == 0 )); then sudo=""
else sudo="sudo"
fi
}
#
# Auto-detect the package manager.
#
function detect_package_manager()
{
case "$os_platform" in
Linux)
if [[ -f /etc/redhat-release ]]; then
if command -v dnf >/dev/null; then
package_manager="dnf"
elif command -v yum >/dev/null; then
package_manager="yum"
fi
elif [[ -f /etc/debian_version ]]; then
if command -v apt-get >/dev/null; then
package_manager="apt"
fi
elif [[ -f /etc/SuSE-release ]]; then
if command -v zypper >/dev/null; then
package_manager="zypper"
fi
elif [[ -f /etc/arch-release ]]; then
if command -v pacman >/dev/null; then
package_manager="pacman"
fi
elif [[ -f /etc/os-release ]]; then
if command -v pacman >/dev/null; then
package_manager="pacman"
elif command -v zypper >/dev/null; then
package_manager="zypper"
fi
elif [[ "$HOME" == *"com.termux"* ]]; then
package_manager="termux"
fi
;;
Darwin)
if command -v brew >/dev/null; then
package_manager="brew"
elif command -v port >/dev/null; then
package_manager="port"
fi
;;
*BSD)
if command -v pkg >/dev/null; then
package_manager="pkg"
fi
;;
esac
}
#
# Detect the ruby version.
#
function detect_ruby_version()
{
if command -v ruby >/dev/null; then
ruby_version="$(ruby -e 'print RUBY_VERSION')"
fi
}
#
# Detect the system.
#
function detect_system()
{
check_lang
detect_os
detect_sudo
if [[ -z "$package_manager" ]]; then
detect_package_manager
fi
detect_ruby_version
}
#
# Detect where rubygems installs gems into and whether it's writable.
#
function detect_rubygems_install_dir()
{
local gem_dir="$(gem env gemdir)"
if (( UID == 0 )); then
gem_opts+=(--no-user-install)
elif [[ -d "$gem_dir" ]] && [[ ! -w "$gem_dir" ]]; then
gem="sudo $gem"
gem_opts+=(--no-user-install)
fi
}
#
# Installs a list of package names using the detected package manager.
#
function install_packages()
{
case "$package_manager" in
apt) $sudo apt-get install -y "$@" || return $? ;;
dnf|yum)$sudo $package_manager install -y "$@" || return $? ;;
port) $sudo port install "$@" || return $? ;;
pkg) $sudo pkg install -y "$@" || return $? ;;
brew)
local brew_owner="$(/usr/bin/stat -f %Su "$(command -v brew)")"
sudo -u "$brew_owner" brew install "$@" ||
sudo -u "$brew_owner" brew upgrade "$@" || return $?
;;
pacman)
local missing_pkgs=($(pacman -T "$@"))
if (( ${#missing_pkgs[@]} > 0 )); then
$sudo pacman -Sy --noconfirm "${missing_pkgs[@]}" || return $?
fi
;;
zypper) $sudo zypper -n in -l $* || return $? ;;
termux) pkg install -y "$@" || return $? ;;
"") warn "Could not determine Package Manager. Proceeding anyway." ;;
esac
}
#
# Installs ruby via homebrew and configures it.
#
function homebrew_install_ruby()
{
install_packages ruby
brew pin ruby
# make the homebrew ruby the default ruby for the script
PATH="$(brew --prefix ruby)/bin:$PATH"
hash -r
# make the homebrew ruby the default ruby for zshrc
cat >> ~/.zshrc <<CONFIG
PATH="\$(brew --prefix ruby)/bin:\$PATH"
PATH="\$(gem env gemdir)/bin:\$PATH"
CONFIG
}
#
# Installs ruby 3, if it's not installed.
#
function auto_install_ruby()
{
# check if ruby-3.x is already installed
if [[ ! "$ruby_version" == "3."* ]]; then
log "Installing ruby 3.x ..."
case "$package_manager" in
brew) homebrew_install_ruby ;;
dnf|yum) install_packages ruby-devel ruby-bundled-gems ;;
zypper) install_packages ruby-devel ;;
apt) install_packages ruby-full ;;
*) install_packages ruby ;;
esac || fail "Failed to install ruby!"
fi
auto_install_rubygems
}
#
# Install rubygems if it's missing.
#
function auto_install_rubygems()
{
if ! command -v gem >/dev/null; then
log "Installing rubygems ..."
case "$package_manager" in
dnf|yum|pacman) install_packages rubygems ;;
pkg) install_packages devel/ruby-gems ;;
*)
fail "rubygems was not installed along with ruby. Aborting!"
;;
esac
fi
detect_rubygems_install_dir
}
#
# Installs binutils for ld and ar commands, if they aren't already installed.
#
function auto_install_binutils()
{
if ! command -v ld >/dev/null || ! command -v ar >/dev/null; then
log "Installing binutils ..."
install_packages binutils || \
fail "Failed to install binutils!"
fi
}
#
# Install gcc or clang if there's no C compiler on the system.
#
function auto_install_cc()
{
if ! command -v cc >/dev/null; then
case "$package_manager" in
termux)
log "Installing clang ..."
install_packages clang || \
fail "Failed to install clang!"
;;
*)
log "Installing gcc ..."
install_packages gcc || \
fail "Failed to install gcc!"
;;
esac
fi
}
#
# Install g++ or clang if there's no C++ compiler on the system.
#
function auto_install_cpp()
{
if ! command -v c++ >/dev/null; then
case "$package_manager" in
dnf|yum)
log "Installing g++ ..."
install_packages gcc-g++ || \
fail "Failed to install g++!"
;;
zypper)
log "Installing g++ ..."
install_packages gcc-c++ || \
fail "Failed to install g++!"
;;
termux)
log "Installing clang ..."
install_packages clang || \
fail "Failed to install clang!"
;;
*)
log "Installing g++ ..."
install_packages g++ || \
fail "Failed to install g++!"
;;
esac
fi
}
#
# Install make if it's not already installed.
#
function auto_install_make()
{
if ! command -v make >/dev/null; then
log "Installing make ..."
install_packages make || fail "Failed to install make!"
fi
}
#
# Install pkg-config if it's not already installed.
#
function auto_install_pkg_config()
{
if ! command -v pkg-config >/dev/null; then
# NOTE: BSDs needs pkg-config to compile the sqlite3 gem
case "$package_manager" in
pkg)
log "Installing pkg-config ..."
install_packages devel/pkgconf
;;
esac || fail "Failed to install pkg-config!"
fi
}
#
# Explicitly install nokogiri on Termux by building it against the system's
# libxml2 and libxslt packages.
#
function termux_install_nokogiri()
{
# XXX: compile nokogiri against the system's libxml2 library,
# to workaround issue with the libxml2 tar archive containing
# hardlinks.
log "Installing nokogiri ..."
$gem install nokogiri --platform ruby -- --use-system-libraries || \
warn "Failed to compile nokogiri. Proceeding anyways."
}
#
# Install libraries and headers for other ronin's dependencies.
#
function install_libraries()
{
case "$package_manager" in
dnf|yum)libraries=(libyaml-devel) ;;
termux) libraries=(libxml2 libxslt) ;;
esac
log "Installing libraries ..."
install_packages "${libraries[@]}" || \
warn "Failed to install external dependencies. Proceeding anyways."
if [[ "$package_manager" == "termux" ]] && \
[[ -z "$(gem which nokogiri 2>/dev/null)" ]]; then
termux_install_nokogiri
fi
}
#
# Install external dependencies for ronin.
#
function install_build_dependencies()
{
auto_install_binutils
auto_install_cc
auto_install_cpp
auto_install_make
auto_install_pkg_config
install_libraries
}
#
# Installs git, if it's not installed.
#
function auto_install_git()
{
if ! command -v git >/dev/null; then
log "Installing bundler ..."
install_packages git || \
warn "Failed to install git. Proceeding anyways ..."
fi
}
#
# Installs zip, if it's not installed.
#
function auto_install_zip()
{
if ! command -v zip >/dev/null; then
log "Installing bundler ..."
install_packages zip || \
warn "Failed to install zip. Proceeding anyways ..."
fi
}
#
# Install nmap if it's not already installed.
#
function auto_install_nmap()
{
if ! command -v nmap >/dev/null; then
log "Installing nmap ..."
install_packages nmap || \
warn "Failed to install nmap. Proceeding anyways ..."
fi
}
#
# Install masscan if it's not already installed.
#
function auto_install_masscan()
{
if ! command -v masscan >/dev/null; then
log "Installing masscan ..."
install_packages masscan || \
warn "Failed to install masscan. Proceeding anyways ..."
fi
}
#
# Install GraphViz if it's not already installed.
#
function auto_install_graphviz()
{
if ! command -v dot >/dev/null; then
log "Installing GraphViz ..."
install_packages graphviz || \
warn "Failed to install GraphViz. Proceeding anyways ..."
fi
}
#
# Install runtime dependencies for ronin.
#
function install_runtime_dependencies()
{
auto_install_git
auto_install_zip
auto_install_nmap
auto_install_masscan
auto_install_graphviz
}
#
# Print the --help usage.
#
function print_usage()
{
cat <<USAGE
usage: ./ronin-install.sh [OPTIONS]
Options:
--package-manager [apt|dnf|yum|pacman|zypper|brew|pkg|port]
Sets the package manager to use
--pre Enables installing pre-release versions
-V, --version Prints the version
-h, --help Prints this message
USAGE
}
#
# Parse additional command-line options.
#
function parse_options()
{
while [[ $# -gt 0 ]]; do
case "$1" in
--pre)
prerelease="true"
gem_opts+=(--prerelease)
shift
;;
--package-manager)
package_manager="$2"
shift 2
;;
-V|--version)
echo "ronin-install: $ronin_install_version"
exit
;;
-h|--help)
print_usage
exit
;;
-*)
echo "ronin-install: unrecognized option $1" >&2
return 1
;;
*)
echo "ronin-install: additional arguments not allowed" >&2
return 1
;;
esac
done
}
parse_options "$@" || exit $?
detect_system
auto_install_ruby
install_build_dependencies
install_runtime_dependencies
if ! command -v ronin >/dev/null; then
if [[ "$prerelease" == "true" ]]; then
log "Installing ronin pre-release. This may take a while ..."
else
log "Installing ronin. This may take a while ..."
fi
$gem install ${gem_opts[@]} ronin
log "ronin is now fully installed!"
else
if [[ "$prerelease" == "true" ]]; then
log "Updating ronin to the latest pre-release. This may take a while ..."
else
log "Updating ronin to the latest version. This may take a while ..."
fi
$gem update ${gem_opts[@]} ronin
log "ronin has successfully been upgraded!"
fi
if [[ ! "$ruby_version" == "3."* ]] && [[ "$package_manager" == "brew" ]]; then
warn "Ruby ${ruby_version} was installed via Homebrew."
warn "You will need to restart your shell or open a new terminal."
fi
log "Congratulations! You can now run ronin:"
echo
echo " $ ronin help"
echo