forked from ronin-rb/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathronin-dev.sh
executable file
·476 lines (427 loc) · 9.3 KB
/
ronin-dev.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
#!/usr/bin/env bash
gem="gem"
gem_opts=(--no-format-executable)
bundler_version="~> 2.0"
github_org="ronin-rb"
github_base_uri="https://github.com/$github_org"
github_repos=(
docker
ronin
ronin-agent-node
ronin-agent-php
ronin-agent-ruby
ronin-code-asm
ronin-code-sql
ronin-core
ronin-db
ronin-db-activerecord
ronin-dns-proxy
ronin-listener
ronin-listener-dns
ronin-listener-http
ronin-exploits
ronin-fuzzer
ronin-masscan
ronin-nmap
ronin-payloads
ronin-post_ex
ronin-rb.github.io
ronin-recon
ronin-repos
ronin-support
ronin-vulns
ronin-web
ronin-web-server
ronin-web-session_cookie
ronin-web-spider
ronin-web-user_agents
rubocop-ronin
scripts
)
src_dir="$HOME/src"
ronin_src_dir="${src_dir}/${github_org}"
#
# 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 exists 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
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
detect_package_manager
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 $? ;;
"") warn "Could not determine Package Manager. Proceeding anyway." ;;
esac
}
#
# Installs git, if it's not installed.
#
function auto_install_git()
{
if ! command -v git >/dev/null; then
log "Installing git ..."
install_packages git || fail "Failed to install git!"
fi
}
#
# 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 git, 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 ;;
pacman) install_packages community/ruby ;;
*) 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) install_packages rubygems ;;
pacman) install_packages community/rubygems ;;
*)
fail "rubygems was not installed along with ruby. Aborting!"
;;
esac
fi
detect_rubygems_install_dir
}
#
# Install gcc if there's no C compiler on the system.
#
function auto_install_gcc()
{
if ! command -v cc >/dev/null; then
log "Installing gcc ..."
install_packages gcc || fail "Failed to install gcc!"
fi
}
#
# Install g++ if there's no C++ compiler on the system.
#
function auto_install_gpp()
{
if ! command -v c++ >/dev/null; then
log "Installing g++ ..."
case "$package_manager" in
dnf|yum) install_packages gcc-g++ ;;
zypper) install_packages gcc-c++ ;;
*) install_packages g++ ;;
esac || fail "Failed to install g++!"
fi
}
#
# Install make if it's not already installed.
#
function auto_install_make()
{
if ! command -v make >/dev/null; then
log "Install make ..."
install_packages make || fail "Failed to install make!"
fi
}
#
# Installs bundler, if it's not installed.
#
function auto_install_bundler()
{
if ! command -v bundle >/dev/null; then
log "Installing bundler ..."
$gem install ${gem_opts[@]} bundler -v "$bundler_version" ||
fail "Failed to install bundler!"
elif [[ "$(bundle --version)" == "Bundler version 1."* ]]; then
log "Updating bundler 1.x to 2.x ..."
$gem update ${gem_opts[@]} bundler
fi
}
#
# Install external dependencies for ronin.
#
function install_dependencies()
{
case "$package_manager" in
dnf|yum)libraries=(readline-devel sqlite-devel) ;;
zypper) libraries=(readline-devel sqlite3-devel) ;;
apt) libraries=(libreadline-dev libsqlite3-dev) ;;
*) libraries=(readline sqlite) ;;
esac
log "Installing external dependencies ..."
install_packages "${libraries[@]}" || \
warn "Failed to install external dependencies. Proceeding anyways."
}
#
# Print the --help usage.
#
function print_usage()
{
cat <<USAGE
usage: ./ronin-dev.sh [OPTIONS] [REPO ...]
Options:
--package-manager [apt|dnf|yum|pacman|zypper|brew|pkg|port]
Sets the package manager to use
-V, --version Prints the version
-h, --help Prints this message
USAGE
}
#
# Parse additional command-line options.
#
function parse_options()
{
local argv=()
while [[ $# -gt 0 ]]; do
case "$1" in
--package-manager)
package_manager="$2"
shift 2
;;
-V|--version)
echo "ronin-dev: $ronin_install_version"
exit
;;
-h|--help)
print_usage
exit
;;
-*)
echo "ronin-dev: unrecognized option $1" >&2
return 1
;;
*)
argv+=($1)
shift
;;
esac
done
if (( ${#argv[@]} > 0 )); then
github_repos=("${argv[@]}")
fi
}
parse_options "$@" || exit $?
detect_system
auto_install_git
auto_install_gcc
auto_install_gpp
auto_install_make
auto_install_ruby
auto_install_bundler
install_dependencies
mkdir -p "$ronin_src_dir"
pushd "$ronin_src_dir"/ >/dev/null
for repo in "${github_repos[@]}"; do
github_uri="${github_base_uri}/${repo}.git"
if [[ -d "$repo" ]]; then
warn "Repository '${repo}' already exists. Skipping."
else
log "Cloning ${github_uri} ..."
git clone "$github_uri" "$repo" || \
fail "Failed to clone '$repo!'"
pushd "$repo" >/dev/null
if [[ -f Gemfile ]]; then
# Have bundler install all gems into a shared gem dir
bundle config set --local path ../vendor/bundle >/dev/null || fail "Failed to run 'bundle config'"
fi
popd >/dev/null
fi
done
ln -sf ronin-rb.github.io website
popd >/dev/null
log "Successfully setup a development environment in ${ronin_src_dir}"
echo
for dir in "${ronin_src_dir}"/*; do
echo " $dir"
done
echo
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