command_postdetails.sh 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #!/bin/bash
  2. # LGSM command_postdetails.sh function
  3. # Author: CedarLUG
  4. # Contributor: CedarLUG
  5. # Website: https://gameservermanagers.com
  6. # Description: Strips sensitive information out of Details output
  7. local commandname="postdetails"
  8. local commandaction="Postdetails"
  9. local function_selfname="$(basename $(readlink -f "${BASH_SOURCE[0]}"))"
  10. # postdetails variable affects the output of command_details.sh. Setting
  11. # it here silences the output from sourcing command_details.sh.
  12. postdetails=yes
  13. # Set posttarget to the appropriately-defined post destination.
  14. # The options for posttarget are:
  15. # The default destination - hastebin
  16. # posttarget="https://hastebin.com"
  17. #
  18. # Secondary destination - pastebin
  19. # posttarget="http://pastebin.com
  20. #
  21. # Third option - leave on the filesystem
  22. # posttarget=
  23. #
  24. # All of these options can be specified/overridden from the top-level
  25. # invocation, as in:
  26. # rustserver@gamerig:~$ posttarget="http://pastebin.com" ./rustserver pd
  27. # to post to pastebin, or
  28. # rustserver@gamerig:~$ posttarget= ./rustserver pd
  29. # to leave the output on the filesystem.
  30. posttarget=${posttarget="https://hastebin.com"}
  31. # For pastebin, you can set the expiration period.
  32. # use 1 week as the default, other options are '24h' for a day, etc.
  33. # This, too, may be overridden from the command line at the top-level
  34. postexpire="${postexpire="30D"}"
  35. # This file sources the command_details.sh file to leverage all
  36. # of the already-defined functions. To keep the command_details.sh
  37. # from actually producing output, the main executable statements have
  38. # been wrapped in the equivalent of an ifdef clause, that looks
  39. # for the variable "postdetails" to be defined. -CedarLUG
  40. # source all of the functions defined in the details command
  41. command_details.sh
  42. # redefine as command_details.sh changes them
  43. local commandname="postdetails"
  44. local commandaction="Postdetails"
  45. local function_selfname="$(basename $(readlink -f "${BASH_SOURCE[0]}"))"
  46. fn_bad_tmpfile() {
  47. echo "There was a problem creating a temporary file ${tmpfile}."
  48. core_exit.sh
  49. }
  50. # Rather than a one-pass sed parser, default to using a temporary directory
  51. tmpfile="${tmpdir}/postdetails-$(date +"%Y-%d-%m_%H-%M-%S").tmp"
  52. touch "${tmpfile}" || fn_bad_tmpfile
  53. # fn_display_details is found in the command_details.sh file (which
  54. # was sourced above). The output is parsed for passwords and other
  55. # confidential information. -CedarLUG
  56. # The numerous sed lines could certainly be condensed quite a bit,
  57. # but they are separated out to provide examples for how to add
  58. # additional criteria in a straight-forward manner.
  59. # (This was originally a sed one-liner.) -CedarLUG
  60. fn_display_details | sed -e 's/password="[^"]*/password="--stripped--/' |
  61. sed -e 's/password "[^"]*/password "--stripped--/' |
  62. sed -e 's/password: .*/password: --stripped--/' |
  63. sed -e 's/gslt="[^"]*/gslt="--stripped--/' |
  64. sed -e 's/gslt "[^"]*/gslt "--stripped--/' |
  65. sed -e 's/pushbullettoken="[^"]*/pushbullettoken="--stripped--/' |
  66. sed -e 's/pushbullettoken "[^"]*/pushbullettoken "--stripped--/' |
  67. sed -e 's/authkey="[^"]*/authkey="--stripped--/' |
  68. sed -e 's/authkey "[^"]*/authkey "--stripped--/' |
  69. sed -e 's/authkey [A-Za-z0-9]\+/authkey --stripped--/' |
  70. sed -e 's/rcts_strAdminPassword="[^"]*/rcts_strAdminPassword="--stripped--/' |
  71. sed -e 's/rcts_strAdminPassword "[^"]*/rcts_strAdminPassword "--stripped--/' |
  72. sed -e 's/sv_setsteamaccount [A-Za-z0-9]\+/sv_setsteamaccount --stripped--/' |
  73. sed -e 's/sv_password="[^"]*/sv_password="--stripped--/' |
  74. sed -e 's/sv_password "[^"]*/sv_password "--stripped--/' |
  75. sed -e 's/zmq_stats_password="[^"]*/zmq_stats_password="--stripped--/' |
  76. sed -e 's/zmq_stats_password "[^"]*/zmq_stats_password "--stripped--/' |
  77. sed -e 's/zmq_rcon_password="[^"]*/zmq_rcon_password="--stripped--/' |
  78. sed -e 's/zmq_rcon_password "[^"]*/zmq_rcon_password "--stripped--/' |
  79. sed -e 's/pass="[^"]*/pass="--stripped--/' |
  80. sed -e 's/pass "[^"]*/pass "--stripped--/' |
  81. sed -e 's/rconServerPassword="[^"]*/rconServerPassword="--stripped--/' |
  82. sed -e 's/rconServerPassword "[^"]*/rconServerPassword "--stripped--/' > "${tmpfile}"
  83. # strip off all console escape codes (colorization)
  84. sed -i -r "s/[\x1B,\x0B]\[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]//g" "${tmpfile}"
  85. # If the gameserver uses anonymous steam credentials, leave them displayed
  86. # in the output. Otherwise, strip these out as well.
  87. if ! grep -q "^steampass[= ]\"\"" "${tmpfile}" ; then
  88. sed -i -e 's/steampass[= ]"[^"]*/steampass "--stripped--/' "${tmpfile}"
  89. fi
  90. if ! grep -q "^steamuser[= ]\"anonymous\"" "${tmpfile}" ; then
  91. sed -i -e 's/steamuser[= ]"[^"]*/steamuser "--stripped--/' "${tmpfile}"
  92. fi
  93. if [ "${posttarget}" == "http://pastebin.com" ] ; then
  94. fn_print_dots "Posting details to pastbin.com for ${postexpire}"
  95. sleep 1
  96. # grab the return from 'value' from an initial visit to pastebin.
  97. csrftoken=$(curl -s "${posttarget}" |
  98. sed -n 's/^.*input type="hidden" name="csrf_token_post" value="\(.*\)".*$/\1/p')
  99. #
  100. # Use the csrftoken to then post the content.
  101. #
  102. link=$(curl -s "${posttarget}/post.php" -D - -F "submit_hidden=submit_hidden" \
  103. -F "post_key=${csrftoken}" -F "paste_expire_date=${postexpire}" \
  104. -F "paste_name=${gamename} Debug Info" \
  105. -F "paste_format=8" -F "paste_private=0" \
  106. -F "paste_type=bash" -F "paste_code=<${tmpfile}" |
  107. awk '/^location: / { print $2 }' | sed "s/\n//g")
  108. # Output the resulting link.
  109. fn_print_ok_nl "Posting details to pastbin.com for ${postexpire}"
  110. echo " Please share the following url for support: ${posttarget}${link}"
  111. elif [ "${posttarget}" == "https://hastebin.com" ] ; then
  112. fn_print_dots "Posting details to hastebin.com"
  113. sleep 1
  114. # hastebin is a bit simpler. If successful, the returned result
  115. # should look like: {"something":"key"}, putting the reference that
  116. # we need in "key". TODO - error handling. -CedarLUG
  117. link=$(curl -H "HTTP_X_REQUESTED_WITH:XMLHttpRequest" -s -d "$(<${tmpfile})" "${posttarget}/documents" | cut -d\" -f4)
  118. fn_print_ok_nl "Posting details to hastebin.com for ${postexpire}"
  119. echo " Please share the following url for support: ${posttarget}/${link}"
  120. else
  121. fn_print_warn_nl Review the output in "${tmpfile}"
  122. core_exit.sh
  123. fi
  124. # cleanup
  125. rm "${tmpfile}" || /bin/true
  126. core_exit.sh