Advertisement
CSenshi

CN - HW2 (DNS) checker

Nov 16th, 2019
708
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.07 KB | None | 0 0
  1. #!/bin/bash
  2. # Usage: ./test.sh
  3. #        ./test.sh {path_to_dns_server}
  4. SERVER="./dns_server.py"
  5. if [ "$#" -eq 1 ]; then
  6.     SERVER=$1
  7. fi
  8. CONFIG="config/"
  9. IP="127.0.0.1"
  10. PORT=5353
  11. LOG=0
  12. ERROR_FILE='error.log'
  13.  
  14. PARAMS=(A MX NS TXT SOA AAAA CNAME)
  15.  
  16. DEBUG_LOG(){
  17.     CUR_SCORE=$1
  18.     DOMAIN=$2
  19.     FLAG=$3
  20.     if [ $CUR_SCORE -eq "1" ]; then
  21.         BOOL='True'
  22.     else
  23.         BOOL='False'
  24.     fi
  25.     filler='         '
  26.     line='----------------------------------------'
  27.     spac='                    '
  28.     R1=$(printf "* %s %s |" $DOMAIN  "${spac:${#DOMAIN}}" )
  29.     RES="$R1 $FLAG"
  30.     printf "%s %s %s %s\n" "$R1" $FLAG "${line:${#RES}}" $BOOL
  31. }
  32.  
  33. run_test(){
  34.     TEST_COUNT=${#ANS[*]}
  35.     for (( i=0; i<=$(( $TEST_COUNT -1 )); i++ )); do
  36.         FLAG=${PARAMS[$i]}
  37.         CUR_SCORE=$(dig -t ${FLAG} +noall +answer $DOMAIN @$IP -p $PORT | grep -q "${ANS[${FLAG}]}" ; echo $((1-$?)))
  38.         SCORE=$((SCORE + $CUR_SCORE))
  39.        
  40.         DEBUG_LOG ${CUR_SCORE} ${DOMAIN} ${FLAG}
  41.         FULL_SCORE=$((FULL_SCORE + 1))
  42.     done
  43.     echo
  44. }
  45.  
  46. test_configuration(){
  47.     echo "### Testing Zone Files..."
  48.     # example.com
  49.     DOMAIN=example.com
  50.     declare -A ANS=([A]="1.1.1.1" [MX]="aspmx.l.example.com" [NS]="ns1.example.com" [TXT]="v=spf1 mx ~all" [SOA]="2019100600" [AAAA]="2406:da00:ff00::22ce:806" [CNAME]="example.com")
  51.     run_test
  52.    
  53.     # example2.com
  54.     DOMAIN=example2.com
  55.     declare -A ANS=([A]="38.21.5.14" [MX]="aspmx.l.example2.com" [NS]="ns1.example2.com" [TXT]="v=spf1 a mx ip4:69.64.153.131" [SOA]="2019100600" [AAAA]="2406:da00:ff00::22ce:806" [CNAME]="example2.com")
  56.     run_test
  57.    
  58.     # FreeUniCN19.com
  59.     DOMAIN=FreeUniCN19.com
  60.     declare -A ANS=([A]="4.8.15.16" [MX]="aspmx.l.FreeUniCN19.com" [NS]="ns1.FreeUniCN19.com" [TXT]="You Shall Not Pass!" [SOA]="2020506078" [AAAA]="dae7:b6c9:6c51:c4d8:1ae3:abf9:1960:c1b2" [CNAME]="FreeUniCN19.com")
  61.     run_test
  62. }
  63.  
  64. test_outside(){
  65.     echo "### Testing Outside of configurations..."
  66.     # twitch.tv
  67.     DOMAIN=twitch.tv
  68.     declare -A ANS=([A]="151.101.66.167" [MX]="aspmx.l.google.com." [NS]="ns1.p18.dynect.net." [TXT]="v=spf1 include:_spf.google.com include:amazonses.com")
  69.     run_test
  70.    
  71.     # stackoverflow.com
  72.     DOMAIN=stackoverflow.com
  73.     declare -A ANS=([A]="151.101.193.69" [MX]="aspmx.l.google.com." [NS]="ns-358.awsdns-44.com." [TXT]="MS=ms52592611")
  74.     run_test
  75.    
  76.     # freeuni.edu.ge
  77.     DOMAIN=freeuni.edu.ge
  78.     declare -A ANS=([A]="185.163.200.15" [MX]="ASPMX.L.GOOGLE.COM." [NS]="ns1.proservice.ge." [TXT]="v=spf1 +a +mx +ip4:74.125.43.121" [SOA]="2019012400" )
  79.     run_test
  80. }
  81.  
  82. test_cache(){
  83.     echo "### Testing Caching..."
  84.     # github.com
  85.     DOMAIN=github.com
  86.     cache_time1=($(dig -t A $DOMAIN @$IP -p $PORT | awk '/Query time/ {print $4}'))
  87.     echo "* $DOMAIN 1st try: time ${cache_time1}"
  88.     cache_time2=($(dig -t A $DOMAIN @$IP -p $PORT | awk '/Query time/ {print $4}'))
  89.     echo "* $DOMAIN 2nd try: time ${cache_time2}"
  90.     [ $(($cache_time1 / 2)) -ge $cache_time2 ] && [ $cache_time1 -ne 0 ] && SCORE=$((SCORE + 5))
  91.     FULL_SCORE=$((FULL_SCORE + 5))
  92.     echo
  93.    
  94.     # pastebin.com
  95.     DOMAIN=pastebin.com
  96.     cache_time1=($(dig -t MX $DOMAIN @$IP -p $PORT | awk '/Query time/ {print $4}'))
  97.     echo "* $DOMAIN 1st try: time ${cache_time1}"
  98.     cache_time2=($(dig -t MX $DOMAIN @$IP -p $PORT | awk '/Query time/ {print $4}'))
  99.     echo "* $DOMAIN 2nd try: time ${cache_time2}"
  100.     [ $(($cache_time1 / 2)) -ge $cache_time2 ] && [ $cache_time1 -ne 0 ] && SCORE=$((SCORE + 5))
  101.     FULL_SCORE=$((FULL_SCORE + 5))
  102.     echo
  103. }
  104.  
  105. declare -i SCORE=0
  106. declare -i FULL_SCORE=0
  107. if [ -f $SERVER ]; then
  108.     if [ $LOG -eq "0" ]; then
  109.         DBG='> /dev/null'
  110.     fi
  111.     eval python3 $SERVER $CONFIG $IP $PORT $DBG  &
  112.     SPID=$!
  113.     sleep 1
  114.    
  115.     test_configuration
  116.     test_outside
  117.     test_cache
  118.    
  119.     # Show Result
  120.     echo "Score : ${SCORE}/${FULL_SCORE}"
  121.     echo "Total score: " $(((SCORE*100 / FULL_SCORE )))%
  122.     echo $1
  123.     read -p "press enter to kill process"
  124.     sudo kill -9 $SPID
  125. else
  126.     echo "File $FILE does not exist"
  127. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement