metalx1000

Extract all files from an HAR archive

Jun 17th, 2025
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.83 KB | None | 0 0
  1. #!/bin/bash
  2. ######################################################################
  3. #Copyright (C) 2025 Kris Occhipinti
  4. #https://filmsbykris.com
  5. #This program is free software: you can redistribute it and/or modify
  6. #it under the terms of the GNU General Public License as published by
  7. #the Free Software Foundation version 3 of the License.
  8.  
  9. #This program is distributed in the hope that it will be useful,
  10. #but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. #GNU General Public License for more details.
  13.  
  14. #You should have received a copy of the GNU General Public License
  15. #along with this program.  If not, see <http://www.gnu.org/licenses/>.
  16. ######################################################################
  17.  
  18. # Extracts all files from an har file
  19.  
  20. # fatal uses SIGUSR1 to allow clean fatal errors
  21. trap "exit 1" 10
  22. PROC=$$
  23.  
  24. function error() {
  25.   red=$(echo -en "\e[31m")
  26.   normal=$(echo -en "\e[0m")
  27.  
  28.   echo -e "${red}$@${normal}" >&2
  29.   #  exit 1
  30.   kill -10 $PROC
  31. }
  32.  
  33. [[ $1 ]] || error "Input file needed\n$0 file.har"
  34. har_file="$1"
  35. [[ -f $har_file ]] || error "$har_file does not exist"
  36.  
  37. output_dir="extracted_files"
  38.  
  39. mkdir -p "$output_dir"
  40.  
  41. cat "$har_file" | jq -cr '.log.entries[] | {url: .request.url, content: .response.content.text, encoding: .response.content.encoding}' |
  42.   while read -r entry; do
  43.     echo "$entry"
  44.     url=$(echo "$entry" | jq -r '.url')
  45.     content=$(echo "$entry" | jq -r '.content')
  46.     encoding=$(echo "$entry" | jq -r '.encoding')
  47.  
  48.     # Simple filename based on URL (adjust as needed)
  49.     filename=$(basename "$url" | cut -d\? -f1)
  50.  
  51.     if [ "$encoding" == "base64" ]; then
  52.       echo "$content" | base64 -d >"$output_dir/$filename"
  53.     else
  54.       echo "$content" >"$output_dir/$filename"
  55.     fi
  56.   done
  57.  
Add Comment
Please, Sign In to add comment