In-Class Activity: The RDTSC Timing Check
- How this runs
- The specimen
- Step 1 (5 min): measure the baseline
- Step 2 (6 min): find the check
- Step 3 (5 min): trip it, then prove what tripped it
- Step 4 (7 min): bypass it twice
- Step 5 (optional, 3 min): the same check, packed
- What to write down
- What this does not teach you
- References
One technique, worked end to end, in twenty-five minutes: the RDTSC timing
check from Anti-Analysis Techniques and
Week 7.
Timing checks are the right one to practise on. They need no API call, so there is nothing for an API monitor to log and nothing to hook. They need no imports, so a static pass does not flag them. And nearly every student arrives believing the wrong thing about them — that attaching a debugger is what gets caught. It is not. Stopping is what gets caught, and that distinction changes how you work.
How this runs
- In pairs. One person drives, one takes notes; swap after Step 3.
- Twenty-five minutes. The step budget below adds to 25 with three minutes of slack. If a step overruns, skip Step 5 — it is the extension.
- Everything here is reproducible. No malware sample is involved. The specimen is a 137-line C program you compile yourself, and every number on this page came from running it. Yours will differ, and the way in which they differ is part of the point.
- The deliverable is five lines of notes, listed at the bottom. They feed the anti-analysis section of the final report, which asks for exactly this: a technique identified, explained, and bypassed, with evidence.
The specimen
Two files, which go in one directory of their own — this is a tiny build, not a project:
timecheck.c— the specimen itself, 137 lines. The source is published on purpose: this is a teaching specimen, not a crackme, and the skill being practised is measuring and defeating the check, not finding it in the dark.Makefile— the build, plus thedisastarget Step 2 uses. Build withmake, not with agccline of your own: every flag in it is load-bearing and commented, and the wrong optimisation level inlines the check away and ruins the exercise.
Save both with exactly those names — the Makefile builds timecheck.c by
name. A .c file often opens in the browser instead of downloading, in which
case save it from there (Ctrl+S, or right-click the link →
Save link as).
To pull them straight onto the machine you are actually working on — which is what you want if that is a VM or a box you are SSH’d into — skip the browser:
$ mkdir ~/timecheck && cd ~/timecheck
$ curl -O "https://web.cecs.pdx.edu/~dmcgrath/courses/F26/malware/timecheck/{timecheck.c,Makefile}"
curl’s brace expansion fetches both in one command, and -O keeps the
server’s filename for each, so they land correctly named. wget works too, one
URL at a time.
You need gcc, make, gdb and objdump (and upx for the optional Step 5).
The course Linux VM has all of them; so does any ordinary Linux install. The
specimen touches nothing outside its own directory and never uses the network,
so this does not have to run in the isolated malware lab VM.
Then, in that directory:
$ make
gcc -O1 -g -fno-inline -fno-pie -no-pie -Wall -Wextra -o timecheck timecheck.c
$ ./timecheck
timecheck: stage 2 target is c2.flow-7.timecheck.invalid:8443
That string is XOR-encoded in the binary and decoded at run time, so it exists in cleartext only in process memory — which is why you want the program to reach that code with you watching, rather than stepping around the check.
When the check fires instead, you get this, and nothing else:
$ ./timecheck
timecheck: no configuration found, nothing to do
No crash, no error, exit status 0. A sample that announces “debugger detected” has told you where to look; this one has not.
TIMECHECK_DEBUGis the one unrealistic thing here. Setting it makes the specimen print its own measurement, which no real sample would ever do. It exists so Step 1 can collect a baseline without a debugger attached. Everything else on this page transfers to real samples; that does not.
Step 1 (5 min): measure the baseline
Collect the delta with nothing attached. Run it five times — timing numbers are distributions, not values.
$ for i in 1 2 3 4 5; do TIMECHECK_DEBUG=1 ./timecheck >/dev/null; done
[debug] t0=755298633005170 t1=755298633005358 delta=188 threshold=50000
[debug] t0=... delta=202 threshold=50000
[debug] t0=... delta=208 threshold=50000
[debug] t0=... delta=224 threshold=50000
[debug] t0=... delta=242 threshold=50000
Those five are from an Intel Core Ultra 9 285H. Write down your own range and your CPU model — both go on the board at the end.
Then answer two questions in your notes:
- The threshold is 50,000 cycles and your delta is in the low hundreds. How many times over budget would the check have to run before it tripped?
grepyour/proc/cpuinfoflags forconstant_tsc. It is there. What does that mean for whetherRDTSCis measuring work or wall-clock time?
The answer to the second one is the whole technique: with constant_tsc the
counter advances at a fixed rate no matter what the core is doing, so the
delta is a clock reading. A debugger does not make the loop slower. It makes
you slow, and the counter notices you, not the debugger.
Step 2 (6 min): find the check
Do this before reading the source — that is the version of the skill that
transfers. Two RDTSC instructions, a subtraction, and a comparison against a
hard-coded constant.
$ make disas
The pattern to look for, in order:
| Look for | Why |
|---|---|
Two rdtsc instructions in one function |
A single read measures nothing |
shl rdx, 0x20 / or after each |
Reassembling EDX:EAX into one 64-bit value |
A sub between the two reads’ results |
The delta |
A cmp against a round-looking immediate |
The threshold |
What you should have found (addresses from one particular build)</summary>
0000000000401180 <environment_is_clean>:
401190: rdtsc <- first read
401192: shl rdx,0x20
401196: mov eax,eax
40119b: or rbx,rax <- t0 in rbx
...
4011c0: mov edx,eax <- the loop being timed
4011c2: imul edx,eax
4011c5: add DWORD PTR [rsp+0xc],edx
4011cf: jne 4011c0
4011d1: rdtsc <- second read
4011e6: sub r12,rbx <- delta = t1 - t0, in r12
40121d: cmp r12,0xc350 <- threshold, 0xc350 = 50000
401224: setbe al <- the verdict, into eax
0xc350 is 50,000. Finding that constant is most of the exercise: a
round-numbered immediate compared against a timing delta is the signature of
this entire technique, in any binary.
Note what the compiler did at 401224 — there is no conditional jump here.
-O1 turned the comparison into setbe, so the verdict leaves the function as
a return value in EAX and the branch happens in main instead. Expect that.
The check and the branch on its result are frequently not in the same place.
Your addresses will differ. This build is GCC 16.2.1; a different compiler
version moves everything. The pattern does not move.
</details>
Step 3 (5 min): trip it, then prove what tripped it
Under GDB, with no breakpoints at all:
$ gdb -q ./timecheck
(gdb) run
timecheck: stage 2 target is c2.flow-7.timecheck.invalid:8443
[Inferior 1 (process 1823760) exited normally]
The payload ran. The check did not fire, with a debugger plainly attached
to the process. Now put one breakpoint inside the timed window — anywhere
between the two reads — and continue:
(gdb) break timecheck.c:108
(gdb) run
Breakpoint 1, environment_is_clean () at timecheck.c:108
108 acc += (uint32_t)(i * i);
(gdb) delete breakpoints
(gdb) continue
timecheck: no configuration found, nothing to do
Detected. Now move the same breakpoint after the second read — on the
comparison — and read the delta out of the register:
(gdb) break *0x40121d
(gdb) run
Breakpoint 1, environment_is_clean () at timecheck.c:122
122 if (delta > THRESHOLD)
(gdb) printf "delta = %llu\n", $r12
delta = 224
Clean, and 224 is exactly the no-debugger baseline from Step 1.
Here is the measured version of the whole step, on the reference machine:
Condition
Delta, in cycles
Verdict
No debugger at all, 5 runs
188 – 242
clean
Under GDB, no breakpoints
224
clean
Breakpoint after the second read
224
clean
One breakpoint stop inside the window
394,738
detected
Stopping inside, then reading the screen for 3 s
11,067,006,850
detected
Three seconds of human thought is eleven billion cycles — 220,000 times the
threshold. The margin is not subtle, and it is the reason the first and
cheapest counter to a timing check is breakpoint placement. Put your stops
outside the window and the check answers correctly, with you watching.
Divide 11,067,006,850 by 3 and you get the TSC’s nominal frequency, about
3.7 GHz. Do that division on your own number and write down what you get; it
is a useful habit, because it converts any timing threshold you ever find into
seconds.
Step 4 (7 min): bypass it twice
Every check has two surfaces: the measurement and the verdict. Attack
each one once, and notice which of the two you would want in front of a real
sample.
Bypass A — lie about the measurement
Stop on the comparison and rewrite the delta before it is read:
(gdb) break timecheck.c:108 # stop inside the window, on purpose
(gdb) run
(gdb) delete breakpoints
(gdb) break *0x40121d # the cmp
(gdb) continue
(gdb) printf "measured %llu\n", $r12
measured 1475537
(gdb) set $r12 = 0
(gdb) delete breakpoints
(gdb) continue
timecheck: stage 2 target is c2.flow-7.timecheck.invalid:8443
With symbols you can do the same thing by name, which is worth seeing once
because real samples never let you:
(gdb) print delta
$2 = 394738
(gdb) set var delta = 0
(gdb) print delta
$3 = 0
Precise, and brittle: it needs the address 0x40121d and the register r12,
and both change the next time the file is compiled.
Bypass B — lie about the verdict
environment_is_clean() returns its answer in EAX, like every other function
returning int under the SysV ABI. So let it measure whatever it likes — let it
catch you, in fact — and overwrite the answer on the way out:
(gdb) break environment_is_clean
(gdb) run
Breakpoint 1, environment_is_clean () at timecheck.c:99
99 {
(gdb) next 4 # four steps puts you inside the timed window
108 acc += (uint32_t)(i * i);
(gdb) shell sleep 2 # or just sit here and think for a moment
(gdb) finish
0x00000000004012b1 in main () at timecheck.c:130
130 if (!environment_is_clean()) {
Value returned is $1 = 0
(gdb) print $eax
$2 = 0
(gdb) set $eax = 1
(gdb) print $eax
$3 = 1
(gdb) delete breakpoints
(gdb) continue
timecheck: stage 2 target is c2.flow-7.timecheck.invalid:8443
Follow what happened there. The two-second pause inside the window guarantees
the check measures billions of cycles and concludes, correctly, that it is
being analysed — Value returned is $1 = 0 is it saying exactly that. Then
finish stops in main on the test eax, eax that has not executed yet,
so EAX still holds the verdict and is still writable. One assignment, and the
branch goes the other way.
No addresses. No register names specific to one build. Survives
recompilation.
This is the generalisation worth keeping. Find the function that
decides, let it decide, and lie about the result. It works on
IsDebuggerPresent wrappers, VM checks, licence checks and C2 gates alike,
and it does not care how the decision was reached.
For the third approach — patching the branch rather than the data — the
conditional jump in main at 0x4012b3 is two bytes, 74 25. Overwrite both
with 0x90 and the evade path becomes unreachable:
(gdb) set *(unsigned char *)0x4012b3 = 0x90
(gdb) set *(unsigned char *)0x4012b4 = 0x90
(gdb) x/4i 0x4012ac
0x4012ac <main+4>: call 0x401180 <environment_is_clean>
0x4012b1 <main+9>: test %eax,%eax
0x4012b3 <main+11>: nop
0x4012b4 <main+12>: nop
That one is the model for a persistent patch: the same two bytes written to
the file on disk with a hex editor give you a binary that no longer checks, for
every future run.
Step 5 (optional, 3 min): the same check, packed
If there is time, see what packing does to everything you just did:
$ cp timecheck timecheck.upx && upx -q timecheck.upx
20448 -> 9656 47.22% linux/amd64 timecheck.upx
$ objdump -d timecheck.upx | grep -c rdtsc
0
$ readelf -S timecheck.upx | head -3
There are no sections in this file.
$ upx -d timecheck.upx -o timecheck.unpacked && cmp timecheck timecheck.unpacked
$ objdump -d timecheck.unpacked | grep -c rdtsc
2
The check did not go anywhere. It is simply no longer something a disassembler
can see — which is the point of Part 4 of the lecture, and the reason packing
and anti-debugging are usually shipped together.
What to write down
Five lines, in your own notes:
- Your baseline delta range, your CPU model, and your derived TSC frequency.
- The address of the
cmp in your build, and the threshold constant.
- Which breakpoint placements tripped the check and which did not.
- The two bypasses, as the exact commands you ran.
- One sentence on which bypass you would reach for first against a real
sample, and why.
Item 5 is the one worth arguing about with your partner. There is a defensible
answer in both directions, and the argument is the reason this is a classroom
activity rather than a handout.
What this does not teach you
Being honest about the limits, since all three come up in the final project:
- This threshold is generous. 50,000 cycles with a 200-cycle window is a
margin a real author would not leave. Thresholds in the low thousands are
common, and some samples compare against a value they computed earlier in
the same run rather than a constant — no round immediate to grep for.
- One check is not the hard case. Commodity loaders ship dozens, scattered
and re-checked. At that point the answer is not patching each one; it is
emulation (Qiling) or a hooking library.
- Timing is also used against VMs, not just debuggers, by measuring the
cost of an instruction that forces a VM exit. That is a different and much
noisier measurement, and TSC offsetting in the hypervisor is a direct
counter to it. The Black Hat talk in the references is the readable
treatment.
References
- Course page — Anti-Analysis Techniques
- Course page — Week 7: Anti-Analysis Techniques
- Lecture — the Week 7 slides, shown in class; the prose version of everything
in them is the two course pages above
- Specimen —
timecheck.c and
Makefile
- Anti-Debug: Timing checks
— Check Point’s encyclopedia entry for this family, with working code
- Anti-Debug Tricks encyclopedia — the
rest of the catalogue
- Evasions encyclopedia — the anti-VM and
anti-sandbox companion
- al-khaser — open-source
collection of anti-analysis proofs of concept, including the timing family
- ATT&CK T1622: Debugger Evasion
- My Ticks Don’t Lie: New Timing Attacks for Hypervisor Detection
— D’Elia et al., Black Hat EU 2020, on timing against hypervisors
- Intel SDM, Vol. 2B, RDTSC
— the instruction reference, including why it is not serializing
Maintenance note: the numbers on this page were measured on 2026-11-11
with GCC 16.2.1, GDB 18.1 and UPX 5.2.0, on an Intel Core Ultra
9 285H. Every address (0x40121d, 0x4012b3) and the register r12 belong
to that one build and will not survive a compiler change — make disas is
the authoritative version for whatever is in front of you, and the page says
so at each address. The cycle counts are hardware-specific by design; the
activity collects the class’s spread deliberately. The threshold constant
0xc350 is in timecheck.c and changes only if that file does. The curl
command in The specimen spells out the full URL, including the term, so it
needs updating when the site rolls over to a new term; the two links beside
it are relative and do not.
Related course pages: Anti-Analysis Techniques ·
Week 7: Anti-Analysis Techniques ·
Advanced Dynamic Analysis ·
bomb3 pwndbg walkthrough ·
Qiling · Final Project
0000000000401180 <environment_is_clean>:
401190: rdtsc <- first read
401192: shl rdx,0x20
401196: mov eax,eax
40119b: or rbx,rax <- t0 in rbx
...
4011c0: mov edx,eax <- the loop being timed
4011c2: imul edx,eax
4011c5: add DWORD PTR [rsp+0xc],edx
4011cf: jne 4011c0
4011d1: rdtsc <- second read
4011e6: sub r12,rbx <- delta = t1 - t0, in r12
40121d: cmp r12,0xc350 <- threshold, 0xc350 = 50000
401224: setbe al <- the verdict, into eax
0xc350 is 50,000. Finding that constant is most of the exercise: a
round-numbered immediate compared against a timing delta is the signature of
this entire technique, in any binary.401224 — there is no conditional jump here.
-O1 turned the comparison into setbe, so the verdict leaves the function as
a return value in EAX and the branch happens in main instead. Expect that.
The check and the branch on its result are frequently not in the same place.$ gdb -q ./timecheck
(gdb) run
timecheck: stage 2 target is c2.flow-7.timecheck.invalid:8443
[Inferior 1 (process 1823760) exited normally]
(gdb) break timecheck.c:108
(gdb) run
Breakpoint 1, environment_is_clean () at timecheck.c:108
108 acc += (uint32_t)(i * i);
(gdb) delete breakpoints
(gdb) continue
timecheck: no configuration found, nothing to do
(gdb) break *0x40121d
(gdb) run
Breakpoint 1, environment_is_clean () at timecheck.c:122
122 if (delta > THRESHOLD)
(gdb) printf "delta = %llu\n", $r12
delta = 224
(gdb) break timecheck.c:108 # stop inside the window, on purpose
(gdb) run
(gdb) delete breakpoints
(gdb) break *0x40121d # the cmp
(gdb) continue
(gdb) printf "measured %llu\n", $r12
measured 1475537
(gdb) set $r12 = 0
(gdb) delete breakpoints
(gdb) continue
timecheck: stage 2 target is c2.flow-7.timecheck.invalid:8443
(gdb) print delta
$2 = 394738
(gdb) set var delta = 0
(gdb) print delta
$3 = 0
0x40121d and the register r12,
and both change the next time the file is compiled.environment_is_clean() returns its answer in EAX, like every other function
returning int under the SysV ABI. So let it measure whatever it likes — let it
catch you, in fact — and overwrite the answer on the way out:(gdb) break environment_is_clean
(gdb) run
Breakpoint 1, environment_is_clean () at timecheck.c:99
99 {
(gdb) next 4 # four steps puts you inside the timed window
108 acc += (uint32_t)(i * i);
(gdb) shell sleep 2 # or just sit here and think for a moment
(gdb) finish
0x00000000004012b1 in main () at timecheck.c:130
130 if (!environment_is_clean()) {
Value returned is $1 = 0
(gdb) print $eax
$2 = 0
(gdb) set $eax = 1
(gdb) print $eax
$3 = 1
(gdb) delete breakpoints
(gdb) continue
timecheck: stage 2 target is c2.flow-7.timecheck.invalid:8443
Value returned is $1 = 0 is it saying exactly that. Then
finish stops in main on the test eax, eax that has not executed yet,
so EAX still holds the verdict and is still writable. One assignment, and the
branch goes the other way.This is the generalisation worth keeping. Find the function that
decides, let it decide, and lie about the result. It works on
IsDebuggerPresent wrappers, VM checks, licence checks and C2 gates alike,
and it does not care how the decision was reached.
main at 0x4012b3 is two bytes, 74 25. Overwrite both
with 0x90 and the evade path becomes unreachable:(gdb) set *(unsigned char *)0x4012b3 = 0x90
(gdb) set *(unsigned char *)0x4012b4 = 0x90
(gdb) x/4i 0x4012ac
0x4012ac <main+4>: call 0x401180 <environment_is_clean>
0x4012b1 <main+9>: test %eax,%eax
0x4012b3 <main+11>: nop
0x4012b4 <main+12>: nop
$ cp timecheck timecheck.upx && upx -q timecheck.upx
20448 -> 9656 47.22% linux/amd64 timecheck.upx
$ objdump -d timecheck.upx | grep -c rdtsc
0
$ readelf -S timecheck.upx | head -3
There are no sections in this file.
$ upx -d timecheck.upx -o timecheck.unpacked && cmp timecheck timecheck.unpacked
$ objdump -d timecheck.unpacked | grep -c rdtsc
2
cmp in your build, and the threshold constant.timecheck.c and
MakefileMaintenance note: the numbers on this page were measured on 2026-11-11
with GCC 16.2.1, GDB 18.1 and UPX 5.2.0, on an Intel Core Ultra
9 285H. Every address (0x40121d, 0x4012b3) and the register r12 belong
to that one build and will not survive a compiler change — make disas is
the authoritative version for whatever is in front of you, and the page says
so at each address. The cycle counts are hardware-specific by design; the
activity collects the class’s spread deliberately. The threshold constant
0xc350 is in timecheck.c and changes only if that file does. The curl
command in The specimen spells out the full URL, including the term, so it
needs updating when the site rolls over to a new term; the two links beside
it are relative and do not.