CastleCops, Internet Crime Fighters
Need help? Click here to register for free! Absolutely zero advertisements on this site!

$9736.22 of $21422.68
left sidedonated so farneed $11686.46 donated to reach our goalright side, our goal
Help CastleCops serve the community on new servers, Donate Here to reach our goal.

Donation/Premium
spacer
block bottom
Security Central
spacer
· Home
· PIRT/Fried Phish
· MIRT
· SIRT
· Deutsch
· Wiki
· Newsletter
· O16/ActiveX
· CLSID List
· Contest2007
· Downloads
· Feedback (send)
· Forums
· HijackThis
· Hijacktrend
· LSPs
· My Downloads
· O18
· O20
· O21
· O22
· O23
· O9
· Premium
· Private Messages
· Proxomitron
· Reviews
· Search
· StartupList
· Stories Archive
· Submit News
· WsIRT
· Your Account
· Acceptable Use Policy
block bottom
Survey
spacer
Was 2007 a good year?

Yes it was a wonderful year
Yes, but there is always room for improvement
Status quo
It was a challenge
Other (leave comment)



Results
Polls

Votes: 952
Comments: 28
block bottom
spacer spacer
image Applying Myself: Hello World image
CastleCops

Hello World







By Richard Lindberg, aka RiBiNiN, CastleCops Staff Writer
September 6, 2006


Last time, I confessed to being the source of all the security problems with computers. That was a mistake. Criminals apparently read the Castle Cops front page. There are now articles being written like Attackers pass on operating systems and Attackers pass on OS, aim for drivers and apps.

I seem to have made matters worse. I’m sorry. I am trying make amends by how I write programs now. In addition, Websplasher is a sponsor of the upcoming Security Opus conference in San Francisco. There will be some great speakers including the keynote by Simple Nomad.

Have you ever taken a class in a “temporary” classroom? You know, where they parked a trailer near the school while they built the classrooms they needed? That happened how many years ago?

The same thing happens in programming. We throw in some code, fully intending to go back and make it right, and somehow never get around to doing it. Temporary, buggy code becomes permanent.

Do not write any code that you wouldn’t like to see in a PowerPoint presentation. Picture everybody you know professionally seeing your code. It is the same principle as putting on clean underwear in case you are taken to a hospital. Make your potential shame work for you.

Do not leave any code improperly indented. Calling it “Pretty Print” makes spacing and indentation sound insignificant. In fact, it is extremely important. The brain will often interpret logic from the indentation instead of from the code.

I know this sounds like the opening remarks in Computer Science 101 and all the students are thinking, “Yeah, yeah, when are we going to code?” Right now!
int main(int argc, char* argv[])
{
01	char Message[64];
02	char * pMessage=Message;
03	strcpy(pMessage,argv[1]);
04	strcat(pMessage," ");
05	strcat(pMessage,argv[2]);
06	strcat(pMessage,"\n");
07	printf(pMessage);
08	return 0;
}
This is a basic “hello world” program written in C. I added the line numbers for convenient reference.

This program allows you to enter any 2 words on the command line and have them display on the console. Line 01 sets up a space 64 characters long, plenty of space for any two words I can think of. Line 02 gives us a pointer to the space that we can use to work with it. Line 03 takes the first word and moves it to the message. Line 04 adds a space to go between the words. Line 05 places the second word after the space. And finally, line 06 completes the string with a newline character.

If the person using the program enters Castle01 Hello world on the command line, it works fine. Leave off one of the words and it blows up. If the combined length of the parameters is more than 62, the program abends and is vulnerable to being taken control of.

Is this example too contrived? I don’t think so. Accepting that parameters are correct is a very common practice. Never trust input. Adding some parameter checking helps:
#define MessageLENGTH 64
int main(int argc, char* argv[])
{
   if (argc==3)
   {
      if ((strlen(argv[1]) + strlen(argv[2])) < MessageLENGTH-2)
      {
         MainNormalProcessing(argv[1],argv[2]);
         return 0;
      }
      else
      {
         printf("This parameters are too long\n");
         return 1;
      }
   }
   else
   {
      printf("This program needs 2 parameters\n");
      return 1;
   }
}

void MainNormalProcessing(char * cWordOne, char * cWordTwo)
{
   char Message[MessageLENGTH+1];
   char * pMessage=Message;
   memset(pMessage,'\x0',MessageLENGTH+1);
   strcpy(pMessage,cWordOne);
   strcat(pMessage," ");
   strcat(pMessage,cWordTwo);
   strcat(pMessage,"\n");
   printf(pMessage);
   return;
}
Did I miss anything? What is the error that will likely be introduced the first time the output message is changed? E-mail: RiBiNiN@CastleCops.com Hint: put CCPS in the subject line so I know it’s not spam.
Posted on Wednesday, 06 September 2006 @ 15:33:15 UTC by Robin (1544 reads)
[ Trackback ]
image

"Applying Myself: Hello World" | Login/Create an Account | 0 comments
Threshold
The comments are owned by the poster. We aren't responsible for their content.

No Comments Allowed for Anonymous, please register
 
Login
spacer
Nickname

Password

Security Code: Type Security Code: Usage signifies AUP acceptance
· New User? · Click here to create a registered account.
block bottom
Related Links
spacer
· del.icio.us!
· digg it!
· reddit!
· TrackBack (0)
· PHP HomePage
· HotScripts
· W3 Consortium
· Spam Cop
· CastleCops
· More about CastleCops
· News by Robin


Most read story about CastleCops:
Acceptable Use Policy

block bottom
Article Rating
spacer
Average Score: 0
Votes: 0

Please take a second and vote for this article:

Bad
Regular
Good
Very Good
Excellent


block bottom
Options
spacer

Printer Friendly Page  Printer Friendly Page

block bottom
spacer spacer