Writing binary files: a tutorial in C and Python (security@cambridge screencast)

Опубликовано: 24 Декабрь 2021
на канале: Frank Stajano Explains
1,092
33

A tutorial on writing binary files in C and Python, as a complement to my security course.
While I hope it will be helpful, is not an official lecture in the course.

00:00 Introduction
04:21 exercise 0 (also discussing line endings, viewing binary files etc)
17:47 exercise 1 (in C, also discussing size of ints, little-endian and big-endian)
32:48 exercise 2 (in C)
38:32 exercise 3 (in C)
44:48 exercise 4 (in C)
50:45 self-test (in C)
1:05:18 exercise 1 (in Python)
1:11:01 exercise 2 (in Python)
1:13:42 exercise 3 (in Python)
1:16:02 exercise 4 (in Python)
1:22:20 self-test (in Python)
1:29:49 Python one-liners

This is a leisurely-paced tutorial on writing binary files. This is a basic skill that all of you should possess before attending my forthcoming Security course. If you are able to complete the self-test below without difficulty, then you don't need to watch this video. I'll be using C and Python but feel free use any other tool you like.

Exercise 0
Write the characters of the string "Hello world" to a file.

Exercise 1
Write 1984 to a binary file as a C short int, then as a regular int, and as long int.

Exercise 2
Write -1984 and 1984 to a binary file as short, int and long.

Exercise 3
Write to a binary file the numbers from -10 to 10, first as signed shorts and then as signed longs. How long will the file be?

Exercise 4
Write a 128-byte binary file containing
At offset 96: the dotted quad 192.168.0.1 as 4-byte big-endian
Everywhere else: the byte 42

Self-test
Write a 537-byte-long binary file, containing
At byte offset 192: little-endian 64-bit repr of 3735928559;
At byte offset 299: null-terminated ASCII string "frankstajanoexplains.com";
At every offset divisible by 128: big-endian 32-bit repr of 3735928559;
At every other offset i (in 0..536): the byte (i + 100) mod 256

Reference answer for the self-test: http://stajano.com/goodies/wbf-self-t...

My home page: http://stajano.com

My security course playlist: (forthcoming)