Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- TOS=3s
- BYPASS_DIR='/home/cs168/public'
- STARTER_DIR='Starter_files'
- HTTP_LOG='http.log'
- # full log file
- LOG_FILE="Students.log"
- > ${LOG_FILE}
- MAX_SCORE=10
- test_firewall(){
- SCORE=0
- RES_TXT='test_tmp.txt'
- #launch python script
- chmod +x main.py
- sudo ./main.py --mode firewall --rule rules.conf &
- sleep 1
- ### DNS
- # test 1 : shoudl pass
- > ${RES_TXT}
- echo 'test 1: dig @8.8.8.8 mail.google.com -- DENY' | tee -a ${TMP}
- timeout ${TOS} dig @8.8.8.8 mail.google.com >> ${RES_TXT}
- echo 'Should Deny : ' >> ${TMP}
- if grep -q '169.229.49.130' "${RES_TXT}"; then
- SCORE=$((SCORE+1))
- echo 'True'>> ${TMP}
- else
- echo 'False'>> ${TMP}
- fi
- cat ${RES_TXT} >> ${TMP}
- echo '' >> ${TMP}
- # test 2: should drop
- > ${RES_TXT}
- echo 'test 2: dig @8.8.8.8 mail.ru -- DENY' | tee -a ${TMP}
- timeout ${TOS} dig @8.8.8.8 mail.ru >> ${RES_TXT}
- echo 'Should Deny : ' >> ${TMP}
- if grep -q '169.229.49.130' "${RES_TXT}"; then
- SCORE=$((SCORE+1))
- echo 'True'>> ${TMP}
- else
- echo 'False'>> ${TMP}
- fi
- cat ${RES_TXT} >> ${TMP}
- echo '' >> ${TMP}
- # test 3: should pass
- > ${RES_TXT}
- echo 'test 2: dig @8.8.8.8 google.com -- PASS' | tee -a ${TMP}
- timeout ${TOS} dig @8.8.8.8 google.com >> ${RES_TXT}
- LC="$(wc -l ${RES_TXT})"
- echo 'Should Pass : ' >> ${TMP}
- if grep -q "169.229.49.130" "${RES_TXT}"; then
- echo 'False'>> ${TMP}
- elif grep -q ";; ANSWER SECTION:" "${RES_TXT}"; then
- SCORE=$((SCORE+1))
- echo 'True'>> ${TMP}
- else
- echo 'False'>> ${TMP}
- fi
- cat ${RES_TXT} >> ${TMP}
- echo '' >> ${TMP}
- ### TCP
- # test 4: should deny
- > ${RES_TXT}
- echo "test 4: wget 151.101.65.80 -q -O - -- DENY" | tee -a ${TMP}
- timeout ${TOS} wget 151.101.65.80 -q -O - >> ${RES_TXT}
- RET=$?
- LC="$(wc -l ${RES_TXT})"
- echo 'Should Deny : ' >> ${TMP}
- # timeout err code is 124
- if [ "${RET}" == "124" ]; then
- echo "False: timeout (didn't deny)" >> ${TMP}
- elif [ "${LC}" == "0 ${RES_TXT}" ]; then
- SCORE=$((SCORE+1))
- echo 'True'>> ${TMP}
- else
- echo 'False'>> ${TMP}
- fi
- cat ${RES_TXT} >> ${TMP}
- echo '' >> ${TMP}
- # test 5: should deny
- > ${RES_TXT}
- echo "test 5: wget https://code.jquery.com/jquery-3.3.1.min.js -- DENY" | tee -a ${TMP}
- timeout ${TOS} wget https://code.jquery.com/jquery-3.3.1.min.js >> ${RES_TXT} 2>> ${RES_TXT}
- RET=$?
- LC="$(wc -l ${RES_TXT})"
- echo 'Should Deny : ' >> ${TMP}
- # timeout err code is 124
- if [ "${RET}" == "124" ]; then
- SCORE=$((SCORE+1))
- echo 'True'>> ${TMP}
- else
- echo 'False'>> ${TMP}
- fi
- cat ${RES_TXT} >> ${TMP}
- echo '' >> ${TMP}
- ### HTTP LOG
- # test 6: should log
- > ${RES_TXT}
- echo "test 6: curl http://api.openweathermap.org/data/2.5/weather -- LOG" | tee -a ${TMP}
- timeout ${TOS} curl http://api.openweathermap.org/data/2.5/weather >> ${RES_TXT} 2>> ${RES_TXT}
- RET=$?
- LC="$(wc -l ${RES_TXT})"
- echo 'Should Log : ' >> ${TMP}
- # timeout err code is 124
- if grep -q "api.openweathermap.org" "${HTTP_LOG}"; then
- SCORE=$((SCORE+1))
- echo 'Log1: True'>> ${TMP}
- else
- echo 'Log1: False'>> ${TMP}
- fi
- if grep -q "GET" "${HTTP_LOG}" && grep -q "HTTP/1.1" "${HTTP_LOG}"; then
- SCORE=$((SCORE+1))
- echo 'Log2: True'>> ${TMP}
- else
- echo 'Log2: False'>> ${TMP}
- fi
- cat ${RES_TXT} >> ${TMP}
- echo '' >> ${TMP}
- # sleep 1
- sudo pkill python
- }
- prepare_workspace(){
- TAG=$1
- # empty temporary file
- > ${TMP}
- # cd to bypass mode dir
- cd ${BYPASS_DIR}
- # launch bypass mode to have git access
- sudo ./main.py --mode bypass > /dev/null &
- # save PID
- EX_ID=$!
- # cd to workspace
- cd -
- # checkout to stateless branch
- git checkout tags/${TAG} >> ${TMP} &>> ${TMP}
- # kill bypass mode
- sudo pkill python
- # check if exists
- GIT_ERR_MSG="error: pathspec 'tags/${TAG}' did not match any file(s) known to git."
- if grep -q "${GIT_ERR_MSG}" "${TMP}"; then
- HAS_DONE=0
- fi
- }
- for DIR in ./*/; # list directories in the form "/tmp/dirname/"
- do
- SCORE=0
- # evaluate dir name
- DIR=${DIR%*/} # remove the trailing "/"
- DIR=${DIR##*/}
- cd ${DIR}
- echo "Testing : ${DIR}"
- # Write student info into file
- echo "-----------------------------------" >> ../${LOG_FILE}
- echo -e "Student: ${DIR} \n" >> ../${LOG_FILE}
- # copy starter files
- cp ../../${STARTER_DIR}/* .
- # create temporary file for loging student's work
- TMP=../tmp.txt
- ### STATELESS ###
- HAS_DONE=1
- TAG='stateful'
- prepare_workspace ${TAG}
- if [ ${HAS_DONE} -eq 0 ]; then
- echo "${TAG} Homework Not Done..." >> ../${LOG_FILE}
- else
- test_firewall
- fi
- # Write temporary file to LOG file
- cat ${TMP} >> ../${LOG_FILE}
- # Write Results
- echo '' >> ../${LOG_FILE}
- echo "Score : ${SCORE}/${MAX_SCORE}" >> ../${LOG_FILE}
- echo "Total score: $(((SCORE*100 /MAX_SCORE )))" >> ../${LOG_FILE}
- echo ""
- cd ..
- done;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement