Skip to content

Commit

Permalink
Fix bash script to work on various nc variants
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewDryga committed Jul 22, 2024
1 parent 90ba095 commit 23fdea2
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/probe/controllers/run.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule Probe.Controllers.Run do

def start(conn, %{"token" => token}) do
with {:ok, %{session_id: session_id, pid: pid, port: port}} <- Token.verify(token),
true <- Process.alive?(pid) do
true <- is_pid(pid) and Process.alive?(pid) do
remote_ip = get_client_ip(conn)
anonymized_id = get_anonymized_id(session_id, remote_ip)
{city, region, country, latitude, longitude, provider} = geolocate_ip(remote_ip)
Expand Down
64 changes: 60 additions & 4 deletions priv/static/scripts/unix.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
#!/bin/sh
#!/usr/bin/env sh

set -e
set -ex

payload_interval=0.2

# Check for required commands
for cmd in base64 sleep curl; do
if ! command -v "$cmd" >/dev/null 2>&1; then
echo "Error: $cmd command not found"
exit 1
fi
done

# This script is intended to run from https://probe.sh and requires a valid
# token to start. NOTE: Unfortunately only IPv4 is supported at this time.
start_url=$1
Expand All @@ -19,9 +27,57 @@ trap cancel EXIT

# Function to send payloads
send_payload() {
payload=$1
payload="$1"

# Determine the appropriate base64 option for decoding
base64_option="-d"
if base64 --help 2>&1 | grep -q "D"; then
base64_option="-D"
fi

# Determine the appropriate nc options
nc_cmd="nc"
nc_options="-4 -u -w 0"

# Check for GNU netcat
if nc --help 2>&1 | grep -q "GNU"; then
echo "GNU netcat detected"
nc_options="-u -c"

# Check for OpenBSD netcat
elif nc -h 2>&1 | grep -q "OpenBSD"; then
echo "OpenBSD netcat detected"
# Keep the default options

# Check for ncat from Nmap
elif nc -h 2>&1 | grep -q "Ncat"; then
echo "Nmap netcat (ncat) detected"
nc_cmd="ncat"
nc_options="--udp --send-only"

elif netcat --help 2>&1 | grep -q "GNU"; then
echo "GNU netcat detected"
nc_cmd="netcat"
nc_options="-u -c"

# Check for OpenBSD netcat
elif netcat -h 2>&1 | grep -q "OpenBSD"; then
echo "OpenBSD netcat detected"
nc_cmd="netcat"
# Keep the default options

# If none of the above, keep the default options
fi

if ! command -v "$nc_cmd" >/dev/null 2>&1; then
echo "Error: $nc_cmd command not found"
exit 1
fi

# Loop to send the payload
for i in 1 2 3; do
echo "$payload" | base64 -d | nc -u -w 0 "$host" "$port"
echo "Using $nc_cmd with options: $nc_options"
echo "$payload" | base64 "$base64_option" | $nc_cmd $nc_options "$host" "$port"
sleep $payload_interval
done
}
Expand Down

0 comments on commit 23fdea2

Please sign in to comment.