Programming

by AstralSin on 05-21-2008

Most programmers that have worked with C or C-like languages are familiar with the main() function.  Its the heart of the program and from where all other portions of the code execute.  Python doesn't have a built-in main() function so it can be a little foreign to people like me so I found a way to create a main() and have my code structured more like I'm used to.  Its actually quite simple to do.

def main():

     <your python here>

if __name__ == "__main__":
    sys.exit(main())

Thats it.  You'll declare a function called main and call it with the two lines at the bottom.  Now you can structure your code with other functions and classes and have everything originate from the main().


0 comments


by AstralSin on 05-20-2008

OK, last time I ranted about how most programming tutorials don't really tell you anything.  I've pinpointed exactly what they don't tell you.  Most tutorial writers tell you the proper syntax of loops, functions, methods, whatever, and for most languages that's fine because the programs are written the same way.  But some languages, like Python, don't necessarily rely on functions to define the structure of the code.  In C and many languages like it, there is the main() function and you know thats the beginning of the program.  In Python, there is no main() function. 

While I can figure out to put stuff wherever I can fit it, some people can get confused by this, especially beginner programmers.  I mean, really, authors please take into consideration that your readers may not have as much experience as you and they may need an extra sentence or two describing the structure of a program.  Or like I said earlier, while you're writing your tutorial, write a little program that demonstrates how to use all the primary features of the language while putting them in context.  It makes things alot easier and by doing this, you open the doors to better programmers in the future that may have gotten their start from reading your material.  Take a little pride in your work and put some extra effort into your instruction by adding clear context to your example code.

 

Oh, and btw, please stop using the command line interpreter to show us how to do one line at a time.  No one writes a program one line at a time.  Its completely useless to show us anything there.  Write it with some other code to let us know how at least TWO lines can interact with one another.


0 comments


by AstralSin on 05-15-2008

I've read many a programming tutorial and if you've read some, you'll agree that the great majority of them suck. They all just try to tell you what a variable or loop is written in that particular language and that may be useful information for some people, some of us would like a little more contextual relevance. I love tutorials that actually write a program and explain things along the way. This way you get to write a real program, which may or may not be useful to you, and learn how things are really done in that language. This is especially useful if you're treading unfamiliar waters such as learning GUI programming when you're accustomed to CLI programming or learning game programming when you're used to writing database programs.

I've been learning C# lately, just out of the need to know Windows programming so I can make myself more marketable in the job market. I have been following this tutorial which walks you through creating a game in C#. Sure, knowing how to write a game is completely useless in the IT world but this particular tutorial walks you through the Microsoft way of coding C#. It explains and gives examples and hands on exercises with methods (aka functions), classes, AI, polymorphic programming, arrays, and more. This is how a tutorial should be written. Not only is it very informative and comprehensive, its also interesting and rather fun. It really helps when learning a new language to have a) a definite goal and b) an interesting instructor/learning aid.

I'm constantly on the lookout for tutorials of any programming language that follows this archetype. If you know of any, please let me know in the comments.


0 comments


by AstralSin on 05-15-2008

One of the most important things to do when a program is finished is to run checks to be as sure as possible that there are no memory leaks or buffer overflows/underruns.  Not only does this lower the chance for your program to crash, it lowers the chance of someone finding an exploitable vulnerability that could do any number of things from crashing the program to providing access to the system.  In the programming world of open source and Linux, there are several options for checking your code against this type of flaw.

These errors are caused by programming mistakes, and they happen.  No one writes perfect code 100% of the time so everyone should run some type of tests against their programs.  I've recently been testing a program with valgrind and electric fence to try and find the cause of a specific error that occurs on a specific platform.  Alas, the problem has not been found but I did learn quite a bit about how to test for programming flaws. 

Valgrind should be run on all your program code.  It checks for memory leaks and helps you determine where the errors may be.  Using this is pretty straightforward, Just run valgrind <binary> and it will log all the memory leaks as they happen.

If you know your program has some flaws and you just can't find them, electric fence may be able to lend a hand. Electric fence is used in combination with gcc and gdb to attempt to find the exact line of code where a malloc() overrun or underrun occurs.  While it doesn't always provide a sure-fire explanation of what's going on, it can provide some valuable information about where the problem could reside.  To use electric fence, link efence with -efence in your build line.  The code is then compiled and a binary is created.  Then call gdb <binary> and type run at the gdb shell and recreate the conditions that cause your program to crash. 

There are other options as well.  gcc has its own malloc() debugger built in, but I won't be documenting it here.  If you need more information, a quick Google search can provide much more information about either of these solutions.  Valgrind and electric fence have been highly recommended to me lately and they've been very useful.  Perhaps you can find them as useful as I have.


0 comments


by AstralSin on 04-15-2008

Lots of times in the depths of programming and security studies, you sometimes come across binary and hexadecimal numbers. Most people look at it and have absolutely no idea how to read either. Its actually quite simple once you know what you're looking at. Let's just look at some examples to show you what's going on.

In each of these numbering systems, each digit represents a field that has a value. The digit itself is only there to manipulate the value of that field. In binary, the fields are read from left to right in descending values and each place is double the one to the right of it. Its easier to remember the values of the places starting from the right, and consequently, its easier to read the value right to left. If there is a one in that column, that means that value is turned on and should be added to the total, if there is a zero, that value should not be counted. Take the following example:

8 4 2 1
1 0 1 1 = 11

As you can see, the rightmost column represents one, the one to the left of that represents two, the next represents four, the one to the left of that represents eight. A binary value that has four places is referred to as a nibble, half a byte (a term with which you might be more familiar that has eight places, a bit is only once place). The fourth bit in this nibble (8) is turned on, so we'll add eight to our total, the third bit is off so we'll ignore that. The second bit is turned on so we'll add two to our total; and the first bit is turned on so we'll add one to come to a total of eleven. Easy math, right? Now let's try some hexadecimal.

Hexadecimal is a tad more complicated because there is an added dimension to the math and there are more than two numbers to worry about, now there are 10 numbers and 5 letters as well. Each place still represents a value and each place is the square of the last. In this nibble we have four different values for each place. A, B, C, and 2. The numbers 0-9 are easy, they represent themselves, A,B,C,D,and E however, represent the next five numbers respectively (A=10, B=11, C=12, D=13, E=14, F=15).

4096 256 16 1
A    B   C 2 = 43970

If you'll look closely, you'll see what's going on. The character in each field is multiplied by the value of that field and added together. So we've got A(10) * 4096 + B(11) * 256 + C(12) * 16 + 2 * 1 = 43970. Sure, its a bit confusing but you'll NEVER have to calculate this in the field off the top of your head. You're in IT, you should have a device with you at all times that either a) has a calculator or b) can connect to the internet. Now that you know how to calculate the values, you can use any calculator to do so.

Something I didn't touch on here is octal numbers. If you're a *nix guy like me, you're already familar with an octal set (does chmod 755 ring a bell?) The mode numbers for permissions in *nix operating systems is an octal set. Learning octal is up to you, grasshopper. I have taught you all I can teach you.


0 comments


by AstralSin on 03-23-2008

OK, as I continue to tweak my blog, I'm noticing a few things that didn't work correctly. I just implemented search engine friendly URLs for the content posts and fixed some other things. If you find anything that's broken, please let me know astralsin (at) gmail (dot) com.


0 comments


by AstralSin on 03-12-2008

That's right, I've coded my new blog from scratch and I'll finally start posting again. Only this time, things will be a bit different with the site's content focusing more on security and programming. Hope you all enjoy! :)
BTW, if you find any bugs, please let me know.

0 comments