Menu

1 / 2

Day 5/90: String Manipulation for Security -- Python Security

DEV Community·Naveen Karasu·6 months ago
#lIFnRzfy
Reading 0:00
15s threshold

Day 5/90: String Manipulation for Security

90 Day Python Security Scripting Challenge

Strings are the fundamental unit of security analysis. Today I built tools for the string operations that come up during incident response, malware triage, and web application testing.

Multi-Encoding Log Reader

During incident response, you pull logs from systems running different OSes and locales. A function that tries encodings in strict mode and falls back gracefully prevents silent data corruption.

def read_log_safely ( path ): """ Read log files trying common encodings in order. """ for encoding in [ " utf-8 " , " latin-1 " , " cp1252 " , " ascii " ]: try : with open ( path , encoding = encoding , errors = " strict " ) as fh : lines = fh . readlines () return lines , encoding except ( UnicodeDecodeError , UnicodeError ): continue with open ( path , encoding = " utf-8 " , errors = " replace " ) as fh : return fh .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.