summarylogtreecommitdiffstats
path: root/blackarch-helper
blob: 0e24f5e2677a6c2b279797c3ea5d9259cccd8893 (plain)
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
#!/usr/bin/env bash
#
# blackarch-helper
# Simple CLI helper for BlackArch tools
# Author: Lucas "x0rgus"
# License: MIT
# Repository: https://github.com/x0rgus/blackarch-helper
#
# --- Configuration ---
# Set VERBOSE to "false" by default. It can be overridden by a command-line flag.
VERBOSE=false

## --- Color and Symbol Configuration ---
COLOR_RESET=$'\033[0m'
COLOR_GREEN=$'\033[0;32m'
COLOR_YELLOW=$'\033[0;33m'
COLOR_RED=$'\033[0;31m'
COLOR_CYAN=$'\033[0;36m'
COLOR_BOLD=$'\033[1m'

# --- Logging Functions ---
# Helper functions to standardize message output.

einfo() { echo -e "${COLOR_GREEN}[INFO]${COLOR_RESET} $1"; }
ewarn() { echo -e "${COLOR_YELLOW}[WARN]${COLOR_RESET} $1"; } >&2
eerror() { echo -e "${COLOR_RED}[ERROR]${COLOR_RESET} $1"; } >&2
edie() { eerror "$1"; exit 1; }

# --- Core Functions ---

# Check if the BlackArch repository is configured in pacman.conf
check_blackarch_repo() {
    if ! grep -q '^\[blackarch\]' /etc/pacman.conf; then
        eerror "BlackArch repository not found in /etc/pacman.conf."
        echo -e "\nPlease follow the installation instructions at:"
        echo -e "${COLOR_CYAN}https://blackarch.org/guide.html${COLOR_RESET}\n"
        exit 1
    fi
}

# Check if fzf is installed
check_fzf() {
    if ! command -v fzf &>/dev/null; then
        edie "fzf not found. Please install it with: sudo pacman -S fzf"
    fi
}

# --- Logic Functions (Backend) ---

# List all available BlackArch categories, hiding stderr by default.
_list_all_categories() {
    pacman -Sg 2>/dev/null | grep '^blackarch-' | cut -d' ' -f1 | sort -u
}

# List all tools in the BlackArch repository, hiding stderr by default.
_list_all_tools() {
    pacman -Sgg 2>/dev/null | grep '^blackarch-' | cut -d' ' -f2 | sort -u
}

# List tools in a specific category, hiding stderr by default.
_list_tools_in_category() {
    pacman -Sg "$1" 2>/dev/null | awk '{print $2}'
}

# NEW: Pre-processes the tool list to show which are already installed.
_get_tools_with_install_status() {
    local category="$1"
    # Get all installed blackarch packages once.
    local installed_pkgs
    installed_pkgs=$(pacman -Qqg blackarch 2>/dev/null)

    # Get all tools in the given category.
    _list_tools_in_category "$category" | while read -r tool; do
        # Check if the tool is in the list of installed packages.
        if grep -q -w "^${tool}$" <<< "$installed_pkgs"; then
            # If installed, prepend a check mark.
            printf "✔ %s\n" "$tool"
        else
            # If not, prepend spaces for alignment.
            printf "  %s\n" "$tool"
        fi
    done
}

# --- Verbose-aware Command Execution ---

# Shows a spinner while a command runs in the background.
_spinner() {
    local msg="$1"
    shift
    local cmd=("$@")
    local spinner_chars="/-\\|"
    
    "${cmd[@]}" &
    local pid=$!
    trap 'kill $pid 2>/dev/null' SIGINT

    einfo "$msg"
    while kill -0 $pid 2>/dev/null; do
        for (( i=0; i<${#spinner_chars}; i++ )); do
            echo -ne "${COLOR_GREEN}[BUSY]${COLOR_RESET} ${spinner_chars:$i:1}\r"
            sleep 0.1
        done
    done
    echo -e "\r${COLOR_GREEN}[DONE]${COLOR_RESET} Operation finished.   "

    wait $pid
    return $?
}

# Installs or upgrades packages.
_run_pacman_S() {
    if [[ "$VERBOSE" == "true" ]]; then
        einfo "Running pacman in verbose mode..."
        # shellcheck disable=SC2086
        sudo pacman -S --needed --noconfirm "$@"
    else
        _spinner "Running pacman (output hidden)..." sudo pacman -S --needed --noconfirm "$@" >/dev/null 2>&1
    fi

    if [[ $? -ne 0 ]]; then
        eerror "An error occurred during the pacman operation."
        ewarn "Try running with the --verbose flag to see detailed output."
        return 1
    fi
    return 0
}


# --- Subcommand Functions (User Interface) ---

# Handler for the 'category' command
handle_category() {
    local action="$1"
    local category_name="$2"

    case "$action" in
        list)
            einfo "Listing all BlackArch categories:"
            _list_all_categories
            ;;
        install)
            [[ -z "$category_name" ]] && edie "Usage: $0 category install <category-name>"
            einfo "Preparing to install all tools from the '$category_name' category..."
            # shellcheck disable=SC2046
            _run_pacman_S $(_list_tools_in_category "$category_name")
            einfo "Installation of category '$category_name' complete."
            ;;
        *)
            eerror "Invalid subcommand for 'category'. Use 'list' or 'install'."
            usage
            exit 1
            ;;
    esac
}

# Handler for the 'tool' command
handle_tool() {
    local action="$1"
    local tool_name="$2"

    case "$action" in
        list)
            einfo "Listing all tools in the BlackArch repository..."
            _list_all_tools
            ;;
        list-installed)
            einfo "Listing all installed BlackArch packages:"
            pacman -Qg blackarch --color=never
            ;;
        search)
            [[ -z "$tool_name" ]] && edie "Usage: $0 tool search <keyword>"
            einfo "Searching for tools with the keyword '$tool_name':"
            _list_all_tools | grep -i --color=auto "$tool_name"
            ;;
        info)
            [[ -z "$tool_name" ]] && edie "Usage: $0 tool info <tool-name>"
            einfo "Displaying information for the tool '$tool_name':"
            pacman -Si "$tool_name"
            ;;
        install)
            [[ -z "$tool_name" ]] && edie "Usage: $0 tool install <package-name>"
            einfo "Attempting to install '$tool_name'..."
            _run_pacman_S "$tool_name"
            ;;
        upgrade)
            einfo "Checking for installed BlackArch packages to upgrade..."
            local installed_packages
            installed_packages=$(comm -12 <(pacman -Slq blackarch | sort) <(pacman -Qq | sort))
            
            if [[ -z "$installed_packages" ]]; then
                einfo "No BlackArch packages are installed. Nothing to upgrade."
                exit 0
            fi
            
            einfo "The following installed BlackArch packages will be checked for updates:"
            echo -e "${COLOR_CYAN}${installed_packages}${COLOR_RESET}"
            
            read -p $'\e[33m[WARN]\e[0m Do you want to proceed with the upgrade? (y/N) ' -n 1 -r reply
            echo
            
            if [[ "$reply" =~ ^[Yy]$ ]]; then
                einfo "Starting upgrade..."
                # shellcheck disable=SC2086
                _run_pacman_S $installed_packages
                einfo "Upgrade process complete."
            else
                ewarn "Upgrade cancelled by the user."
            fi
            ;;
        *)
            eerror "Invalid subcommand for 'tool'. Use 'list', 'search', 'info', 'install', 'upgrade', or 'list-installed'."
            usage
            exit 1
            ;;
    esac
}

# Interactive menu with FZF
interactive_menu() {
    check_fzf
    
    while true; do
        einfo "Select a category to explore (ESC to exit)"
        local category
        category=$(_list_all_categories | fzf --prompt="▶ Category: " --height=40% --border --header="Press ESC to exit the program.")
        
        [[ -z "$category" ]] && break

        while true; do
            einfo "Category: ${COLOR_CYAN}$category${COLOR_RESET}"
            local fzf_output
            # UPDATED: Major changes to the fzf command for better UX.
            fzf_output=$(_get_tools_with_install_status "$category" | fzf \
                --multi \
                --prompt="▶ Tool: " \
                --border \
                --header="✔=Installed | TAB to select | ENTER to install | CTRL-I for info | ESC to go back" \
                --preview-window 'right:50%:border-left' \
                --preview 'pacman -Si $(echo {} | awk "{print \$NF}")' \
                --bind 'ctrl-i:toggle-preview' \
                --expect=esc)

            [[ -z "$fzf_output" ]] && break 2

            local key_pressed
            key_pressed=$(head -n1 <<< "$fzf_output")
            
            if [[ "$key_pressed" == "esc" ]]; then
                break
            fi

            local selected_tools
            # UPDATED: Strip the status indicator (✔) and spaces from the selection.
            selected_tools=$(tail -n +2 <<< "$fzf_output" | awk '{print $NF}')

            if [[ -z "$selected_tools" ]]; then
                ewarn "No tools selected. Press ESC to go back."
                sleep 1
                continue
            fi

            einfo "The following tools will be installed:"
            echo -e "${COLOR_CYAN}${selected_tools}${COLOR_RESET}"
            
            read -p $'\e[33m[WARN]\e[0m Do you want to proceed with the installation? (y/N) ' -n 1 -r reply
            echo

            if [[ "$reply" =~ ^[Yy]$ ]]; then
                _run_pacman_S "${selected_tools}"
                einfo "Installation complete. Press any key to continue."
                read -n 1 -s
            else
                ewarn "Installation cancelled by the user."
            fi
            
            continue 
        done
    done

    einfo "Exiting blackarch-helper."
}

# Help/usage function
usage() {
    cat <<EOF

${COLOR_BOLD}blackarch-helper${COLOR_RESET}: A CLI tool to explore the BlackArch repository.

${COLOR_BOLD}USAGE:${COLOR_RESET}
    blackarch-helper [options] <command> <subcommand> [arguments]

${COLOR_BOLD}OPTIONS:${COLOR_RESET}
    ${COLOR_YELLOW}-v, --verbose${COLOR_RESET}   Enable verbose output for commands like installations.

${COLOR_BOLD}COMMANDS:${COLOR_RESET}
    ${COLOR_CYAN}category${COLOR_RESET}    Manage tool categories.
        ${COLOR_YELLOW}list${COLOR_RESET}          Lists all available categories.
        ${COLOR_YELLOW}install${COLOR_RESET} <cat>  Installs all tools from a category.

    ${COLOR_CYAN}tool${COLOR_RESET}        Manage individual tools.
        ${COLOR_YELLOW}list${COLOR_RESET}          Lists all tools in the repository.
        ${COLOR_YELLOW}list-installed${COLOR_RESET}  Lists all installed BlackArch packages.
        ${COLOR_YELLOW}search${COLOR_RESET}  <str>  Searches for tools by keyword.
        ${COLOR_YELLOW}info${COLOR_RESET}    <pkg>  Shows information about a tool (package).
        ${COLOR_YELLOW}install${COLOR_RESET} <pkg>  Installs a specific BlackArch package.
        ${COLOR_YELLOW}upgrade${COLOR_RESET}       Upgrades all installed BlackArch packages.

    ${COLOR_CYAN}interactive${COLOR_RESET}   Opens an interactive FZF menu to explore categories and tools.
    ${COLOR_CYAN}help${COLOR_RESET}          Shows this help message.

${COLOR_BOLD}EXAMPLES:${COLOR_RESET}
    blackarch-helper category list
    blackarch-helper tool search nmap
    blackarch-helper tool install metasploit
    sudo blackarch-helper tool upgrade
    sudo blackarch-helper --verbose category install blackarch-scanner

EOF
}

# --- Main Execution ---

# Initial check
check_blackarch_repo

# --- Argument Parsing for Flags ---
while [[ $# -gt 0 ]]; do
    case "$1" in
        -v|--verbose)
            VERBOSE=true
            shift
            ;;
        *)
            break
            ;;
    esac
done


# If no arguments are left, default to the interactive menu.
if [[ $# -eq 0 ]]; then
    interactive_menu
    exit 0
fi

# Command Dispatcher
case "$1" in
    category)
        handle_category "${@:2}"
        ;;
    tool)
        handle_tool "${@:2}"
        ;;
    interactive | fzf)
        interactive_menu
        ;;
    help | --help | -h)
        usage
        ;;
    *)
        eerror "Unknown command '$1'."
        echo -e "\nValid commands are: ${COLOR_CYAN}category, tool, interactive, help${COLOR_RESET}"
        exit 1
        ;;
esac