Friday, August 21, 2015

Convert Linux text file to Windows text file

DOS text files traditionally have carriage return and line feed pairs '\r\n' as their newline characters while Linux text files have the linefeed character, '\n', as their newline character.

To convert a Linux text file to a DOS text file do:

todos -b file.txt


The above command creates a backup of the original file, file.txt.bak, in addition to
converting file.txt to the DOS text file format.




Wednesday, August 19, 2015

Easy TCP/IP packet capture with tcpdump

Get list of interfaces that tcpdump can listen on:
tcpdump -D

Listen on interface eth0:

tcpdump -i eth0

Listen on interface eth0 and record the capure to a .pcap file:

tcpdump -w capture.pcap -i eth0

Display packet content of capture file capure.pcap:

tcpdump -r capture.cap

Limit the capure to 100 packets:

tcpdump -c 100 -i eth0

Display IP addresses and port numbers instead of domain and service names when capturing packes(some systems require -nn to be specified to display port numbers):

tcpdump -n -i eth0

Capture any packets where the destination host is 192.168.1.1. Display IP addresses and port numbers:

tcpdump -n dst host 192.168.1.1

Capure any packets where the src host is 192.168.1.1. Display IP addresses and port numbers:

tcpdump -n src host 192.168.1.1

Capure any packets where the source OR destination host is 192.168.1.1. Display IP addresses and port numbers:

tcpdump -n host 192.168.1.1

Capture any packets where the source or destination network is 192.168.1.0/24. Display IP addresses and port numbers:

tcpdump -n net 192.168.1.0/24

Capture any packets where the destination port is 23. Display IP addresses and port numbers:

tcpdump -n dst port 23

Capture any packets where the destination port is between 1 and 1023 inclusive. Display IP addresses and
port numbers:

tcpdump -n dst portrange 1-1023

Capture ONLY tcp packets where destination port is between 1 and 1023 inclusive:

tcpdump -n tcp dst portrange 1-1023

Capture ONLY udp packets where the destination port is between 1 and 1023 inclusive.

tcpdump -n udp dst portrange 1-1023



Capture any packets with destination IP 192.168.1.1 and destination port 23. Display IP addresses and port numbers:

tcpdump -n "dst host 192.168.1.1 and dst port 23"

Capture any packets with destination IP 192.168.1.1 and destination port 80 or 443. Display IP addresses and port numbers:

tcpdump -n "dst host 192.168.1.1 and (dst port 80 or dst port 443)"

Capture any ICMP packets:

tcpdump -v icmp

Capture any ARP packets:

tcpdump -v arp

Capture either ICMP or ARP packets:

tcpdump -v "icmp or arp"

Capture any packets that are broadcast ore multicast:

tcpdump -n "broadcast or multicast"

Capture 500 bytes of data for each packet rather than the default of 68 bytes:

tcpdump -s 500

Capture all bytes of data within the packet:

tcpdump -s 0



Monday, August 17, 2015

Mount usb to Raspberry Pi

Insert the usb-storage into an open usb port on the raspberrypi and issue

sudo fdisk -l

My usb-storage is displayed as /dev/sda1 and the filesystem is FAT32, but if your usb-storage has
 filesystem NTFS 3g you will need to install the package ntfs-3g:

sudo apt-get install ntfs-3g

Linux need to mount the usb-storage in order to access the folders and files on it.
The contents of the usb-storage will appear as a folder in /media. You can mount disks in other folders, but it's conventional to use /media. You need to create a directory where the mounted disk will appear in the media directory:




sudo mkdir /media/usbhdd

Change ownership of usb-storage to pi:

sudo chown pi:pi /media/usbhdd




Mount the usb-storage via:

sudo mount -t vfat -o uid=pi,gid=pi /dev/sda1 /media/usbhdd


The '-t vfat' tells the mount command that your drive has a fat32 file system. If your drive is formatted with NTFS, you should use '-t ntfs-3g' instead.

The '-o uid=pi,gid=pi' part of the command means that the disk will be owned by user pi.
You can use this command to unmount the disk:

sudo umount /media/usbhdd

Now you need to edit the file system table so that this disk is mounted every time your Raspberry Pi starts up:

sudo leafpad /etc/fstab &

You need to use sudo because the fstab file is owned by root. If you don't use sudo, you'll be able to open the file in leafpad, but you won't be able to save changes. The '&' means the command runs in the background, and you can keep using the terminal for other commands while leafpad is running. You should see something like this:

proc /proc proc defaults 0 0 /dev/mmcblk0p1 /boot vfat defaults 0 2 /dev/mmcblk0p2 / ext4 defaults,noatime 0 1


Add the following line, and save the file:

/dev/sda1 /media/usbhdd vfat uid=pi,gid=pi 0 0

Reboot your Pi via

sudo reboot

and you should be able to access your USB-storage via /media/usbhdd.

Some curl basic examples

The command line browser curl is most useful and one example is sending cookies via the b-directive:

curl  -b "name=daniel"  http://www.site.com

To send cookies stored in a file, assemble cookies in a file and run:

curl -b some-cookie-file http://www.site.com

To view a websites HTTP-response-headers do:

curl http://www.site.com --head

If the website is using redirects curl must have the L-flag set to follow the redirect:

curl -L http://www.redirect.com

You may want to test out a website that is yet not in the DNS. Then set a custom Host : header identifying
the server name you want to reach but use the target ip address in the url:

 curl --header "Host: www.site.com" http://127.0.0.1/


Specify a chosen user-agent :

curl -A "Mozilla/4.0" http://www.site.com


Do a  POST request:



curl -d "param1=value1&param2=value2" http://hostname/resource
 
GET request with XML:
 
curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET http://hostname/resource


File upload is done via:

curl --form "fileupload=@filename.txt" http://hostname/resource
 
Log in to a site and dump received headers to a file called headers:
 
curl -d "username=admin&password=admin&submit=Login" --dump-header headers http://site.com/Login
 
Use proxy:


curl -x proxy_ip:proxy_port http://www.site.com