Skip to content

Instantly share code, notes, and snippets.

@zaafar
Last active January 23, 2024 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zaafar/1e0bcf3719449852fee3ce03758144cf to your computer and use it in GitHub Desktop.
Save zaafar/1e0bcf3719449852fee3ce03758144cf to your computer and use it in GitHub Desktop.
modify post/upload file on oc via automation
OC_USERNAME=${1}
OC_PASSWORD_HASH=${2}
OC_POST_ID=${3}
UPLOAD_FILE_PATHNAME=${4}
### Private Variables ###
__TMP_FOLDER="tmp"
__CONTENT_FILE="${__TMP_FOLDER}/__OC_POST_CONTENT.txt"
#########################
# Extract requested cookie from header file and convert it into global variable with same name
# Pass filename to look into as first argument
# Pass keywords in rest of the arguments
function oc_extract_cookie_from_header_file()
{
filename=$1;
for var in "${@:2}"
do
export $var=`grep $var $filename | sed "s/.*$var=//g;s/;.*//g;"`
done
}
# Extract variables from response file and convert it into global variable with same name
# Pass filename to look into as first argument
# Pass keywords in rest of the arguments
function oc_extract_variable_from_response_file()
{
filename=$1;
for var in "${@:2}"
do
export $var=`grep "var $var" $filename | sed 's/.* "//g;s/".*//g;'`
done
}
# Extracts content of EDIT post area and store it in a new file
# unfortunately I had to store it in a file since I don't know
# how to store multiple lines in single bash variable.
# Also extracts post hash and time variable
# Pass filename as first argument
# Pass OLD attachment detection pattern as 2nd argument
function oc_extract_content_from_response_file()
{
pattern=${2:-">>>"};
mkdir -p $__TMP_FOLDER;
cat $1 | \
awk 'BEGIN {
START_PRINTING=0
}
($0 ~ /BASH_EDITOR_editor"/) {
START_PRINTING=1
}
(START_PRINTING == 1) {
print $0
}
($0 ~ /<\/textarea>/ && START_PRINTING == 1) {
START_PRINTING=0
}' | \
sed 's/<\/textarea>//g;s/.*BASH_EDITOR_editor.*>//g;' | \
perl -n -mHTML::Entities -e ' ; print HTML::Entities::decode_entities($_) ;' \
> $__CONTENT_FILE
export POST_HASH=`grep "<posthash>" $1 | sed 's/.*<posthash>\|<\/posthash>//g;'`
export POST_STARTTIME=`grep "<poststarttime>" $1 | sed 's/.*<poststarttime>\|<\/poststarttime>//g;'`
export OLD_ATTACHMENT_ID=`grep "$pattern" $__CONTENT_FILE | sed 's/.*\[ATTACH\]//g;s/\[.*//g;'`
export SECURITYTOKEN=`grep securitytoken $1 | sed 's/.*securitytoken":"//g;s/".*//g;'`
}
# Extracts the uploaded file ID from the response file and convert it into global variable
# Pass response file pathname in 1st argument
# Pass uploaded file name in 2nd argument
function oc_extract_uploaded_file_id()
{
export UPLOADED_FILE_ID=`grep "<filename>$2" --after-context=3 $1 | grep "attachmentid" | sed 's/.*<attachmentid>//g;s/<\/attachmentid>.*//g;'`
}
# Login into the OC website and dump response + headers into a file
# Pass username as first argument
# Pass md5hash of the password as second argument
function oc_login_and_dump_to_file()
{
mkdir -p $__TMP_FOLDER;
curl --silent 'https://www.ownedcore.com/forums/login.php?do=login' \
-X POST \
-H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0' \
-H 'Content-Type: application/x-www-form-urlencoded' \
--data-raw "vb_login_username=$1&vb_login_md5password=$2&do=login&cookieuser=1" \
-o $__TMP_FOLDER/__oc_login_response.html \
-D $__TMP_FOLDER/__oc_login_headers.txt
}
# Get the main page of the OC website and dump response + headers into a file
# Pass ocf_userid cookie value as 1st argument
# Pass ocf_password cookie value as 2nd argument
function oc_get_page()
{
mkdir -p $__TMP_FOLDER
curl --silent 'https://www.ownedcore.com/' \
-H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0' \
-H "Cookie: ocf_userid=$1; ocf_password=$2" \
-o $__TMP_FOLDER/__oc_get_page_response.html \
-D $__TMP_FOLDER/__oc_get_page_headers.txt
}
# Get the post to modify and dump response + headers into a file.
# Pass ocf_userid cookie value as 1st argument
# Pass ocf_password cookie value as 2nd argument
# Pass post id as 3rd argument
# Pass securitytoken as 4th argument
function oc_edit_post_content()
{
mkdir -p $__TMP_FOLDER;
curl --silent "https://www.ownedcore.com/forums/ajax.php?do=quickedit&p=$3" \
-X POST \
-H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0' \
-H 'X-Requested-With: XMLHttpRequest' \
-H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' \
-H "Cookie: ocf_userid=$1; ocf_password=$2" \
--data-raw "securitytoken=$4&do=quickedit&p=$3&editorid=BASH_EDITOR" \
-o $__TMP_FOLDER/__oc_edit_post_response.html \
-D $__TMP_FOLDER/__oc_edit_post_headers.txt
}
# Save the post to modify and dump response + headers into a file.
# Pass ocf_userid cookie value as 1st argument
# Pass ocf_password cookie value as 2nd argument
# Pass post id as 3rd argument
# Pass securitytoken as 4th argument
# Pass poststarttime as 5th argument
# Pass posthash as 6th argument
function oc_save_post_content()
{
mkdir -p $__TMP_FOLDER;
curl --silent "https://www.ownedcore.com/forums/editpost.php?do=updatepost&postid=$3" \
-X POST \
-H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0' \
-H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' \
-H "Cookie: ocf_userid=$1; ocf_password=$2" \
--data-raw "securitytoken=$4&do=updatepost&postid=$3&poststarttime=$5&posthash=$6" \
--data-urlencode "message@$__CONTENT_FILE" \
-o $__TMP_FOLDER/__oc_save_post_response.html \
-D $__TMP_FOLDER/__oc_save_post_headers.txt
}
# Uploads a new file to OC post
# Pass ocf_userid cookie value as 1st argument
# Pass ocf_password cookie value as 2nd argument
# Pass post id as 3rd argument
# Pass securitytoken as 4th argument
# Pass attachment file path as 5th argument
# Pass poststarttime as 6th argument
# Pass posthash as 7th argument
function oc_upload_attachment()
{
mkdir -p $__TMP_FOLDER;
curl --silent "https://www.ownedcore.com/forums/newattachment.php" \
-X POST \
-H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0' \
-H "Cookie: ocf_userid=$1; ocf_password=$2" \
-F "do=manageattach" \
-F "upload=1" \
-F "securitytoken=$4" \
-F "poststarttime=$6" \
-F "posthash=$7" \
-F "contenttypeid=1" \
-F "MAX_FILE_SIZE=8388608" \
-F "values[p]=$3" \
-F "attachment[]=@$5" \
-F "ajax=1" \
-o $__TMP_FOLDER/__oc_upload_attachment_response.html \
-D $__TMP_FOLDER/__oc_upload_attachment_headers.txt
}
# Deletes the attachment from the OC post
# Pass ocf_userid cookie value as 1st argument
# Pass ocf_password cookie value as 2nd argument
# Pass post id as 3rd argument
# Pass securitytoken as 4th argument
# Pass attachment file id as 5th argument
# Pass poststarttime as 6th argument
# Pass posthash as 7th argument
function oc_delete_attachment()
{
mkdir -p $__TMP_FOLDER;
curl --silent "https://www.ownedcore.com/forums/newattachment.php" \
-X POST \
-H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0' \
-H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' \
-H "Cookie: ocf_userid=$1; ocf_password=$2" \
--data "do=manageattach&upload=0&securitytoken=$4&poststarttime=$6&posthash=$7&contenttypeid=1&MAX_FILE_SIZE=8388608" \
--data-urlencode "values[p]=$3" \
--data-urlencode "delete[$5]=1" \
-o $__TMP_FOLDER/__oc_delete_attachment_response.html \
-D $__TMP_FOLDER/__oc_delete_attachment_headers.txt
}
oc_login_and_dump_to_file $OC_USERNAME $OC_PASSWORD_HASH
oc_extract_cookie_from_header_file "$__TMP_FOLDER/__oc_login_headers.txt" "ocf_userid" "ocf_password"
oc_get_page $ocf_userid $ocf_password
oc_extract_variable_from_response_file "$__TMP_FOLDER/__oc_get_page_response.html" "SECURITYTOKEN"
oc_edit_post_content $ocf_userid $ocf_password $OC_POST_ID $SECURITYTOKEN
oc_extract_content_from_response_file "$__TMP_FOLDER/__oc_edit_post_response.html"
oc_upload_attachment $ocf_userid $ocf_password $OC_POST_ID $SECURITYTOKEN ${UPLOAD_FILE_PATHNAME} $POST_STARTTIME $POST_HASH
oc_extract_uploaded_file_id "$__TMP_FOLDER/__oc_upload_attachment_response.html" "$(basename -- ${UPLOAD_FILE_PATHNAME})"
sed -i "s/${OLD_ATTACHMENT_ID}/${UPLOADED_FILE_ID}/g;" $__CONTENT_FILE
oc_save_post_content $ocf_userid $ocf_password $OC_POST_ID $SECURITYTOKEN $POST_STARTTIME $POST_HASH
oc_delete_attachment $ocf_userid $ocf_password $OC_POST_ID $SECURITYTOKEN $OLD_ATTACHMENT_ID $POST_STARTTIME $POST_HASH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment