Misc scripting

From packets2photons
Jump to navigation Jump to search

This script will pick a random number between 0 and 15 and do a sha hash of it and insert the output in a file.

#!/bin/bash

random()
{
    local range=${1:-1}

    RAND=`od -t uI -N 4 /dev/urandom | awk '{print $2}'`
    let "RAND=$RAND%($range+1)"
}
random 15
hash="$(echo "$RAND" | shasum |  sed 's/ .*//')"
echo "Theflagis{$hash}"

Some user number based hashing

#!/bin/bash

random()
{
    local range=${1:-1}

    let "RAND=$UID%($range+1)"
}
random 15
hash="$(echo "$RAND" | shasum |  sed 's/ .*//')"
echo $USER"Flag{$hash}"

Generate Accounts

First, create a group called competitors:

sudo groupadd competitors

This script will generate user accounts and insert public keys where the .csv format is:

username,ssh-rsa long_rand_nthoeuntoehu_pub_key root@archbox
#!/bin/bash

input="test_key_responses.csv"
while IFS=',' read -r f1 f2
do
  adduser --disabled-password --force-badname --gecos student "$f1"
  mkdir /home/"$f1"
  chown "$f1" /home/"$f1"/
  mkdir /home/"$f1"/.ssh
  chown "$f1" /home/"$f1"/.ssh
  echo "$f2" > /home/"$f1"/.ssh/authorized_keys
  cp /home/dave/.unreadable /home/"$f1"/
  chown "$f1" /home/"$f1"/.unreadable
  echo "/usr/games/cowsay -f /usr/share/cowsay/cows/default.cow Hi I am $HOSTNAME" > /home/"$f1"/.profile
  echo "echo "" " >> /home/"$f1"/.profile
  echo "/bin/userhash" >> /home/"$f1"/.profile
  echo "echo "" " >> /home/"$f1"/.profile
  usermod -a -G competitors "$f1"
  echo "User: $f1" 
  echo "Key: $f2"
done < "$input"

Removing Accounts

This script will generate user accounts and insert public keys where the .csv format is:

username,ssh-rsa long_rand_nthoeuntoehu_pub_key root@archbox
#!/bin/bash

input="test_key_responses.csv"
while IFS=',' read -r f1 f2
do
  deluser "$f1"
  rm -rf /home/"$f1"/
done < "$input"

Basic user siloing in multi user systems

Edit the following in: /etc/security/limits.conf

#<domain>      <type>  <item>         <value>
#
@competitors        hard    nproc         40
@competitors        hard    cpu           3
#*        hard    fsize         10000
#*        hard    core          unlimited

Before we generate accounts it would be a good idea to set a sensible UMASK for the users:

sudo vim /etc/login.defs
Change the UMASK to 077