
nocare
Novice
May 17, 2010, 3:41 PM
Post #2 of 2
(601 views)
|
|
Re: [nocare] Crypt::DES and Digest:MD5 strangeness
[In reply to]
|
Can't Post
|
|
I figured out the problem! But I need help fixing it. ... reference this image, and i believe I can explain what is happening. http://i225.photobucket.com/albums/dd317/deadlyp99/programming/nullonpass.png My static variable "root\0\0\0\0" is eight bytes, but it is stored as a single solid variable string. My input however is not static, and get's modified. I decided to send the variable directly after input, and directly after my CheckLength function. Directly after input I had the user name and a NEW LINE. Tested that against my password and there was no new line. wtf right?! After passing through CheckLength, because the username had a "\n" char attached it only had 3 "\0" tagged onto the end as apposed to the static variable which has 4 "\0" and no "\n". I'm so happy I found this bug, and that I have prior knowledge of how these unseen data are tagged onto strings in files. Here are my thoughts on how this goes, and my first approach at fixing it.
} else { sysopen(my $handle, "usernamelog.txt", O_RDWR|O_EXCL|O_CREAT, 0755) || die "WTFFFFF"; print " FAILED"; print "\n\nIn order to continue you need authenticate yourself!\nUsername: "; my $username = <STDIN>; my $username_len = CheckLength($username); print "\nPassword: "; my $passwd = <STDIN>; my $passwd_len = CheckLength($passwd); printf $handle "Username: ".$passwd_len;close($handle);exit; my ($username_md5, $passwd_md5) = EncryptAndHash($username_len, $passwd_len); print "\n\nusername_len: ".$username_md5."\npasswd_len: ".$passwd_md5; TryLogin($username_md5, $passwd_md5); } You'll notice that I did something very very bad, and that I've never encountered or needed to do before in a programming language. Through some research I realized that STDIN collects everything untill the enter key is pressed, but in addition holds the newline char generated when you press return!! BECAUSE my password input just so happens to be 8 bytes, the 9th byte is a newline, and because my function substrings that variable, it removes the newline! The username which is 4 bytes+"\n", appends the nulls, as mentioned above. After more research I learned I need to "chomp" my input, which is essentially substringing to 1 character lower then the total length of the input. What a freaking coincidence, eh? So people new to perl, make sure to take a big ol' bite out of your user input, using chomp
|