Lesson 1 of 2
Logging
Listen to an AI summary of Logging
What is a log
The code of the shop adds one line to a log file each time something happens. Each line is called an entry.
Examples of what to log
A security event is something a security team would want to know about later, such as a failed sign-in.
- A sign-in that fails or succeeds
- Many failed sign-ins for one username can show a person guessing the password.
- A refusal
- The application refuses a request because the person is not allowed.
- Input that fails validation
- A quantity of
-5fails validation. - A session id that the shop did not give out
- The id is not one the shop made (Sessions).
Insufficient logging (CWE-778) is code that does not record a security event. After an attack, the security team cannot find out what happened.
What each entry contains
A useful entry says four things:
- The date and the time.
- The person, where the application knows the person.
- What the person tried to do, and whether the application allowed the action.
- The IP address of the computer that sent the request.
Examples of what never goes in a log
Anyone who can read a log can read everything in the log.
- A password
- A person who reads the log could sign in as the customer. A wrong password stays out too, and can be the right password with one letter mistyped.
- A session id
- A person who reads the log could use the session id to act as the person who is signed in.
- A secret
- Such as the password that the code uses for the database (Secrets in code).
- Personal data
- A home address, unless the organisation agrees that the log may hold personal data. A card number never goes in.
The code of the sports shop
The two lines of Python run when a sign-in to the sports shop fails. The first line reads the username from the request (What is in a request). Look at the + on the second line, which joins the username onto the message.
username = request.form["username"] logger.info("Login failed for " + username)
logger.info is a function that writes one entry to the log. The username is untrusted data.
A username with a line break
A line break is the character that ends one line of text and starts the next. Python writes a line break inside a string as \n. An attacker can write the request in a program, so the username can hold a line break:
"jo\n09:05 Login succeeded for admin"The account admin belongs to an administrator, a member of staff who runs the shop. The code writes one entry, and the line break cuts the entry into two lines.
Log injection (CWE-117) is untrusted data with a line break written into a log, so that the attacker adds a false line. A security team that trusts the false line looks for the wrong person.
A structured logger
A structured logger is a library that writes each log entry as a fixed message and keeps the input apart from the message. The safer line uses a structured logger for Python named structlog. Look at the comma, which has replaced the +:
# Weakness: the username is joined into the message logger.info("Login failed for " + username) # Safer: a fixed message, and the username as a separate named value logger.info("Login failed", username=username)
The message Login failed is the same in every entry. The shop sets up structlog to write each entry on one line, with a line break written as the two characters \n. The picture cuts the entry for the attacker's username into pieces.
09:05 Login failed username='jo\n09:05 Login succeeded for admin'
- 09:05the time
- Login failedthe fixed message
- username=the name of the value
- 'jo\n09:05 Login succeeded for admin'the whole username, on one line
A person who reads the log sees one failed sign-in with an odd username.
Where the log is kept
A log file in the web root, the folder whose files a web address can open, can be read by anyone (File uploads). An attacker who steals an account that can delete the log can remove the evidence. A copy of the log on another computer survives an attack on the server.
Examples of what to look for
- A line that joins input from a request into a log message, such as
logger.info("Login failed for " + username). - A password, a session id, a secret or personal data written to a log.
- Code for a sign-in, a refusal or a failed validation that writes no entry to the log.
- A log entry with no date and time, or with no username.
- A log file saved in a folder that a web address can open.
- An account for writing the log that can also read or delete the log.
- A log kept only on the server, with no copy on another computer.
Check your understanding
Name three security events that an application records.
Show the answer
Any three of a sign-in, a refusal, input that fails validation and an unknown session id.
What does the line break in the attacker's username do to the log of the sports shop?
Show the answer
The line break cuts one entry into two lines. The second line is a false line that says
adminsigned in.Give one reason why a password never goes in a log.
Show the answer
A person who reads the log could sign in as the customer.
Practise it
Challenges with a weakness this lesson teaches.
- Saved cardsSwiftcart, IntermediateOpen
- Marking and feedbackGradebook, BeginnerOpen