📖
CTF Wiki
  • 🚩Arne's CTF Writeups!
  • 2025
    • TUCTF
      • Forensics - Security Rocks
    • San Diego CTF
      • Crypto - RustSA
      • Misc - Triglot
  • 2024
    • Lexington CTF
      • Misc - a little bit of tomcroppery
    • Imaginary CTF
      • Web - Journal
    • Space Heroes CTF
      • Web - Antikythera
    • HTB Cyber Apocalypse
      • Pwn - Sound of Silence
      • Misc - MultiDigilingual
  • 2023
    • NahamConCTF
      • Mobile - Red Light Green Light
    • BucketCTF
      • Rev - Schematic
      • Rev - Random security
    • HTB Cyber Apocalypse
      • Rev - Cave System
      • Rev - Somewhat Linear
      • Pwn - Void
  • 2022
    • DownUnderCTF 2022
      • Cloud - Jimmy Builds a Kite
    • Ã¥ngstromCTF 2022
      • Pwn - really obnoxious problem
      • Pwn - whatsmyname
    • Engineer CTF
      • Misc - Not really random
      • Misc - Broken Pieces
    • KnightCTF 2022
    • HTB CTF: Dirty Money
      • Forensics - Perseverance
  • 2021
    • MetaCTF CyberGames 2021
    • HTB - Cyber Santa
      • RE - Infiltration
    • Securebug CTF Thor 2021
      • Web - Tricks 1
      • Web - Tricks 2
      • RE - Hidden in Plain Sight
    • TFC CTF 2021
      • RE - Crackity
      • Pwn - Jumpy
      • Misc - Weird Friend
    • K3RN3L CTF 2021
      • Crypto - Pascal RSA
    • DamCTF 2021
      • Misc - library-of-babel
      • Pwn - cookie-monster
    • Killer Queen CTF 2021
      • Pwn - Tweety Birb
      • Forensics - Tippy Tappies
      • Pwn - I want to break free
    • BuckeyeCTF 2021
      • Web - pay2win
      • Misc - USB Exfiltration
Powered by GitBook
On this page
  • Description
  • Downloads
  • Solution
  1. 2025
  2. San Diego CTF

Misc - Triglot

Last updated 11 days ago

Description

Downloads

Solution

The challenge is straight forward - write a Perl-Python-JavaScript file-read polyglot. Firstly, let's write the file-read code for each language:

Python

print(open('flag.txt').read())

Perl

open(my $file, 'flag.txt');print(<$file>);

JavaScript

console.log(require('fs').readFileSync('flag.txt','utf8'))

Combining Perl+Python:

q="""=;open(my $file, 'flag.txt');print(<$file>);#""";print(open('flag.txt').read())

This is simple, put the python code behind the '#' symbol and it will be treated as a comment by Perl. Similarly in python, the Perl code is treated as comment when put between the triple double quotes. Here's the hard part - that is invalid JavaScript.

Anyway, I modified the validation script slightly such that it prints the individual interpreter's output:

#!/usr/bin/env bash
set -eo pipefail

wrong() {
  printf "EXTREMELY LOUD INCORRECT BUZZER!!!\n"
}
trap wrong ERR

code=$(cat)

printf '%s' "$code" | perl -c
printf '%s' "$code" | python3 -c 'import sys,ast; ast.parse(sys.stdin.read())'
printf '%s' "$code" | node -e "const fs=require('fs'), src=fs.readFileSync(0,'utf8'); require('vm').createScript(src)"

perl_out=$(printf '%s' "$code" | perl -)
py_out=$(printf '%s' "$code" | python3 -)
js_out=$(printf '%s' "$code" | node -)

printf 'Perl output:\n%s\n\n' "$perl_out"
printf 'Python output\n%s\n\n' "$py_out"
printf 'JS output\n%s\n\n' "$js_out"

if [[ "$perl_out" == "$py_out" && "$perl_out" == "$js_out" ]]; then
  printf "Your triglot compiles!! Here's your output:\n"
  printf '%s\n' "$py_out"
else
  exit 1
fi

My final script is:

0//1;q=0;print(open('flag.txt').read());'''=;open(my $file, 'flag.txt');print(<$file>);q[
for(x=1,z=(a='')=>a;x;)console.log(require('fs').readFileSync('flag.txt','utf8')+(x--,z()))//]#'''

As you can see from the syntax highlighting, it mostly made use of '//' in JavaScript to comment most of the other code out. Other thing worth mentioning is the odd for loop. To be honest, I still don't quite understand how this loop works. There are only 2 expressions (notice there's only 2 semicolons) and I'd expect the 2nd expression to be a condition in a typical loop but it's not. The decrement is also done in the function itself rather than the 'for' statement. If anyone could explain to me how this works do reach out to me :)

This is a tough one with very little solves but I still attempted it because I did a similar challenge in HTB last year . I thought I could breeze past this real quick but it turned out to be more challenging than I thought.

I spent a good maybe 5 hours going nowhere until I found this post, .

here
https://codegolf.stackexchange.com/questions/209420/inspired-by-hq9-4-behavior-polyglot/209521#209521
648B
validate.sh
Node error