Labels

Linux (28) Python (13) Raspberry Pi (5) Bugs (3) Install (3) C (2) Brainf**k (1) HTML (1) Maths (1) Sorts (1)

Tuesday, 13 December 2016

How to send emails in Python

We are going to be using the yagmail library to send a email using python.

To start of we will need to install the yagmail python library.
pip install yagmail

If you don't have pip installed you can install using the following commands depending on your OS.

Debian systems:
sudo apt-get install python-pip

RedHat/Fedora
sudo yum upgrade python-setuptools
sudo yum install python-pip python-wheel


Arch Linux:
sudo pacman -s python2-pip

openSUSE:
sudo zypper install python-pip python-setuptools python-wheel

Now we will test that the library has been installed with a simple python script.

#!/usr/bin/python
import yagmail


Run this script and if it produces no errors you are good to go.

Now we can get started sending emails.

First we need to import the necessary libraries(yagmail, time).
import yagmail
import time


Now lets setup the variables that are going to be sent in the email
subject = "Sent from Python at " + time.strftime("%D:%M:%Y")
message = "Hello, World!"
sender_email = "YOUR_EMAIL"
sender_password = "YOUR_PASSWORD"
recipient_email = "RECIPIENT_EMAIL"


Now lets send the email
yag = yagmail.SMTP(sender_email, sender_password)
yag.send(recipient_email, subject, message)


Putting all of that together we get:
import yagmail
import time

subject = "Sent from Python at " + time.strftime("%D:%M:%Y")
message = "Hello, World!"
sender_email = "YOUR_EMAIL"
sender_password = "YOUR_PASSWORD"
recipient_email = "RECIPIENT_EMAIL"

yag = yagmail.SMTP(sender_email, sender_password)
yag.send(recipient_email, subject, message)


If we run this script you should have a sent a email using less than 10 lines of python.

Link to yagmail GitHub

Saturday, 10 December 2016

Buffer Overflow Variable writing in C



Buffer overflows usually stem from code that has been badly written and not included the necessary checks to prevent them.



Vulnerable code:
#include
int main(){
        char buffer[5];
        char a[2];
        char b[2];
        printf("Enter a string: ");
        gets(buffer);
        printf("Contents of buffer:%s\n",buffer);
        printf("Contents of a:%c\n",a);
        printf("Contents of b:%c\n",b);
}
To compile this code use this command
gcc bufferOverflow.c -fno-stack-protector
Now lets try inputting 5 characters:
Enter a string:aaaaa
Contents of buffer:aaaaa
Contents of a:
Contents of b:
At the moment it is running well without any errors and not overflowing into the variables a and b, this is because we are not exceeding the size of the buffbuffer overflower.

Lets increase it to 6:
Enter a string:aaaaa
Contents of buffer:aaaaa
Contents of a:
Contents of b:
a and b are still empty.

After some trial and error I managed to write into the variable b using 15 characters.
Enter a string: aaaaaaaaaaaaaaa
Contents of buffer:aaaaaaaaaaaaaaa
Contents of a:
Contents of b:a
Using 16 characters I have managed to indirectly write into the memory which holds the values of a and b.

If you input more characters at some point you will cause a segmentation fault as the program is trying to access memory that it doesn't have access to.
Enter a string: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Contents of buffer:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Contents of a:a
Contents of b:a
Segmentation fault (core dumped)
To fix the code we need to change the gets function to fgets.
#include
int main(){
        char buffer[5];
        char a;
        char b;
        printf("Enter a string: ");
        fgets(buffer, sizeof(buffer), stdin);
        printf("Contents of buffer:%s\n",buffer);
        printf("Contents of a:%c\n",a);
        printf("Contents of b:%c\n",b);
}
Now no matter what we enter to the program it will never write more characters than the size of the buffer to the buffer.
Enter a string: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Contents of buffer:aaaa
Contents of a:
Contents of b:
Only 4 characters are printed as fgets includes the newline character at the end of the input.

Friday, 9 December 2016

Shrink a PDF on linux

You can shrink a pdf using this command:


ps2pdf -dPDFSETTINGS=/"OPTION" BiggerPdf.pdf SmallerPDF.pdf


To change the amount of compression change "Option" to:
screen - Low quality
ebook - Med quality
printer - High quality

Monday, 5 December 2016

Creating a excutable shell script

When writing shell scripts you can use .sh file extension and run using the sh command.
Example:
sh helloWorld.sh

You can make your shell script executable and run using ./
Example:
./helloWorld.sh

To make it like this we need to do 2 things:
Change the file permissions
Define the interpreter/shell we will be using in the script.

Changing the file permissions:
Run the command:
chmod 0755 helloWorld.sh
This will allow all users to run the script

To only allow the current user to run the script run:
chmod 0700 helloWorld.sh

Defining the shell within the script:
Add the following line to the start of the script
#!/bin/sh
Or
#!/bin/bash
Depending on which one you are using.

Now you can run your script by typing
./helloWorld.sh