Advertisement
Axion34

bash netcat

Feb 24th, 2014
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.36 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. RCON_AUTH=3
  4. RCON_CMD=2
  5.  
  6. declare -A decode_msg
  7.  
  8. function switchEndian
  9. {
  10.   printf %s ${1} | grep -o .. | tac | tr -d '\n'
  11.   return 0
  12. }
  13.  
  14. function intToHex
  15. {
  16.   printf %.8X ${1}
  17.   return 0
  18. }
  19.  
  20. function hexToInt
  21. {
  22.   echo "ibase=16; "${1} | bc
  23.   return 0
  24. }
  25.  
  26. function intToHex_SE
  27. {
  28.   echo $(switchEndian $(intToHex ${1}))
  29.   return 0
  30. }
  31.  
  32. function hexToInt_SE
  33. {
  34.   echo $(hexToInt $(switchEndian ${1}))
  35.   return 0
  36. }
  37.  
  38. function helper
  39. {
  40.   echo ${1} | sed -e 's/\(..\)/\\x\1/g'
  41.   return 0
  42. }
  43.  
  44. function rcon_build_msg
  45. {
  46.   # {1} = ident
  47.   # {2} = type
  48.   # {3} = payload
  49.  
  50.   if [[ "${#@}" -ne 3 ]]
  51.   then
  52.     echo "Error using rcon_build_msg" >&2
  53.     return 1
  54.   fi
  55.  
  56.   local msg_size=$(helper $(intToHex_SE $(( 10 + ${#3}))))
  57.   local msg_body=$(helper $(intToHex_SE "${1}"))$(helper $(intToHex_SE "${2}"))"${3}""\\00\\00"
  58.  
  59.   echo "${msg_size}""${msg_body}"
  60.   return 0
  61. }
  62.  
  63. function rcon_sendMsg
  64. {
  65.   # ${1} = message
  66.  
  67.   if [[ "${#@}" -ne 1 ]]
  68.   then
  69.     echo "Error rcon_sendMsg" >&2
  70.     return 1
  71.   fi
  72.  
  73.   echo -ne "${1}" >"${rcon_pIn}"
  74.   return 0
  75. }
  76.  
  77. function rcon_readByte
  78. {
  79.   dd if=${rcon_pOut} bs=1 count=${1} 2>/dev/null | xxd -ps  | tr 'a-z' 'A-Z'
  80.   return 0
  81. }
  82.  
  83. function rcon_readMsg
  84. {
  85.   local msgSize=$(rcon_readByte "4")
  86.   local msgBody=$(rcon_readByte $(hexToInt_SE ${msgSize}))
  87.   echo ${msgSize}${msgBody}
  88.   return 0
  89. }
  90.  
  91. function rcon_decodeMsg
  92. {
  93.   if [[ "${#@}" -ne 1 ]]
  94.   then
  95.     return 1
  96.   fi
  97.  
  98.   decode_msg["size"]=$(hexToInt_SE ${1:0:8})
  99.   decode_msg["ident"]=$(hexToInt_SE ${1:8:8})
  100.   decode_msg["type"]=$(hexToInt_SE ${1:16:8})
  101.   decode_msg["payload"]=$( echo ${1:24:-4} | xxd -p -r )
  102.  
  103.   return 0
  104. }
  105.  
  106. function rcon_connect
  107. {
  108.   # {1} = host
  109.   # {2} = port
  110.   # {3} = password
  111.  
  112.   ident=42
  113.   local msg=""
  114.   local response=""
  115.  
  116.   if [[ "${#@}" -ne 3 ]]
  117.   then
  118.     echo "Usage: rcon_connect host port password" >&2
  119.   fi
  120.  
  121.   netcat -nq 1 "${1}" "${2}" <"${rcon_pIn}" >"${rcon_pOut}" &
  122.   local res=${!}
  123.  
  124.   exec {hold_pIn}>"${rcon_pIn}"
  125.   exec {hold_pOut}<"${rcon_pOut}"
  126.  
  127.   ps -p "${res}" >/dev/null
  128.  
  129.   if [[ $? -ne 0 ]]
  130.   then
  131.     echo "Error netcat" >&2
  132.     return 1
  133.   fi
  134.  
  135.   msg=$(rcon_build_msg "${ident}" "${RCON_AUTH}" "${3}")
  136.   rcon_sendMsg "${msg}"
  137.   response=$(rcon_readMsg)
  138.   rcon_decodeMsg "${response}"
  139.  
  140.   if [[ ${decode_msg["ident"]} -ne "${ident}" ]]
  141.   then
  142.     echo "Authentification failed" >&2
  143.     return 1
  144.   fi
  145.   return 0
  146. }
  147.  
  148. rcon_pIn=$(mktemp -u)
  149. mkfifo "${rcon_pIn}"
  150.  
  151. rcon_pOut=$(mktemp -u)
  152. mkfifo "${rcon_pOut}"
  153.  
  154. ident=0
  155. response=""
  156.  
  157. rcon_connect "127.0.0.1" "25575" "password"
  158. if [[ "${?}" -eq 0 && "${ident}" -gt 0 ]]
  159. then
  160.   msg=$(rcon_build_msg "${ident}" "${RCON_CMD}" "save-off")
  161.   rcon_sendMsg "${msg}"
  162.   response=$(rcon_readMsg)
  163.   rcon_decodeMsg "${response}"
  164.   echo ${decode_msg["payload"]}
  165.  
  166.   if [[ "${decode_msg["payload"]}" == "Disabled"* ]]
  167.   then
  168.     msg=$(rcon_build_msg "${ident}" "${RCON_CMD}" "save-all")
  169.     rcon_sendMsg "${msg}"
  170.     response=$(rcon_readMsg)
  171.     rcon_decodeMsg "${response}"
  172.     echo "${decode_msg["payload"]}"
  173.  
  174.     if [[ "${decode_msg["payload"]}" == *"Save complete." ]]
  175.     then
  176.       echo "TODO: BACKUP"
  177.     fi
  178.  
  179.     msg=$(rcon_build_msg "${ident}" "${RCON_CMD}" "save-on")
  180.     rcon_sendMsg "${msg}"
  181.     response=$(rcon_readMsg)
  182.     rcon_decodeMsg "${response}"
  183.     echo "${decode_msg["payload"]}"
  184.   fi
  185. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement