Wednesday, August 26, 2020

PHP Shell Exec

Prerequisite:

# Remote ssh command: Install sshpass first and copy the known_hosts file that belongs to the sysad user (/home/sysad/.ssh/known_hosts to the /var/www/.ssh/known_hosts.  Take note that the password is in single quote or it will error with permission denied.
# Add the sysad user to the /etc/group
  • adm:x:4:administrator,syslog,sysad
# Add to the last line of the sudoers file: sudo visudo
  • sysad ALL=(ALL) NOPASSWD: ALL

PHP file: test.php

<?php
$output = shell_exec("./whoami.sh 2>&1");
echo "<pre>$output</pre>";
?>
Note: must require the 2>&1 for the shell_exec to work on commands considered external.

Shell Script: whoami.sh
Content:

#!/bin/bash
whoami
sshpass -p 'password' ssh -o StrictHostKeyChecking=no -T sysad@serverIP 'ls'

Make executable
chmod +x whoami.sh

Monday, October 14, 2019

Redmine Installation on Ubuntu 19.04

Reference:
https://www.hiroom2.com/2019/06/17/ubuntu-1904-redmine-en/

File: redmineInstall.sh
#!/bin/sh -e

MYSQL_VERSION=5.7
[ -z "${MYSQL_PASSWD}" ] && MYSQL_PASSWD=mysql
[ -z "${REDMINE_PASSWD}" ] && REDMINE_PASSWD=redmine

mysql_install()
{
  cat <<EOF | sudo debconf-set-selections
mysql-server-${MYSQL_VERSION} mysql-server/root_password password \
${MYSQL_PASSWD}
mysql-server-${MYSQL_VERSION} mysql-server/root_password_again password \
${MYSQL_PASSWD}
EOF
  sudo apt install -y mysql-server
}

redmine_install()
{
  cat <<EOF | sudo debconf-set-selections
redmine redmine/instances/default/dbconfig-install boolean true
redmine redmine/instances/default/database-type select mysql
redmine redmine/instances/default/mysql/admin-pass password ${MYSQL_PASSWD}
redmine redmine/instances/default/password-confirm password ${MYSQL_PASSWD}
redmine redmine/instances/default/mysql/app-pass password ${REDMINE_PASSWD}
redmine redmine/instances/default/app-password-confirm password \
${REDMINE_PASSWD}
EOF
  sudo apt install -y redmine-mysql
}

apache_install()
{
  sudo apt install -y apache2 libapache2-mod-passenger bundler

  # Overwrite passenger.conf.
  cat << EOF | sudo tee /etc/apache2/mods-available/passenger.conf
<IfModule mod_passenger.c>
  PassengerRoot /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini
  PassengerDefaultRuby /usr/bin/ruby
  PassengerDefaultUser www-data
  RailsBaseURI /redmine
</IfModule>
EOF

  cd /var/www/html
  sudo ln -s /usr/share/redmine/public redmine
  sudo chown -R www-data:www-data /usr/share/redmine
  cat << EOF | sudo tee /etc/apache2/sites-available/redmine.conf
<VirtualHost _default_:443>
  SSLEngine on
  SSLCertificateFile    /etc/ssl/certs/ssl-cert-snakeoil.pem
  SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key

  <Directory /redmine>
    Options FollowSymLinks
    PassengerResolveSymlinksInDocumentRoot on
    AllowOverride None
  </Directory>
</VirtualHost>
EOF

  sudo a2enmod passenger
  sudo a2enmod ssl
  sudo a2ensite redmine

  sudo systemctl enable apache2
  sudo systemctl restart apache2
}

redmine_main()
{
  mysql_install
  redmine_install
  apache_install
}

redmine_main


To access 

http://server_ip/redmine

Wednesday, October 9, 2019

QR Code with Text Overlay

Prequisite:
sudo apt install python-pip
sudo pip install image
sudo pip install qrcode
sudo pip install fonts
sudo apt-get install ttf-mscorefonts-installer

To test using imagemagick
sudo apt install imagemagick


Python code: test.py
from PIL import Image, ImageDraw,ImageFont
import qrcode

qr = qrcode.QRCode(
version=3,
error_correction=qrcode.constants.ERROR_CORRECT_Q,
box_size=10,
border=4,
)

image = Image.new( "RGBA", ( 410, 480 ) ,"white");

qr.add_data('https://example.com/stuff/rack/1')
qr.make(fit=True)

img = qr.make_image()
print img.size
image.paste(img, (0,0), img.convert("RGBA") );


draw = ImageDraw.Draw(image)
font = ImageFont.truetype("/usr/lib/cinelerra/fonts/arial.ttf",60)
#font = ImageFont.load_default()
txt = "rack 1"
draw.text((100, 410), txt, (0,0,0), font=font)


image.save("qr.png")


To run:
python test.py
display qr.png

Friday, September 13, 2019

Compare two files


MAKE sure you sort the files first
sort file_unsorted > file1
sort file_unsorted2 > file2
The comm command (short for "common") may be useful comm - compare two sorted files line by line
#find lines only in file1
comm -23 file1 file2 

#find lines only in file2
comm -13 file1 file2 

#find lines common to both files
comm -12 file1 file2 

Wednesday, August 14, 2019

Delegation Domain Name

Let's say you need to redirect a website within your domain to an external host.  Instead of creating a webserver and redirecting the traffic.  In some case such as being on the same domain and wanting to redirect to an external name yet it includes your domain.

example: your domain is example.com.  You want www.example.com yet it is hosted by somewhereelse.com.  somewhereelse.com has a c-name of www.example.com

Solution is to add a delegation under your forward lookup zone.  Add two new name server: resolver1.opendns.com and resolver2.opendns.com