MexiCode Ideas en codigo

Socks Proxy via SSH Tunneling

Posted on January 9, 2010

Probably, everyone and their dog knows how to set up a ssh tunnel for safe webbrowsing.

Well, I didn't know, so I'm writing how to do it.

I have a SSH account on a webserver somewhere (I'm not telling). It's really simple, log in to your account with a local port that forwards all traffic to your ssh connection. Do you know how to do it? no? Well..

Windows
start putty.exe -D 1080 -ssh servername -l username -pw password

Linux
ssh username@servername -D 1080

If you don't have putty, shame on you. Download it here

Now, open your webbrowser of choice, and edit the proxy information:

Address: 127.0.0.1 (thats your own computer)
Port: 1080
Proxy Type: SOCKSv5

And that should do it.

Now, here's an extra tip for all of you who use Firefox. We can do all of our DNS Lookups using our proxy (Effectively overriding any dns webfilter your company may use).

Open about:config and look for network.proxy.socks_remote_dns value and change it to true. This only works with SOCKSv5.

Enjoy :D

Serializing Classes

Posted on January 4, 2010

I stumbled upon a new problem. Again.

In a project I'm currently involved in, I had to use serializable classes. The application saves the serialized object in a database, and then, another application can retrieve the object and deserialize it.

Or at least, that's what i thought.

I was doing some "cut&paste" programming, and I thought it would be easier to just have 2 copies of the serializable class in two different projects. (this one)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
using System;
using System.Collections.Generic;
 
namespace Trivius.Wix.ORM
{
    [Serializable]
    public class Message
    {
 
        [Serializable]
        public class ColorMessage
        {
            public string Message { get; set; }
            public byte Color { get; set; }
        }
 
        public byte Effect { get; set; }
        List<ColorMessage> colorMessages = new List<ColorMessage>();
 
        public void AddMessage(ColorMessage msg)
        {
            colorMessages.Add(msg);
        }
 
        public List<ColorMessage> GetMessages()
        {
            return colorMessages;
        }            
    }    
}

In one application, this approach worked beautifully. It saved and retrieved from the database like a charm. But when I tried to deserialize the object in another application (using the other copy of the exact same class) the compiler told me that:

Cannot cast object of type Trivius.Wix.Message into an object of type Trivius.Wix.Message

Not nice.

So, what I did, is that I separated the serializable class into a Dll, and added a reference to that same class in both projects, and it worked.

I should have done that in the first place. :(

CodeIgniter Session Cookie

Posted on December 29, 2009

I found something "peculiar" about how codeigniter stores session cookies.

CodeIgniter allows you to encrypt your cookie using sha1 which requires an encryption key, but it's not set by default.

A typical CI cookie looks like this:

a%3A9%3A%7Bs%3A10%3A%22session_id%22%3Bs%3A32%3A%22e56862bcb9fe688cb8806b7067b06b7f%22%3Bs%3A10%3A%22ip_address%22%3Bs%3A13%3A%22192.168.0.124%22%3Bs%3A10%3A%22user_agent%22%3Bs%3A50%3A%22Mozilla%2F5.0+%28Windows%3B+U%3B+Windows+NT+6.1%3B+en-GB%3B+rv%22%3Bs%3A13%3A%22last_activity%22%3Bs%3A10%3A%221262120952%22%3Bs%3A4%3A%22user%22%3Bs%3A1%3A%227%22%3Bs%3A3%3A%22rol%22%3Bs%3A1%3A%221%22%3Bs%3A4%3A%22giro%22%3Bs%3A1%3A%221%22%3Bs%3A5%3A%22lugar%22%3Bs%3A2%3A%2250%22%3Bs%3A6%3A%22logged%22%3Bs%3A1%3A%221%22%3B%7D37a9099621cf8f09b531e91c327b4b9d

Which is URL Encoded. A simple UrlDecode gives us:

a:9:{s:10:"session_id";s:32:"e56862bcb9fe688cb8806b7067b06b7f";s:10:"ip_address";s:13:"192.168.0.124";s:10:"user_agent";s:50:"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv";s:13:"last_activity";s:10:"1262120952";s:4:"user";s:1:"7";s:3:"rol";s:1:"1";s:4:"giro";s:1:"1";s:5:"lugar";s:2:"50";s:6:"logged";s:1:"1";}37a9099621cf8f09b531e91c327b4b9d

Which is way more readable. Basically, single letters are the data type (a => array, s => string, i => integer). Then, the length of the field and then the value. (length is omitted on integers).

The last 32 hex digits is the MD5 hash. I found out that if you trim the hash away, and you modify your data, you can "re-hash" it, and append the new hash to your data.

Then, you just UrlEncode your data again, and modify your cookie.

Amazingly... it works.

Microsoft Jet DB Engine doesn’t like 64bit Windows

Posted on December 23, 2009

Apparently, Microsoft never released a 64bit version of it. Change target platform to 32bit and you should be alright.

Tagged as: , No Comments

DataGridView EditOnEnter and row deleting

Posted on December 21, 2009

When you're working in a datagridview, sometimes is useful to use the EditOnEnter edit mode.
But for some reason (i.e. Bug) the deleting functionality is broken under this scheme.

To overcome this, you could use this little hack.

You'll need 2 events:

CellEnter

        private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            DataGridView dgv = sender as DataGridView;
            if (dgv != null)
            {
                dgv.EditMode = DataGridViewEditMode.EditOnEnter;
            }
        }

MouseClick

        private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
        {
            DataGridView dgv = sender as DataGridView;
            if (dgv != null)
            {
                DataGridView.HitTestInfo hti = dgv.HitTest(e.X, e.Y);
 
                if (hti.Type == DataGridViewHitTestType.RowHeader)
                {
                    dgv.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;
                    dgv.EndEdit();
                }
            }
        }

Importing data directly from Excel

Posted on December 21, 2009
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
 
namespace ExcelImport
{
    public partial class Form1 : Form
    {
 
        public Form1()
        {
            InitializeComponent();
            System.IO.FileInfo info = new System.IO.FileInfo("C:\\cosa.xls");
            this.openFileDialog1.FileName = "*.xls";
            DataTable dt = new DataTable();
            dt.Columns.Add("ID");
            dt.Columns.Add("Name");
            dt.Columns.Add("Date");
            dt.Columns.Add("Last Name");
            dt.Columns.Add("Address");
 
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
 
                /* For some reason, all excel lists and "arrays" are not zero based.
                 * This is important to keep in mind while looping thru everything
                 */
 
                Excel.Application ExcelObj = new Excel.Application();
                //I really don't know what the other 13 or so parameters do, but i swear this works
                Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open(openFileDialog1.FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                Excel.Sheets sheets = theWorkbook.Worksheets;
                Excel.Worksheet worksheet = (Excel.Worksheet)sheets.get_Item(1);
                /* This is neat. Only retrieve the used range in the worksheet.
                 * Instead you could load the entire worksheet and get ranges for each row,
                 * but this approach is *much* faster*/
                Excel.Range range = worksheet.UsedRange;
 
                object[,] value = (object[,])range.get_Value(Excel.XlRangeValueDataType.xlRangeValueDefault);
                int rows = value.GetLength(0);
                for (int i = 1; i < = value.GetLength(0); i++)
                {
                    dt.Rows.Add();
                    for (int j = 1; j <= value.GetLength(1); j++)
                    {
                        dt.Rows[dt.Rows.Count - 1][j - 1] = value[i, j];
                    }
                }
 
                //This line is very important. Otherwise you'll end up with N excel processes
                ExcelObj.Quit();
            }            
            dataGridView1.DataSource = dt;
        }
    }
}
Tagged as: , No Comments

Hello world!

Posted on December 18, 2009

Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!

Filed under: Uncategorized 1 Comment