Advertisement
dvdjaco

8.2

Feb 18th, 2012
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.00 KB | None | 0 0
  1. # #!/usr/bin/python
  2. #
  3. # By dvdjaco
  4. # Exercise 8.2
  5. #
  6. # Figure out which line of the above program is still not properly guarded.
  7. # See if you can construct a text file which causes the program to fail and then modify the
  8. # program so that the line is properly guarded and test it to make sure it handles your new
  9. # text file.
  10. #
  11. # R: The original program is vulnerable to input files containing any line starting with 'From' but with less than 3 words. Also, if we are only looking for the 'From' header line (and want to discard random lines in the body starting with From) we should check whether the 2nd word in the line is an email address:
  12.  
  13. fhand = open('mbox-short.txt') # The modified file contains the line "From here to the moon"
  14.  
  15. for line in fhand:
  16.     words = line.split()
  17.     #print 'Debug:', words
  18.     if words[0] != 'From' : continue
  19.     if words[1].find('@') == -1 : continue # discard random (body) lines starting with 'From'
  20.     if len(words) < 3 : continue # drop lines with less than 3 words
  21.     print words[2]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement