forked from pointblank-club/vestigo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·1128 lines (959 loc) · 38.5 KB
/
setup.sh
File metadata and controls
executable file
·1128 lines (959 loc) · 38.5 KB
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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
################################################################################
# Vestigo
################################################################################
# This script installs ALL dependencies for the Vestigo firmware analysis and
# crypto-detection pipeline in a single unified virtual environment.
#
# Supported OS: Ubuntu/Debian, Fedora/RHEL, Arch Linux, macOS
# Requirements: sudo access, internet connection, ~10GB disk space
#
# Usage: ./setup.sh [OPTIONS]
# --skip-ghidra Skip Ghidra installation
# --skip-containers Skip container build
# --skip-cross Skip cross-compiler installation
# --skip-ml Skip ML/torch dependencies
# --minimal Minimal install (backend + qiling only)
# --dry-run Show what would be installed without doing it
# -h, --help Show this help message
################################################################################
set -e # Exit on error
# =============================================================================
# CONFIGURATION
# =============================================================================
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
VENV_DIR="${SCRIPT_DIR}/venv"
GHIDRA_VERSION="11.2.1"
GHIDRA_DATE="20241105"
GHIDRA_INSTALL_DIR="/opt/ghidra"
QILING_DIR="${SCRIPT_DIR}/qiling_analysis/qiling"
ROOTFS_DIR="${SCRIPT_DIR}/qiling_analysis/rootfs"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
# Parse command line arguments
SKIP_GHIDRA=false
SKIP_CONTAINERS=false
SKIP_CROSS=false
SKIP_ML=false
MINIMAL=false
DRY_RUN=false
for arg in "$@"; do
case $arg in
--skip-ghidra)
SKIP_GHIDRA=true
;;
--skip-containers)
SKIP_CONTAINERS=true
;;
--skip-cross)
SKIP_CROSS=true
;;
--skip-ml)
SKIP_ML=true
;;
--minimal)
MINIMAL=true
SKIP_GHIDRA=true
SKIP_CONTAINERS=true
SKIP_CROSS=true
SKIP_ML=true
;;
--dry-run)
DRY_RUN=true
;;
-h|--help)
head -30 "$0" | tail -25
exit 0
;;
*)
echo -e "${RED}Unknown option: $arg${NC}"
echo "Use --help for usage information"
exit 1
;;
esac
done
# =============================================================================
# HELPER FUNCTIONS
# =============================================================================
print_header() {
echo -e "\n${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${BLUE} $1${NC}"
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}\n"
}
print_success() {
echo -e "${GREEN}[✓]${NC} $1"
}
print_error() {
echo -e "${RED}[✗]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[!]${NC} $1"
}
print_info() {
echo -e "${CYAN}[*]${NC} $1"
}
detect_os() {
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
if [ -f /etc/os-release ]; then
. /etc/os-release
OS=$ID
elif [ -f /etc/debian_version ]; then
OS="debian"
elif [ -f /etc/redhat-release ]; then
OS="rhel"
else
OS="unknown"
fi
elif [[ "$OSTYPE" == "darwin"* ]]; then
OS="macos"
else
OS="unknown"
fi
echo $OS
}
check_command() {
command -v "$1" &> /dev/null
}
run_cmd() {
if [ "$DRY_RUN" = true ]; then
echo -e "${YELLOW}[DRY-RUN]${NC} $*"
else
"$@"
fi
}
# =============================================================================
# SYSTEM DEPENDENCIES
# =============================================================================
install_system_deps() {
print_header "Installing System Dependencies"
OS=$(detect_os)
print_info "Detected OS: $OS"
case $OS in
ubuntu|debian|pop|linuxmint)
print_info "Installing dependencies for Debian/Ubuntu..."
run_cmd sudo apt-get update
run_cmd sudo apt-get install -y \
python3 \
python3-pip \
python3-venv \
python3-dev \
git \
build-essential \
cmake \
pkg-config \
wget \
curl \
strace \
file \
binutils \
binwalk \
libffi-dev \
libssl-dev \
liblzma-dev \
liblzo2-dev \
zlib1g-dev \
libsqlite3-dev \
libreadline-dev \
libncurses5-dev \
libncursesw5-dev \
libmagic-dev \
libbz2-dev \
libxml2-dev \
libxmlsec1-dev \
xz-utils \
tk-dev \
yara \
unzip \
openjdk-17-jdk
;;
fedora|rhel|centos|rocky|almalinux)
print_info "Installing dependencies for Fedora/RHEL..."
run_cmd sudo dnf install -y --skip-unavailable \
python3 \
python3-pip \
python3-devel \
git \
gcc \
gcc-c++ \
make \
cmake \
pkg-config \
wget \
curl \
strace \
file \
binutils \
libffi-devel \
openssl-devel \
xz-devel \
lzo-devel \
zlib-devel \
sqlite-devel \
readline-devel \
ncurses-devel \
file-devel \
bzip2-devel \
libxml2-devel \
xmlsec1-devel \
tk-devel \
yara \
unzip \
java-21-openjdk-devel || true
;;
arch|manjaro|endeavouros)
print_info "Installing dependencies for Arch Linux..."
run_cmd sudo pacman -Sy --noconfirm \
python \
python-pip \
git \
base-devel \
cmake \
pkg-config \
wget \
curl \
strace \
file \
binutils \
libffi \
openssl \
xz \
lzo \
zlib \
sqlite \
readline \
ncurses \
bzip2 \
libxml2 \
xmlsec \
tk \
yara \
unzip \
jdk17-openjdk
;;
macos)
print_info "Installing dependencies for macOS..."
if ! check_command brew; then
print_error "Homebrew not found. Installing Homebrew..."
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi
run_cmd brew update
run_cmd brew install \
python@3.11 \
git \
cmake \
pkg-config \
wget \
curl \
libffi \
openssl@3 \
xz \
lzo \
zlib \
sqlite \
readline \
ncurses \
bzip2 \
libxml2 \
xmlsec1 \
tk \
yara \
openjdk@17
print_warning "Note: strace is not available on macOS"
;;
*)
print_error "Unsupported OS: $OS"
print_warning "Please install dependencies manually. See README.md"
exit 1
;;
esac
print_success "System dependencies installed"
}
# =============================================================================
# CONTAINER RUNTIME (PODMAN)
# =============================================================================
install_container_runtime() {
print_header "Installing Container Runtime (Podman)"
if check_command podman; then
print_success "Podman is already installed: $(podman --version)"
return
fi
if check_command docker; then
print_warning "Docker found but Podman preferred. Using Docker as fallback."
return
fi
OS=$(detect_os)
case $OS in
ubuntu|debian|pop|linuxmint)
run_cmd sudo apt-get install -y podman
;;
fedora|rhel|centos|rocky|almalinux)
run_cmd sudo dnf install -y podman
;;
arch|manjaro|endeavouros)
run_cmd sudo pacman -Sy --noconfirm podman
;;
macos)
run_cmd brew install podman
run_cmd podman machine init
run_cmd podman machine start
;;
*)
print_warning "Could not install Podman automatically"
;;
esac
if check_command podman; then
print_success "Podman installed: $(podman --version)"
else
print_warning "Podman installation may have failed. Container features may not work."
fi
}
# =============================================================================
# PYTHON VIRTUAL ENVIRONMENT
# =============================================================================
setup_python_venv() {
print_header "Setting Up Python Virtual Environment"
# Check Python version
if ! check_command python3; then
print_error "Python 3 not found. Please install Python 3.9 or higher."
exit 1
fi
PYTHON_VERSION=$(python3 --version | awk '{print $2}')
print_info "Python version: $PYTHON_VERSION"
# Check if version is at least 3.9
MAJOR=$(echo $PYTHON_VERSION | cut -d. -f1)
MINOR=$(echo $PYTHON_VERSION | cut -d. -f2)
if [ "$MAJOR" -lt 3 ] || ([ "$MAJOR" -eq 3 ] && [ "$MINOR" -lt 9 ]); then
print_error "Python 3.9 or higher is required. Current version: $PYTHON_VERSION"
exit 1
fi
# Create virtual environment
if [ -d "$VENV_DIR" ]; then
print_warning "Virtual environment already exists at $VENV_DIR"
print_info "Using existing virtual environment"
else
print_info "Creating virtual environment at $VENV_DIR..."
run_cmd python3 -m venv "$VENV_DIR"
fi
# Activate virtual environment (skip in dry-run if venv doesn't exist)
if [ "$DRY_RUN" = true ] && [ ! -f "$VENV_DIR/bin/activate" ]; then
print_info "[DRY-RUN] Would activate venv at: $VENV_DIR/bin/activate"
print_warning "[DRY-RUN] Skipping pip/python steps (venv not created in dry-run)"
print_success "Virtual environment setup previewed"
VENV_ACTIVE=false
return
fi
source "$VENV_DIR/bin/activate"
VENV_ACTIVE=true
# Upgrade pip
print_info "Upgrading pip, setuptools, wheel..."
run_cmd pip install --upgrade pip setuptools wheel
print_success "Virtual environment ready"
}
# =============================================================================
# PYTHON DEPENDENCIES
# =============================================================================
install_python_deps() {
print_header "Installing Python Dependencies"
# Skip if venv not active (dry-run without existing venv)
if [ "$DRY_RUN" = true ] && [ ! -f "$VENV_DIR/bin/activate" ]; then
print_info "[DRY-RUN] Would install Python packages in venv"
print_success "Python dependencies installation previewed"
return
fi
source "$VENV_DIR/bin/activate"
# Install typing-extensions early with compatible version for all packages
print_info "Installing typing-extensions..."
run_cmd pip install 'typing_extensions==4.12.2'
# Core web framework
print_info "Installing FastAPI and web dependencies..."
run_cmd pip install \
'fastapi>=0.100.0' \
'uvicorn[standard]>=0.23.0' \
'python-multipart>=0.0.6' \
'pydantic>=2.0.0' \
'python-dotenv>=1.0.0' \
'python-magic>=0.4.27' \
'requests>=2.31.0'
# Database
print_info "Installing database dependencies..."
run_cmd pip install 'prisma>=0.11.0'
# ML and Data Science
print_info "Installing ML and data science packages..."
run_cmd pip install \
'scikit-learn>=1.3.0' \
'lightgbm>=4.0.0' \
'pandas>=2.0.0' \
'numpy>=1.24.0' \
'joblib>=1.3.0'
# Binary analysis core
print_info "Installing binary analysis tools..."
run_cmd pip install \
'pyelftools>=0.28' \
'capstone>=4.0.0' \
'yara-python>=4.0.0' \
'pycryptodome>=3.15.0' \
'z3-solver>=4.11.0' \
'binwalk'
# Qiling dependencies
print_info "Installing Qiling framework dependencies..."
run_cmd pip install \
'unicorn==2.1.3' \
'keystone-engine>=0.9.2' \
'pefile>=2022.5.30' \
'python-registry>=1.3.1' \
'gevent>=20.9.0' \
'multiprocess>=0.70.12.2' \
'pyyaml>=6.0.1' \
'questionary' \
'termcolor'
# LLM integration
print_info "Installing LLM integration..."
run_cmd pip install 'openai>=1.0.0'
# Additional utilities
print_info "Installing additional utilities..."
run_cmd pip install \
'colorama>=0.4.6' \
'tqdm>=4.65.0' \
'rich>=13.0.0' \
'loguru>=0.7.0'
# Development tools
print_info "Installing development tools..."
run_cmd pip install \
'pytest>=7.0.0' \
'pytest-cov>=4.0.0' \
'black>=22.0.0' \
'flake8>=5.0.0' \
'mypy>=0.990'
# Optional ML dependencies (PyTorch for GNN) - CPU ONLY
if [ "$SKIP_ML" = false ]; then
print_info "Installing PyTorch (CPU) and GNN dependencies..."
# Install CPU version of torch first to avoid huge CUDA downloads
run_cmd pip install \
'torch>=2.0.0' \
'torchvision' \
'torchaudio' \
--index-url https://download.pytorch.org/whl/cpu
run_cmd pip install \
'torch-geometric>=2.3.0' \
'matplotlib>=3.7.0' \
'seaborn>=0.12.0' \
'networkx>=2.6.0'
fi
print_success "Python dependencies installed"
}
# =============================================================================
# QILING FRAMEWORK
# =============================================================================
install_qiling() {
print_header "Installing Qiling Framework"
# Skip pip install in dry-run if venv doesn't exist
if [ "$DRY_RUN" = true ] && [ ! -f "$VENV_DIR/bin/activate" ]; then
print_info "[DRY-RUN] Would clone and install Qiling framework"
print_success "Qiling installation previewed"
return
fi
source "$VENV_DIR/bin/activate"
# Clone Qiling if not exists
if [ -d "$QILING_DIR" ]; then
print_info "Qiling directory exists, pulling latest..."
cd "$QILING_DIR"
run_cmd git pull || print_warning "Could not pull latest Qiling updates"
cd "$SCRIPT_DIR"
else
print_info "Cloning Qiling framework..."
run_cmd git clone https://github.com/qilingframework/qiling.git "$QILING_DIR"
fi
# Install Qiling in editable mode
print_info "Installing Qiling in editable mode..."
run_cmd pip install -e "$QILING_DIR"
# Clone rootfs if not exists or empty
if [ -d "$ROOTFS_DIR" ]; then
FILE_COUNT=$(find "$ROOTFS_DIR" -type f 2>/dev/null | wc -l)
if [ "$FILE_COUNT" -gt 10 ]; then
print_info "Rootfs directory exists and has $FILE_COUNT files"
else
print_warning "Rootfs directory exists but is incomplete, removing..."
rm -rf "$ROOTFS_DIR"
print_info "Cloning Qiling rootfs (shallow clone, this may take a while)..."
mkdir -p "$(dirname "$ROOTFS_DIR")"
if run_cmd git clone --depth 1 https://github.com/qilingframework/rootfs.git "$ROOTFS_DIR"; then
print_success "Qiling rootfs cloned successfully"
else
print_error "Failed to clone Qiling rootfs"
print_info "You can manually clone with: git clone --depth 1 https://github.com/qilingframework/rootfs.git $ROOTFS_DIR"
fi
fi
else
print_info "Cloning Qiling rootfs (shallow clone, this may take a while)..."
mkdir -p "$(dirname "$ROOTFS_DIR")"
if run_cmd git clone --depth 1 https://github.com/qilingframework/rootfs.git "$ROOTFS_DIR"; then
print_success "Qiling rootfs cloned successfully"
else
print_error "Failed to clone Qiling rootfs"
print_info "You can manually clone with: git clone --depth 1 https://github.com/qilingframework/rootfs.git $ROOTFS_DIR"
fi
fi
# Fix typing-extensions conflict after Qiling installation
# python-fx (Qiling dependency) pins typing-extensions==4.12.2
# but pydantic needs >=4.14.1, so we uninstall python-fx and upgrade
print_info "Fixing typing-extensions conflicts from python-fx..."
run_cmd pip uninstall -y python-fx 2>/dev/null || true
run_cmd pip install --force-reinstall 'typing-extensions>=4.14.1'
print_success "Qiling framework installed"
}
# =============================================================================
# GHIDRA HEADLESS ANALYZER
# =============================================================================
install_ghidra() {
print_header "Installing Ghidra Headless Analyzer"
if [ -d "$GHIDRA_INSTALL_DIR" ] && [ -f "$GHIDRA_INSTALL_DIR/support/analyzeHeadless" ]; then
print_success "Ghidra already installed at $GHIDRA_INSTALL_DIR"
return
fi
# Check for Java
if ! check_command java; then
print_error "Java not found. Ghidra requires Java 17+."
print_info "Please install OpenJDK 17 and re-run this script."
return
fi
JAVA_VERSION=$(java -version 2>&1 | head -1 | cut -d'"' -f2 | cut -d'.' -f1)
if [ "$JAVA_VERSION" -lt 17 ]; then
print_warning "Java 17+ recommended. Current version: $JAVA_VERSION"
fi
# Download Ghidra
GHIDRA_URL="https://github.com/NationalSecurityAgency/ghidra/releases/download/Ghidra_${GHIDRA_VERSION}_build/ghidra_${GHIDRA_VERSION}_PUBLIC_${GHIDRA_DATE}.zip"
GHIDRA_ZIP="/tmp/ghidra_${GHIDRA_VERSION}.zip"
print_info "Downloading Ghidra ${GHIDRA_VERSION}..."
run_cmd wget -O "$GHIDRA_ZIP" "$GHIDRA_URL"
# Extract Ghidra
print_info "Extracting Ghidra to $GHIDRA_INSTALL_DIR..."
run_cmd sudo mkdir -p "$GHIDRA_INSTALL_DIR"
run_cmd sudo unzip -q "$GHIDRA_ZIP" -d /opt/
run_cmd sudo mv "/opt/ghidra_${GHIDRA_VERSION}_PUBLIC"/* "$GHIDRA_INSTALL_DIR/"
run_cmd sudo rmdir "/opt/ghidra_${GHIDRA_VERSION}_PUBLIC"
run_cmd rm "$GHIDRA_ZIP"
# Make scripts executable
run_cmd sudo chmod +x "$GHIDRA_INSTALL_DIR/support/analyzeHeadless"
print_success "Ghidra installed to $GHIDRA_INSTALL_DIR"
}
# =============================================================================
# CROSS-COMPILER TOOLCHAINS
# =============================================================================
install_cross_compilers() {
print_header "Installing Cross-Compiler Toolchains"
OS=$(detect_os)
case $OS in
ubuntu|debian|pop|linuxmint)
print_info "Installing cross-compiler binutils..."
run_cmd sudo apt-get install -y \
binutils-aarch64-linux-gnu \
binutils-arm-linux-gnueabi \
binutils-arm-linux-gnueabihf \
binutils-mips-linux-gnu \
binutils-mipsel-linux-gnu \
gcc-aarch64-linux-gnu \
gcc-arm-linux-gnueabi \
gcc-arm-linux-gnueabihf \
gcc-mips-linux-gnu
;;
fedora|rhel|centos|rocky|almalinux)
run_cmd sudo dnf install -y --skip-unavailable \
binutils-aarch64-linux-gnu \
binutils-arm-linux-gnu \
binutils-mips-linux-gnu \
gcc-aarch64-linux-gnu \
gcc-arm-linux-gnu
;;
arch|manjaro|endeavouros)
run_cmd sudo pacman -Sy --noconfirm \
aarch64-linux-gnu-binutils \
arm-linux-gnueabihf-binutils
;;
macos)
print_warning "Cross-compilers for macOS require manual setup"
print_info "Consider using Docker/Podman for cross-compilation"
;;
*)
print_warning "Cross-compiler installation not supported for $OS"
;;
esac
# Verify installation
print_info "Verifying cross-compiler tools..."
for tool in aarch64-linux-gnu-ld arm-linux-gnueabi-ld mips-linux-gnu-ld; do
if check_command $tool; then
print_success "$tool found"
else
print_warning "$tool not found"
fi
done
}
# =============================================================================
# CONTAINER BUILD (SASQUATCH)
# =============================================================================
build_containers() {
print_header "Building Containers"
# Determine container runtime
if check_command podman; then
CONTAINER_CMD="podman"
elif check_command docker; then
CONTAINER_CMD="docker"
else
print_error "No container runtime found. Skipping container build."
return
fi
print_info "Using $CONTAINER_CMD as container runtime"
# Build sasquatch container
if [ -f "$SCRIPT_DIR/Containerfile" ]; then
print_info "Building sasquatch_tool container..."
run_cmd $CONTAINER_CMD build -t sasquatch_tool -f "$SCRIPT_DIR/Containerfile" "$SCRIPT_DIR"
# Also tag it as firmware-extractor for backward compatibility
if [ "$DRY_RUN" != true ]; then
$CONTAINER_CMD tag sasquatch_tool firmware-extractor 2>/dev/null || true
print_info "Tagged as both 'sasquatch_tool' and 'firmware-extractor'"
fi
print_success "sasquatch_tool container built"
else
print_warning "Containerfile not found at $SCRIPT_DIR/Containerfile"
fi
# Build cross-compiler container for factory
if [ -f "$SCRIPT_DIR/factory/Dockerfile.builder" ]; then
print_info "Building cross-compiler container..."
run_cmd $CONTAINER_CMD build -t vestigo-builder -f "$SCRIPT_DIR/factory/Dockerfile.builder" "$SCRIPT_DIR/factory"
print_success "vestigo-builder container built"
fi
}
# =============================================================================
# ENVIRONMENT CONFIGURATION
# =============================================================================
setup_environment() {
print_header "Setting Up Environment Configuration"
# Create .env file if not exists
if [ ! -f "$SCRIPT_DIR/.env" ]; then
if [ -f "$SCRIPT_DIR/.env.example" ]; then
print_info "Creating .env from .env.example..."
cp "$SCRIPT_DIR/.env.example" "$SCRIPT_DIR/.env"
else
print_info "Creating .env file..."
cat > "$SCRIPT_DIR/.env" << EOF
# Vestigo Environment Configuration
# Generated by setup.sh on $(date)
# ============================================================================
# REQUIRED: Database Configuration
# ============================================================================
# You MUST set a valid PostgreSQL connection string for the backend to work
# For NeonDB (recommended): postgresql://user:password@ep-xxx.neon.tech/vestigo?sslmode=require
# For local PostgreSQL: postgresql://postgres:password@localhost:5432/vestigo
DATABASE_URL=postgresql://user:password@localhost:5432/vestigo
# ============================================================================
# REQUIRED: OpenAI API Configuration (for LLM-assisted analysis)
# ============================================================================
# Get your API key from: https://platform.openai.com/api-keys
OPENAI_API_KEY=your_openai_api_key_here
# ============================================================================
# OPTIONAL: Perplexity API Configuration
# ============================================================================
PERPLEXITY_API_KEY=your_perplexity_api_key_here
# ============================================================================
# System Configuration (Auto-configured)
# ============================================================================
# Ghidra Installation Path
GHIDRA_HOME=${GHIDRA_INSTALL_DIR}
# Python Virtual Environment Directory
VENV_DIR=${VENV_DIR}
# Python path for imports (scripts directory)
PYTHONPATH=${SCRIPT_DIR}:${SCRIPT_DIR}/scripts:${SCRIPT_DIR}/backend
EOF
fi
print_success ".env file created"
else
print_info ".env file already exists"
fi
# Update .env with Ghidra path if installed
if [ -d "$GHIDRA_INSTALL_DIR" ]; then
if grep -q "^GHIDRA_HOME=" "$SCRIPT_DIR/.env"; then
sed -i "s|^GHIDRA_HOME=.*|GHIDRA_HOME=${GHIDRA_INSTALL_DIR}|" "$SCRIPT_DIR/.env"
fi
fi
# Add PYTHONPATH to .env for backend to find scripts
if ! grep -q "^PYTHONPATH=" "$SCRIPT_DIR/.env" 2>/dev/null; then
echo "" >> "$SCRIPT_DIR/.env"
echo "# Python path for imports" >> "$SCRIPT_DIR/.env"
echo "PYTHONPATH=${SCRIPT_DIR}:${SCRIPT_DIR}/scripts:${SCRIPT_DIR}/backend" >> "$SCRIPT_DIR/.env"
fi
# Initialize Prisma client (skip in dry-run without venv)
if [ "$DRY_RUN" = true ] && [ ! -f "$VENV_DIR/bin/activate" ]; then
print_info "[DRY-RUN] Would generate Prisma client"
else
print_info "Generating Prisma client..."
source "$VENV_DIR/bin/activate"
cd "$SCRIPT_DIR/backend"
if [ -f "prisma/schema.prisma" ]; then
run_cmd prisma generate || print_warning "Prisma generate failed. Set DATABASE_URL first."
fi
cd "$SCRIPT_DIR"
fi
print_success "Environment configuration complete"
}
# =============================================================================
# UPDATE ACTIVATION SCRIPT
# =============================================================================
create_activation_script() {
print_header "Creating Activation Script"
cat > "$SCRIPT_DIR/activate_vestigo.sh" << 'EOF'
#!/bin/bash
# Vestigo Environment Activation Script
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Activate Python virtual environment
if [ -f "$SCRIPT_DIR/venv/bin/activate" ]; then
source "$SCRIPT_DIR/venv/bin/activate"
echo "✓ Python virtual environment activated"
else
echo "✗ Virtual environment not found. Run setup.sh first."
exit 1
fi
# Set PYTHONPATH to include scripts directory and project root
export PYTHONPATH="$SCRIPT_DIR:$SCRIPT_DIR/scripts:$SCRIPT_DIR/backend:${PYTHONPATH:-}"
echo "✓ PYTHONPATH set: scripts, backend accessible"
# Set Ghidra path if installed
if [ -d "/opt/ghidra" ]; then
export GHIDRA_HOME="/opt/ghidra"
export PATH="$GHIDRA_HOME/support:$PATH"
echo "✓ Ghidra path set: $GHIDRA_HOME"
fi
# Load environment variables
if [ -f "$SCRIPT_DIR/.env" ]; then
export $(grep -v '^#' "$SCRIPT_DIR/.env" | xargs)
echo "✓ Environment variables loaded from .env"
fi
# Show status
echo ""
echo "═══════════════════════════════════════════════════════════"
echo " Vestigo Environment Ready!"
echo "═══════════════════════════════════════════════════════════"
echo "Python: $(python --version)"
echo "Pip: $(pip --version | cut -d' ' -f1-2)"
echo "Directory: $SCRIPT_DIR"
echo ""
echo "Quick commands:"
echo " Backend: cd backend && uvicorn main:app --reload"
echo " Frontend: cd frontend && npm run dev"
echo " Ghidra: analyzeHeadless --help"
echo ""
echo "To deactivate: deactivate"
echo "═══════════════════════════════════════════════════════════"
EOF
chmod +x "$SCRIPT_DIR/activate_vestigo.sh"
print_success "Activation script created: activate_vestigo.sh"
}
# =============================================================================
# VERIFICATION
# =============================================================================
verify_installation() {
print_header "Verifying Installation"
# Skip verification in dry-run if venv doesn't exist
if [ "$DRY_RUN" = true ] && [ ! -f "$VENV_DIR/bin/activate" ]; then
print_info "[DRY-RUN] Would verify installation"
print_success "Dry-run complete - no actual changes were made"
return 0
fi
source "$VENV_DIR/bin/activate"
ALL_OK=true
# Check Python modules
print_info "Checking Python modules..."
# Map display names to actual import names (some packages have different import names)
declare -A MODULE_MAP=(
["fastapi"]="fastapi"
["prisma"]="prisma"
["pandas"]="pandas"
["sklearn"]="sklearn"
["lightgbm"]="lightgbm"
["pyelftools"]="elftools"
["capstone"]="capstone"
["yara"]="yara"
["pycryptodome"]="Crypto"
["qiling"]="qiling"
)
MODULES=("fastapi" "prisma" "pandas" "sklearn" "lightgbm" "pyelftools" "capstone" "yara" "pycryptodome" "qiling")
for module in "${MODULES[@]}"; do
# Get the actual import name (may differ from package name)
import_name="${MODULE_MAP[$module]}"
if [ -z "$import_name" ]; then
import_name="$module"
fi
ERROR_MSG=$(python3 -c "import $import_name" 2>&1)
if [ $? -eq 0 ]; then
print_success "$module"
else
print_error "$module NOT installed or import failed"
if [ ! -z "$ERROR_MSG" ]; then
echo " Error: $ERROR_MSG" | head -n 3
fi
ALL_OK=false
fi
done
# Check Ghidra
if [ "$SKIP_GHIDRA" = false ]; then
print_info "Checking Ghidra..."
if [ -f "$GHIDRA_INSTALL_DIR/support/analyzeHeadless" ]; then
print_success "Ghidra headless analyzer found"
else
print_warning "Ghidra headless analyzer not found"
fi
fi
# Check containers
if [ "$SKIP_CONTAINERS" = false ]; then
print_info "Checking containers..."
if check_command podman; then
if podman image exists sasquatch_tool 2>/dev/null; then
print_success "sasquatch_tool container exists"
else
print_warning "sasquatch_tool container not built"
fi
elif check_command docker; then
if docker image inspect sasquatch_tool >/dev/null 2>&1; then
print_success "sasquatch_tool container exists"
else
print_warning "sasquatch_tool container not built"
fi
fi
fi
# Check rootfs
print_info "Checking Qiling rootfs..."
if [ -d "$ROOTFS_DIR" ]; then
FILE_COUNT=$(find "$ROOTFS_DIR" -type f 2>/dev/null | wc -l)
if [ "$FILE_COUNT" -gt 10 ]; then
print_success "Qiling rootfs populated ($FILE_COUNT files)"
else
print_warning "Qiling rootfs exists but seems incomplete ($FILE_COUNT files)"
print_info "Try: git clone --depth 1 https://github.com/qilingframework/rootfs.git $ROOTFS_DIR"
fi
else
print_warning "Qiling rootfs directory not found: $ROOTFS_DIR"
ALL_OK=false
fi
if [ "$ALL_OK" = true ]; then
return 0
else
return 1
fi
}
# =============================================================================
# PRINT FINAL INSTRUCTIONS
# =============================================================================
print_final_instructions() {
print_header "Setup Complete!"
cat << EOF
${GREEN}Installation successful!${NC}
${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}
${CYAN}Quick Start:${NC}
${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}
1. Activate the environment:
${YELLOW}source activate_vestigo.sh${NC}
2. Configure your .env file:
${YELLOW}nano .env${NC}
${RED}REQUIRED:${NC} Set your DATABASE_URL (PostgreSQL connection string)
${RED}REQUIRED:${NC} Set your OPENAI_API_KEY
${YELLOW}OPTIONAL:${NC} Set your PERPLEXITY_API_KEY