Installing and Using Pintools and angr on Linux and Windows

Some reversing challenges are more tedious to work through by hand than they need to be. Pintools can help by measuring what a program does for different inputs. I first tried installing it on Windows, ran into command-line and library trouble, and eventually moved to Linux. These are the steps and snags I recorded along the way.

Linux

Download and extract the source

Start by downloading the Pintools source package from Intel.

On Linux, wget will fetch it; unpack the archive with tar -zxvf pin-xxxxxx.tar.gz.

Compile the instruction-counting tool

You will also need make. It is present on most systems; if yours is missing it, install it with sudo apt-get install make.

The sample tools inscount0, inscount1, and inscount2 count the instructions executed during a run. That count can be useful when a challenge checks an input one character at a time. Build the 64-bit sample with:

1
2
cd source/tools/ManualExamples
make obj-intel64/inscount0.so TARGET=intel64

For a 32-bit target, build the corresponding version instead:

1
make obj-ia32/inscount0.so

By default, inscount0.cpp writes the count to a file. I wanted a script to read the result directly, so I changed the output in Fini before recompiling:

1
2
3
4
5
6
7
8
41 VOID Fini(INT32 code, VOID *v)
42 {
43 // Write to a file since cout and cerr maybe closed by the application
44 // OutFile.setf(ios::showbase);
45 // OutFile << "Count " << icount << endl;
46 // OutFile.close();
47 std::cout << "Count:" << icount << std::endl;
48 }

Comment out the file-writing lines, add the cout line with whatever label you prefer, and rebuild. Then run the resulting .so through Pin:

1
pin -t <yourpintool> -- <executable>

The count now appears in the program’s output. For example:

1
pin -t myinscout.so -- /bin/sh -c exit

prints something like:

1
Count:1089205

That makes it possible to automate a challenge. In Xiangyun Cup re1, the input is checked character by character, so I tried candidates in turn and watched the instruction count for changes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from subprocess import Popen, PIPE
from sys import argv
import string

pinPath = '/home/damaoooo/Downloads/pin-3.16-98275-ge0db48c31-gcc-linux/pin'
toolPath = '/home/damaoooo/Downloads/pin-3.16-98275-ge0db48c31-gcc-linux/source/tools/ManualExamples/obj-intel64/myinscount0.so'
elfPath = '/home/damaoooo/test/main'
pinRead = lambda : p.communicate()[0]
pinWrite = lambda cont : p.stdin.write(cont.encode())

table = '0123456789abcdef'
flag = ''
if __name__ == '__main__':
last = 0
found = 0
last_letter = ''
for idx in range(38 - 6):
for i in table:
if i == last_letter:
continue
found = 0
p = Popen([pinPath, '-t', toolPath, '--', elfPath], stdin=PIPE, stdout=PIPE)
s = 'flag{%s}'%(flag + i*(38 - 6 - len(flag)))+'\n'
pinWrite(s)
now = int(pinRead().split('Count:'.encode())[1])
delta = now - last
print('input{} -> ins{} -> delta({})'.format(s[:-1], now, now - last))
last = now
last_letter = i
if delta > 0 and delta < 10:
flag += i
found = 1
last_letter = ''
break
if found == 0:
flag = flag[:-1]


print(flag)

The script starts a fresh process for each candidate with subprocess, writes the candidate to stdin, and reads the count back from stdout.

Windows

Windows was a much less pleasant experience.

Downloading and modifying the source was not the hard part; getting it to compile with make on Windows was.

I installed Visual Studio and Cygwin’s make, configured the environment, and still could not get the build to succeed.

Other blogs also reported different instruction counts for identical inputs on Windows. At that point, Linux looked much more attractive for this job.

Eventually I gave up on the Windows Pintools setup.

For a Windows challenge, I turned to angr instead.

angr

Install it with pip install angr.

angr supports Python 3 now, so there is no need to follow the old Python 2 setup guides. The example below creates a project and initial state, explores toward a success address while avoiding a failure address, and prints the input it finds:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import angr

# Create a project; auto_load_libs made little difference in my case
proj = angr.Project('<execute>', auto_load_libs=False)

# Create a simulation state
state = proj.factory.entry_state()

# Create a simulation manager
simgr = proj.factory.simgr(state)

# Explore
simgr.explore(find=0x54444444, avoid=0x400000)

# Print the result
print (simgr.found[0].posix.dumps(0))