Sunday, July 10, 2011

simple Port Scanner

Back in my sixth semester Computer Networks lab, we had to write a simple port scanner (port 0 to 1024). The basic principle was to see whether a connection could be made at a port. If connection can be made, it is open, or else it is closed. I had used the following code then. However, it took 17 minutes to perform the scan.

import java.io.IOException; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; public class port_scanner { static Socket soc; public static void main(String[] args) throws IOException { for(int i=1;i<1024;i++){ try{ soc= new Socket(InetAddress.getLocalHost(),i); System.out.println("port "+i+" open"); soc.close(); }catch(IOException e){ System.out.println("port "+i+" closed"); } } } }

The better option was to use threading. I was lazy back then and the above program was sufficient to fetch me marks, and so I did not write a multi-threaded code. I needed to brush up threading because placements are going to start soon. Therefore, I implemented the multi-threaded version. It took just 2 seconds to perform the scan.

import java.io.IOException; import java.net.InetAddress; import java.net.Socket; class myThread implements Runnable{ Thread t; Socket soc; boolean state=true; int port; myThread(int port){ this.port=port; t=new Thread(this,"Thread"); t.start(); } boolean getState(){ return state; } @Override public void run(){ try{ soc= new Socket(InetAddress.getLocalHost(),port); soc.close(); }catch(IOException e){ state=false; } } } public class new_prt { static myThread threads[] = new myThread[1024]; public static void main(String args[]){ for(int j=0;j<1024;j++){ threads[j]=new myThread(i); } for(int j=0;j<1024;j++){ try{ threads[j].t.join(); if(threads[j].getState()==true) System.out.println("port "+threads[j].port + " open"); else System.out.println("port "+threads[j].port + " closed"); }catch(InterruptedException e){ } } } }

Monday, July 4, 2011

SCP or how to securely transfer files

scp is a useful command. It works on top of ssh.
To transfer file from local to remote:
scp sourcefile user@remoteserver:/path_target/

To transfer from remote to local:
scp user@remoteserver:/path_source/sourcefile /path_target/
Note: if the target is current directory then '.' can be used as in scp user@remoteserver:/path_source/sourcefile .
if the target is the parent directory current directory then '..' can be used as in scp user@remoteserver:/path_source/sourcefile ..

To transfer files from one remote to another remote:
scp user@remoteserver1:/path_source/sourcefile user@remoteserver2:/path_target/
Note: the server must be able to reach each other because files will be transferred directly between them.

To transfer using some specified port:
scp -P port_number user@remoteserver:/path_source/sourcefile .

To transfer all files in a directory:
scp user@remote:/source_path/* .

To transfer a directory recursively:
scp -r user@remote:/source_path/ .

To limit bandwidth used:
scp -l bandwidth user@remoteserver:/path_source/sourcefile .

To view file in remote machine:
vi scp://user@remote/source_path/remotefile

To compare files:
vimdiff scp://user@remote/source_path/remotefile localfile

Ping FU or (ping for u)

I learnt about these commands in my Computer Network classes. I didn't know about these commands initially. Well, I am still learning. One of the commands is
Ping: Packet Internet Groper. Imagine a sonar. Ping is similar. Also, sonar makes a ping sound. Here is a site dedicated to the ping. An interesting introduction can be found there.
The way this works is the computer or device will generate an ICMP packet that is sent over the local network or internet. The ICMP packet will find its way across the network by having a source and destination IP address. When the device receives this information it then sends a reply saying “yes, I am here”.
Ping depends on ARP and DNS.


ping IP 
ping hostname
It is as simple as that. Press enter and packets are sent out and you find out whether the IP is reachable or not. But first you need to check whether the network is up by pinging yourself. Your machine is "localhost" or "127.0.0.1". Ping any of them or 0. Packets will be sent until Ctrl+C is pressed
ping localhost
ping 127.0.0.1
ping 0


Now the options are like ornaments to the basic ping. They do add an edge when used creatively. 
-c specify the number of echo requests. 
in the following example ping sends 3 packets. Notice that although 58 bytes are sent, 64 bytes are received. The extra 8 bytes are due to ICMP headers
using ping the ip can be found if the host name is given. In the following example, the ip of facebook is found

-i specify the number of seconds between each ping. If the interval is less than 0.2 seconds, superuser privileges are required.
One trick with this option is to simulate sleep. You only need to redirect the output to a bit bucket. In Unix or Linux /dev/null and in windows NUL is the bit bucket.
in the example below, the interval will be of 2 seconds and also the simulation of sleep is shown.

-a gives a beep sound when host is reachable. This thing can help by removing the need to look into the screen to check about successful ping

-f ping flood. By default, the interval between pings is set to zero seconds. It send an echo request immediately after receiving the reply to the previous one. I writes a '.' when a packet is sent and a backspace when reply is received. Super user privileges are required for this. In the following example, 20171 packets were sent and received in around 2.8 seconds.

-q Quiet mode.Prints only ping command statistics.

-s Set packet size.  In the following example, size of ping packet is set to 50. However, 78 bytes of data are sent. The additional 28 bytes belong to IP header and ICMP payload. 
This option was, once upon time, to used to create the ping of death.

-w Set deadline. It is used to set the total amount of time the ping packets should be sent. If it is use with -c then, whichever comes first will terminate the sending of packets. Again, this option can be used as a repalcement for the sleep command by directing the output to the bit bucket (/dev/null). In the example below, deadline is set to 2 seconds.

-p Ping pattern. Upto 16 padding bytes to fill out the packet sent can be specified. This is useful for diagnosing data-dependent problems in a network. In the following example, 2 bytes of alternating 0s and 1s is used.

-R Records route. Turns on route recording for the Echo Request packets, and display the route buffer on returned packets (ignored by many routers).

ping hop1 hop2 hop3 hop4 destination This can be used to set the path for ping.

press Ctrl+| (SIGQUIT) to get short summary as shown below


Saturday, July 2, 2011

ShoutOut: using google app engine to ping a number of people at the same time

This is the script i used to message to many of my friends at a time

from google.appengine.api import xmpp from google.appengine.ext import webapp from google.appengine.ext.webapp.util import run_wsgi_app from google.appengine.ext.webapp import xmpp_handlers user1="%s said:" dirc=[] class XMPPHandler(xmpp_handlers.CommandHandler):     def text_message(self, message=None):         global dirc         #message.reply("yo man")         myname=unicode(message.sender.split('.')[0].lower()) + '.com'         for dir in dirc:          if myname in dir:           for x in dir:            if myname != x:             xmpp.send_message(x,user1 % myname)             xmpp.send_message(x, message.body)     def start_command(self, message=None):         global dirc         dir=message.body.split(' ')[1].split(';')         dirc[len(dirc):]=[dir[0:]]         myname=message.sender.split('.')[0]         for x in dirc[len(dirc)-1]:          xmpp.send_message(x,"chat session started by %s" % myname)          xmpp.send_message(x,"now in session are:")          for y in dirc[len(dirc)-1]:  xmpp.send_message(x,y)     def stop_command(self, message=None):      i=-1      myname=unicode(message.sender.split('.')[0].lower()) + '.com'      for dir in dirc:       i=i+1       if myname in dir:           for x in dir:          xmpp.send_message(x,"Session Stopped")         del dirc[i] application = webapp.WSGIApplication([('/_ah/xmpp/message/chat/', XMPPHandler)],                                      debug=True) def main():     run_wsgi_app(application) if __name__ == "__main__":     main()

Here the Google App Engine acts as a middleman. A sort of chatroom is formed between a number of users and message belonging to a group is relayed to all members of the group.
A sends a message which B and C receive.
A------->App Engine----->B
                             |
                             |
                            V
                     C
A group chat is started by using /start username1;username2;usernameN
It is stopped by /stop
I kinda stopped working on it. A lot of improvements are to be done. One reason I stopped working on it is because Gmail has this thing inbuilt.
Will come up with an explaination soon. Bear with me.

XMPP or (How to programatically do stuffs in IM)

Hi.
It's been a while since I worked with the Smack API.... 6 months and so I might have forgotten many things. Anyways, it is a brief introduction to XMPP and more specifically to Smack API. It provides Java libraries for playing around with XMPP.



The above snippet will connect to the gmail server and make an status update.

Presence presence =new Presence(Presence.Type.available,
status,
24,
Presence.Mode.available);
Here Presence.Type is a enum to represent the presecence type. Note that presence type is 
often confused with presence mode. Generally, if a user is signed into a server, they have a 
presence type of available, even if the mode is away, dnd, etc. The presence type is only 
unavailable when the user is signing out of the server
status contains the string which you want to set as your status.
24 is the priority. The highest priory client will receive the packets.
Check this out for better documentation of Presence.


Add
Roster roster = connection.getRoster();
System.out.println("No of contacts: " + roster.getEntryCount());
for (RosterEntry entry : roster.getEntries())     
System.out.println("User: " + entry.getUser());
The above code will show the size of your roster and also show the user list

Message msg=new Message(user@gmail.com",Message.Type.chat);
msg.setBody("ssup?");
connection.sendPacket(msg);
Use this to send IM to some user.

presence.setStatus(status);
connection.sendPacket(presence);
Use this to change status.
There are many fun things that you can do with this. You can make your status rotate, write the lyrics of your favourite song line by line, flood, or change your status from busy to available and vice-versa continuously to get a alternating red-green thing.
Do your thing and please share it. Will love to learn from you.

System Monitoring basics

This is just an overview of few of the system administration tools. Infos about what programs are running, how much memory or how much cpu cycles are being used, who are logged in and for how long. 


top: it provides a lot of information about the processes running, percentatage of the cpu and th ememory being consumed by that process, who owns the process and its Process ID. It also shows the uptime and memory usage. The output is a dynamic full screen.  Using hot key k any process can be killed. Press M to sort by memory, P to sort by %CPU and S to sort by time.
duit shows the disk usage(in Kb) of each directory and sub-directories. It checks recursively and starts from the current folder by default. By supplying a name you can make it start from there.
Options: (these may vary with version run, and there are more ;)
-a (--all) Prints usage for ALL files, not just the subdirectories
-b (--bytes) Prints usage in bytes rather than Kb.
-s (--summarize) Prints ONLY the total usage of the directory
df: this command tells about the amount of free space on all mounted file system, or the name of a device can be specified. It will show the size, used space, available space, used% and mount information.
ps: this command shows what processes are running with your user id. ps aux command gives additional information about all processes. This is similar to top except that it gives only a snapshot and is not updated. 
vmstat: This command  reports information about processes, memory, paging, block IO, traps and cpu activity. 
-a Gives information about Active/Inactive Memory Pages
-m Displays Memory utilization
who: It displays the users who are logged into the system
uptime: to get the uptime, together with the current time, the number of users and load averages for the past 1, 5 and 15 minute intervals
w: It displays information about users currently logged into the machine and their processes. It is a combination of who, uptime, and ps.
free: shows information about the machine's memory. This includes physical memory (RAM), swap as well as the shared memory and buffers used by the kernal. All measurements are in Kb.
iostat: It report Central Processing Unit (CPU) statistics and input/output statistics for devices, partitions and network file systems (NFS).
mpstatdisplays activities for each available processor, processor 0 being the first one. mpstat -P ALL to display average CPU utilization per processor.
man the above commands to learn more

Friday, July 1, 2011

SSH Daemon (or Setting up a SSH server)

The SSH Daemon act like a server and listens for and handles any incoming connection. 


To install ssh daemon in Ubuntu:
sudo apt-get install openssh-server


Assuming the OpenSSH client is also installed already,
ssh localhost
to check whether the daemon is working properly.


To start the server
sudo /etc/init.d/ssh start


To stop the server
sudo /etc/init.d/ssh stop


To restart the server
sudo /etc/init.d/ssh restart


To disable ssh in Ubuntu machine
apt-get remove openssh-server



Now lets change some operational parameters.
Don't forget to keep  backup of the original config file /etc/ssh/sshd_config


By default, ssh server listens at port 22. If you may want to change the port address for safety
purpose. For that open /etc/ssh/sshd_config file and look for line Port 22. Change line to Port <desired number>. Restart sshd server.


It is advisable to not allow root login. To do this, in the above file update with PermitRootLogin no


Make sure Protocol 2 line is in the above file since SSH-1 has man-in-the-middle attacks problem and security vulnerabilities. It's best to avoid using the obsolete SSH-1.


Add AllowUsers user1 user2 user3 to the config file to allow only certain users to use ssh to login to the system. Alternatively, if you want to allow every user except some, you need to DenyUsers user1 user2
AllowGroup and DenyGroup can also be used for control at the group level.


To kick user out after certain time interval of idleness add ClientAliveInterval <time in sec> and ClientAliveCountMax <number of messages to be sent without receiving any message in return>
For example
ClientAliveInterval 600
ClientAliveCountMax 0
Server will wait for 600 seconds and will not send any warning message before kicking out the user.

Update config file with IgnoreRhosts yes to prevent login using the insecure RSH

Using host-based authentication, any user on a trusted host can log into another host (with the same username) on which this feature is enabled. To disable host-based authentication update the file with HostbasedAuthentication no

Update config file with PermitEmptyPasswords no to disallow logins with empty passwords.

To limit ssh port binding, add ListenAddress <ip address> to config file.

Please share other helpful infos in the comments so all of us will be able to learn from each other.

Some Linux Commands

The original page went down around 2007. It belonged to Clark College Linux User Group. Found it in the waybackmachine.org
You can check out this. The link redirects to the archive where the page is stored.
Use  man command for more information on the commands.
Console Commands
Utility or CommandUse/Description
passwdChanges password
nslookupQueries Internet domain name servers
quotaDisplays disk usage and limits
motdMessage of the Day
finger usernameUser information lookup program
man or xman commandDisplays pages of online manual
xmanDisplays System Manual in X
less filename or morefilenameDisplays the contents of a file in the terminal one page at a time
infoDisplays information and documentation on shells, utilities and programs
clearClears the terminal window
ls directoryList contents of a directory
cat filenameDisplays the contents of a file in the terminal
rm filenameRemoves a file
pico filename or emacsfilenameOpens and edits text files
cp sourcefile detstinationfilenameCopies a file
lpr filenameSends file to printer
grep string filenamelooks through files for strings
head filenameDisplays first 10 lines of file
tail filenameDisplays last 10 lines of file
mv existingfilename newfilenameMoves or renames file
lpq filenameDisplays files in printing queue
lprm filenameRemoves file from printing queue
sort filenameDisplays and sorts file contents
diff filename1 filename2Displays differences between files
file filenameDisplays information about file contents
echo stringCopies string to terminal
dateDisplays current date and time
calDisplays calendar
gzip filenameCompresses a file
compress filenameCompresses a file
gunzip filenameDecompresses a compressed file
zcat filenameDisplays contents of a compressed file
apropos commandLists all man page titles/headers that contain the command
lynxText based web browser
dmesgDisplays kernel ring buffer
which commandDisplays path to command
whereis commandDisplays paths to locations of commands
whoLists currently logged on users
finger username@hostnameObtains detailed information about a user currently using the system
wLists currently logged on users with processing usage
mesg y/nSets options for letting other users write you messages
write userSends message to other users
talk userAllows two way chat to other users
chmod permissions filenameChanges file access permissions
mkdir directorynameMakes a directory
rmdir directorynameRemoves an empty directory
ln existingfile new-linkCreates link to an existing file (hard link)
stat filenameLists information about a file
ln -s existingfile new-linkCreates link to an existing file (soft link)
dfDisplays all mounted filesystems
psReports process status
command &Sends a job to the background (job: one or more commands connected by a pipe "|" or pipes) The operating system assigns a number to the job when you press return. example: [1] 3578
topDisplays updating list of currently running processes
ttyDisplays the name of the terminal in which the command was issued
command filenameRedirects standard output 
command < filenameRedirects standard input
cat file1 >> file2Appends standard output from file1 to file2
cat /dev/null > filename orfilename > /dev/nullRedirects "bit bucket" or null string to file (only superuser has write access to this file)
command1 | command2Pipe sends standard output of one command to the standard input of another command
tr string1 string2 < inputfiletranslates each character in string1 to the corresponding character in string2
command | tee filename |grep stringSends the output of one command to standard output and a file
bg %job numberSends job to the background by job number
fg %job numberBrings job to the foreground by job number
kill PID or %job numberAborts a process by PID (Process Identification Number) or job number
jobsDisplays a list of current jobs
netcfgUtility to set up PPP and network configurations
xevUtility used to see information flow from X server to client
echo $DISPLAYEnvironment variable that displays the ID string for a window
echo $PATHVariable that displays executable path
netstatDisplays network connections
viewresGraphical class browser for X
xbillGame featuring Bill Gates trying to put windows on Macs and NeXT workstations
xevilGame similar to Loderunner?
xchompLinux's version of PacMan
xcmapStrange color lookup utility
xeditText editor for X
asclockClock from AfterStep
xconsoleStrange console for X
xmessage messageSends message to a dialog box
xgalXGalaga game
xg3Image viewing program
xgcGraphing calculator?
xjewelJewel game for Linux
xkbvledsLEDs?
xkbwatchLEDs?
xlogoDisplays X logo
xmixerOpens system sound controls
xsnowSnowflakes are fallin' on your desktop
xwininfoDisplays info about a window
startxStarts an X Window System server
ghostviewStarts a text preview application
xv filenameImage viewer
xsetroot -colorSet background color in X
xcalcStarts a calculator in X
xclipboardStarts a clipboard in X
traceroute hostPrints the route packets take to the host
hostnameDisplays system identity name
rlogin hostUtility to connect to a remote system
telnet hostUtility to connect to a remote system (similar to rlogin but more interactive)
rcp file remotemachineUsed to copy from a remote computer
ftpUtility to transfer files between systems on a network
rsh commandUtility to run a command on a remote system without logging in
ping hostUtility used to test connection to a remote system
lcd directorypathChanges local machine directory while logged on to remote machine


Shared Directories
DirectoryDescription
/Root - The root directory is present in all Linux system file structures. It's the parent of all files in the system.
/binEssential common binaries - Holds files needed to boot the system and run it when it comes up in single user mode.
/bootStatic files of the boot loader.
/devDevice files - All files that represent peripheral devices.
/etcMachine-local system configuration - Holds administrative, config, and other system files. Holds /etc/passwd file that contains a list of all users who have permission to the system.
/homeUser home directories - Contains each user's or client's home directory
/libShared libraries
/mntMount point for temporary partitions
/procKernel and process information (virtual filesystem)
/rootHome directory for root
/sbinEssential system binaries - Contains utilities for system administration.
/tmpTemporary files
/usrSecond major hierarchy - Includes subdirectories that contain information used by the system.
/varVariable data - Contains files whose contents change as system runs.

vi Commands
CommandDescription
vi filenameStarts vi and creates a new file
:q!Quits vi without saving work
pPastes data in buffer below current line
PPastes data in buffer above current line
yyCopies current line
:r !commandReads in output of the command
iPuts vi in insert mode
:set autoindentSets vi to indent automatically
:set showmatchSets vi to show matching parenthesis
:set nuSets vi to display line numbers
:set showmodeSets vi to display the mode you're in
ESCAPESets vi to command mode
Control-UErases current line (insert mode)
Control-HErases on letter (insert mode)
Control-WErases current word (insert mode)
h, j, k, lMoves cursor left, up, down, right respectively
uUndoes last action
xDeletes a single character
dwDeletes a single word
ddDeletes a single line
ZZWrites work buffer to disk and exits vi
oinserts line after cursor position
Control-LRedraws screen
:w filenameSave work as filename and exits

Control Characters
KeyUse
Control-H or BACKSPACEErases a character on the command line
Control-UDeletes an entire command line
Control-WErases a word on the command line
Control-CAborts program execution
COMMAND-TabSwitches Programs
Control-L or CONtrOL-RRefreshes the screen
Control-Dlogout or exitLogs you off the system