Python Forum
modified file text getting cut off
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
modified file text getting cut off
#1
I am trying to modify the lines in a file. After the first few lines in a rules file, there are a lot of commented out rules, and it follows this format:
# alert tcp...
After running the script, I notice that the new file output is getting cut off. I checked the original file, and all of the content was there. So it seems somewhere my script is cutting text off at the end of the rules file. I've provided the function below that modifies such a rules file.

def updateFile (status, snortRulesFileName):
    print "working with file " + snortRulesFileName
    ruleFile = open(snortRulesFileName, 'r')
    w = open('output.txt', 'w')

    if status.lower() == "enable":
        for line in ruleFile:
            # Get the first 7 characters
            chars = line[:7]
            if chars == "# alert":
                    line = line[2:] # Remove first two beginning characters
            # Write the rule to another file
            w.write(line)

    if status.lower() == "disable":
        for line in ruleFile:
            # Add the comment chars
            # Get the first five characters of the line
            chars = line[:5]
            if chars == "alert":
                line = '# ' + line
                # print line
            w.write(line)

    # Copy the output file to the original file
    copyfile('output.txt', snortRulesFileName)
    # Then delete the output file
    os.remove('output.txt')
    w.close()
Reply
#2
Can you provide sample input and output? It's not clear from your post what your expected vs. actual result is ("cut off" can mean each line, the whole file, or potentially something I'm not thinking of).
Reply
#3
Sure

Error:
ERROR: /etc/snort/rules/protocol-voip.rules(336) Invalid configuration line: aler
In the modified file:


....
alert udp $EXTERNAL_NET any -> $SIP_SERVERS $SIP_PORTS (msg:"PROTOCOL-VOIP Known SIP scanner User-Agent detected"; flow:to_server; sip_header; content:"User-Agent: sipv"; fast_pattern:only; metadata:service sip; reference:url,blog.kolmisoft.com/sip-attack-friendly-scanner/; classtype:attempted-recon; sid:48317; rev:1;)
alert udp $EXTERNAL_NET any -> $SIP_SERVERS $SIP_PORTS (msg:"PROTOCOL-VOIP Known SIP scanner User-Agent detected"; flow:to_server; sip_header; content:"User-Agent: Gulp"; fast_pattern:only; metadata:service sip; reference:url,blog.kolmisoft.com/sip-attack-friendly-scanner/; classtype:attempted-recon; sid:48316; rev:1;)
aler[EOF]


In the original file:

...
# alert udp $EXTERNAL_NET any -> $SIP_SERVERS $SIP_PORTS (msg:"PROTOCOL-VOIP Known SIP scanner User-Agent detected"; flow:to_server; sip_header; content:"User-Agent: sipv"; fast_pattern:only; metadata:service sip; reference:url,blog.kolmisoft.com/sip-attack-friendly-scanner/; classtype:attempted-recon; sid:48317; rev:1;)
# alert udp $EXTERNAL_NET any -> $SIP_SERVERS $SIP_PORTS (msg:"PROTOCOL-VOIP Known SIP scanner User-Agent detected"; flow:to_server; sip_header; content:"User-Agent: Gulp"; fast_pattern:only; metadata:service sip; reference:url,blog.kolmisoft.com/sip-attack-friendly-scanner/; classtype:attempted-recon; sid:48316; rev:1;)
# alert udp $EXTERNAL_NET any -> $SIP_SERVERS $SIP_PORTS (msg:"PROTOCOL-VOIP Known SIP scanner User-Agent detected"; flow:to_server; sip_header; content:"User-Agent: SIVuS"; fast_pattern:only; metadata:service sip; reference:url,blog.kolmisoft.com/sip-attack-friendly-scanner/; classtype:attempted-recon; sid:48315; rev:1;)
...
Reply
#4
*BUMP*
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PDF properties doesn't show created or modified date Pedroski55 4 1,076 Jun-19-2023, 08:09 AM
Last Post: Pedroski55
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,114 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,658 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Converted Pipe Delimited text file to CSV file atomxkai 4 6,961 Feb-11-2022, 12:38 AM
Last Post: atomxkai
  append a string to a modified line Mr_Blue 10 3,848 Sep-16-2021, 07:24 PM
Last Post: Mr_Blue
  [split] How to convert the CSV text file into a txt file Pinto94 5 3,328 Dec-23-2020, 08:04 AM
Last Post: ndc85430
  How to rename a CSV file by adding MODIFIED in the filename? Python_User 25 8,106 Dec-13-2020, 12:35 PM
Last Post: Larz60+
  Saving text file with a click: valueerror i/o operation on closed file vizier87 5 4,394 Nov-16-2020, 07:56 AM
Last Post: Gribouillis
  saving data from text file to CSV file in python having delimiter as space K11 1 2,398 Sep-11-2020, 06:28 AM
Last Post: bowlofred
  Web Form to Python Script to Text File to zip file to web wfsteadman 1 2,133 Aug-09-2020, 02:12 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020