Monday, May 2, 2011

Scapy Installation

The easiest way to install and keep updates of scapy is by using mercurial. I prefer to use Ubuntu, rather than Windows, because I don't want to keep thinking about compability things and all other unnecessary stuff to think about. Just to keep my mind focused.

Okay, here is how it is done:
Install mercurial
#apt-get install mercurial
Check out a clone of Scapy’s repository. Here, you'll get the latest development version of it.
# hg clone http://hg.secdev.org/scapy
It will create a new directory named after 'scapy'. Here, all scapy codes are copied. You can either install it to your system (enter to this directory, run 'sudo python setup.py install') or run it directly (using run_scapy.bat).
To keep update with the latest contribution, use the following step:
# hg pull
# hg update
Then, install/run run_scapy as usual.

Monday, April 11, 2011

XmlNodeType Members

Enumeration XmlNodeType (namespace: System.Xml) is used to specify the types of XML node.
 
XmlNodeType members:
  • None  --> This is returned by the XmlReader if a Read method has not been called.
  • Element  --> An element (for example, ).
  • Attribute --> An attribute (for example, id='123' ).
  • Text  --> The text content of a node.
  • CDATA --> A CDATA section (for example, )
  • EntityReference --> A reference to an entity (for example, # ).
  • Entity  --> An entity declaration (for example, ).
  • ProcessingInstruction --> A processing instruction (for example, )
  • Comment  --> A comment (for example, )
  • Document  --> A document object that, as the root of the document tree, provides access to the entire XML document.
  • DocumentType --> for example,
  • Notation  --> for example,
  • Whitespace  --> White space between markup
  • SignificantWhitespace  --> White space between markup in a mixed content model or white space within the xml:space="preserve" scope
  • EndElement  --> for example,
  • EndEntity 
  • XmlDeclaration  --> for example,               
dd

Tuesday, April 5, 2011

Check IP Address Type

Idea:
- display all IP addresses
- inspect it one by one, whether:
  - it is IPv4
  - it is IPv6
     - it is loopback address
     - it is link local
     - it is multicast
     - it is site local
     - it is teredo

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace ConsoleCekIPSatuSatu
{
    class Program
    {
        static IPAddress[] GetIPAddress(string host)
        {
            IPHostEntry hostInfo;
            hostInfo = Dns.GetHostEntry(host);
            return hostInfo.AddressList;
        }

        static void CheckIPType(IPAddress ip)
        {
            if (ip.AddressFamily == AddressFamily.InterNetwork)
            {
                Console.WriteLine("\t\tThis is IPv4");
                CheckIPv4(ip);
            }
            else
            {
                Console.WriteLine("\t\tThis is IPv6");
                CheckIPv6(ip);
            }
        }

        static void CheckIPv4(IPAddress ip)
        {
            // check whether it is loopback address or not
            if (IPAddress.IsLoopback(ip))
                Console.WriteLine("\t\tThis is IPv4 loopback address");
        }

        static void CheckIPv6(IPAddress ip)
        {
            // check whether it is loopback address or not
            if (IPAddress.IsLoopback(ip))
                Console.WriteLine("\t\tThis is IPv6 loopback address");

            // check whether it is link local address
            if (ip.IsIPv6LinkLocal)
                Console.WriteLine("\t\tThis is link local address");
            
            // check whether it is multicast address
            if(ip.IsIPv6Multicast)
                Console.WriteLine("\t\tThis is multicast address");

            // check whether it is site local address
            if (ip.IsIPv6SiteLocal)
                Console.WriteLine("\t\tThis is site local address");

            // check whether it is teredo address
            if (ip.IsIPv6Teredo)
                Console.WriteLine("\t\tThis is teredo address");

        }

        static void Main(string[] args)
        {
            string hostName = Dns.GetHostName();
            Console.WriteLine("Hostname: {0}", hostName);

            // get the IP addresses list
            IPAddress[] hostNameIPAddresses;
            hostNameIPAddresses = GetIPAddress(hostName);

            // check the type of each IP address
            foreach (IPAddress ip in hostNameIPAddresses)
            {
                Console.WriteLine("\n\t" + ip.ToString());
                CheckIPType(ip);
            }

            Console.ReadKey();
        }
    }
}



Monday, April 4, 2011

Resolving Hostname

I know, it is kind of lame. But I'll post it anyway.

using System;                   // for String and Console
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;               // for Dns, IpHostEntry, IPAddress
using System.Net.Sockets;       // for SocketException

namespace SocketCoba01
{
    class Program
    {
        static void PrintHostInfo(string host)
        {
            try
            {
                IPHostEntry hostInfo;

                hostInfo = Dns.GetHostEntry(host);  //karena Dns.resolve sudah obsolete 
                //(baca dokumentasi .NET)

                //display the primary hostname
                Console.WriteLine("\tCanonical Name: " + hostInfo.HostName);

                //display list of IP adresses for this host
                Console.WriteLine("\tIP Addresses: ");
                foreach (IPAddress ip in hostInfo.AddressList)
                {
                    Console.WriteLine("\t\t{0}", ip.ToString());
                }

                Console.WriteLine();

                //display list of all aliases for this host
                Console.WriteLine("\tAliases: ");
                foreach (String alias in hostInfo.Aliases)
                {
                    Console.WriteLine("\t\t{0}", alias);
                }
            }
            catch (Exception) 
            {
                Console.WriteLine(".:. Unable to resolve host: " + host + "\n");
            }
 
        }
        static void Main(string[] args)
        {
            try
            {
                String LocalHostName = Dns.GetHostName();
                Console.WriteLine("Local host");
                Console.WriteLine("\tHost name: {0}",LocalHostName);

                PrintHostInfo(LocalHostName);                
            }
            catch (Exception) 
            {
                Console.WriteLine("error @Main");
            }
            Console.ReadKey();
        }

       
    }
}


Saturday, March 19, 2011

Copying Array's Content to Another

Several methods used to copy the contents of an array to another.

Random r = new Random();
int[] pins = new int[4]{ r.Next() % 10, r.Next() % 10,
                         r.Next() % 10, r.Next() % 10 };

// 1st method
int[] copy = new int[pins.Length];
for (int i = 0; i < pins.Length; i++ )
{
 copy[i] = pins[i];
}

// 2nd method
int[] copy2 = new int[pins.Length];
pins.CopyTo(copy2, 0); //starting from index 0 of copy2

// 3rd method
int[] copy3 = new int[pins.Length];
Array.Copy(pins,copy3,copy3.Length);

// 4th method
int[] copy4 = new int[pins.Length];
copy4 = (int[])pins.Clone();

All of them are copying the reference of the original array, not the value.

Friday, March 11, 2011

Step Through Codes during Debugging Process

  • Right-click at the starting line of the code which you want to analyze
  • Select "Run to cursor"
  • Your program will be run. Enter some values to test the code
  • In the lower-left side, choose 'Local' tab. Here, you can monitor the local variables assignment. 
  • Debug -> Step into (shortcut: F11), to run the next code
  • Do the last step for several time while monitoring the 'Local' tab, until you figure out how your code works.

Wednesday, March 9, 2011

Browse and Display Text File (C#)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;          //namespace buat OpenFileDialog 
using System.IO;

namespace BrowseAndDisplay
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();
        }

        private void buttonClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void buttonBrowse_Click(object sender, RoutedEventArgs e)
        {
            //create an instance of the open file dialog box
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.InitialDirectory = "C:";
            openFileDialog.Title = "Choose one of the text file";
            openFileDialog.FileName = "";
            openFileDialog.Filter = "Text file|*.txt";

            openFileDialog.ShowDialog();

            //isi textBox FileLocation dengan path file yang akan dibuka
            textBoxFileLocation.Text = openFileDialog.FileName;

            //tampilkan isi file text ke textBox DisplayTextFile
            FileInfo src = new FileInfo(openFileDialog.FileName);

            textBoxDisplayTextFile.Text = "";
            TextReader reader = src.OpenText();
            string line = reader.ReadLine();

            while (line != null)
            {
                textBoxDisplayTextFile.Text += line + '\n';
                line = reader.ReadLine();
            }
            reader.Close();
        }
    }
}