CTF Challenge: dec2ascii
While practicing for PicoCTF, I ran into a problem: at the end of an exercise the flag was a string of numbers that I guessed were decimal values of letters in the ASCII table.
So I needed to convert the decimal representation of the characters to ASCII text, but I couldn’t find a CLI tool that could accomplish this task.
Obviously I could do this manually, using ascii
which prints an ASCII table
Dec Hex Dec Hex Dec Hex Dec Hex Dec Hex Dec Hex Dec Hex Dec Hex
0 00 NUL 16 10 DLE 32 20 48 30 0 64 40 @ 80 50 P 96 60 ` 112 70 p
1 01 SOH 17 11 DC1 33 21 ! 49 31 1 65 41 A 81 51 Q 97 61 a 113 71 q
2 02 STX 18 12 DC2 34 22 " 50 32 2 66 42 B 82 52 R 98 62 b 114 72 r
3 03 ETX 19 13 DC3 35 23 # 51 33 3 67 43 C 83 53 S 99 63 c 115 73 s
4 04 EOT 20 14 DC4 36 24 $ 52 34 4 68 44 D 84 54 T 100 64 d 116 74 t
5 05 ENQ 21 15 NAK 37 25 % 53 35 5 69 45 E 85 55 U 101 65 e 117 75 u
6 06 ACK 22 16 SYN 38 26 & 54 36 6 70 46 F 86 56 V 102 66 f 118 76 v
7 07 BEL 23 17 ETB 39 27 ' 55 37 7 71 47 G 87 57 W 103 67 g 119 77 w
8 08 BS 24 18 CAN 40 28 ( 56 38 8 72 48 H 88 58 X 104 68 h 120 78 x
9 09 HT 25 19 EM 41 29 ) 57 39 9 73 49 I 89 59 Y 105 69 i 121 79 y
10 0A LF 26 1A SUB 42 2A * 58 3A : 74 4A J 90 5A Z 106 6A j 122 7A z
11 0B VT 27 1B ESC 43 2B + 59 3B ; 75 4B K 91 5B [ 107 6B k 123 7B {
12 0C FF 28 1C FS 44 2C , 60 3C < 76 4C L 92 5C \ 108 6C l 124 7C |
13 0D CR 29 1D GS 45 2D - 61 3D = 77 4D M 93 5D ] 109 6D m 125 7D }
14 0E SO 30 1E RS 46 2E . 62 3E > 78 4E N 94 5E ^ 110 6E n 126 7E ~
15 0F SI 31 1F US 47 2F / 63 3F ? 79 4F O 95 5F _ 111 6F o 127 7F DEL
or to use an online website, but what hacker are you if you don’t bother to automate in a few hours what can be done manually in a few minutes? So, I decided to create my own tool.
Using the Go programming language, I developed dec2ascii, a simple and fast CLI tool for converting the decimal representation of characters to ASCII text. You can find the source code of dec2ascii on GitHub at the following URL: https://github.com/tragdate/dec2ascii-go.
package main
import (
"flag"
"fmt"
"os"
"strconv"
"strings"
)
func main() {
inputFile := flag.String("i", "", "input file")
outputFile := flag.String("o", "", "output file")
flag.Parse()
var decimalNumbers []string
if *inputFile != "" {
fileContent, err := os.ReadFile(*inputFile)
if err != nil {
fmt.Fprintf(os.Stderr, "error reading file: %v\n", err)
os.Exit(1)
}
decimalNumbers = strings.Fields(string(fileContent))
} else {
decimalNumbers = flag.Args()
}
var asciiText string
for _, decimalStr := range decimalNumbers {
decimal, err := strconv.Atoi(decimalStr)
if err != nil {
fmt.Fprintf(os.Stderr, "invalid decimal number: %s\n", decimalStr)
continue
}
asciiText += string(decimal)
}
if *outputFile != "" {
err := os.WriteFile(*outputFile, []byte(asciiText), 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "error writing file: %v\n", err)
os.Exit(1)
}
} else {
fmt.Println(asciiText)
}
}
What does dec2ascii do?
dec2ascii does exactly what its name suggests: it converts decimal numbers to ASCII characters. You can use this tool to convert decimal characters from text files or other sources to ASCII characters.
How to install dec2ascii?
To install dec2ascii, you need to follow these steps:
- Download the source code from GitHub.
- Make sure you have Go installed on your computer.
- Navigate to the directory where you downloaded the source code.
- Run
go build
to compile the source code. - Run
./dec2ascii --help
to see usage options.
Or the following steps for system-wide installation with makefile:
- Clone the repo
- Make sure you have Go installed on your computer.
- Navigate to the directory where you cloned the repo
- Run `sudo make install’ to install dec2ascii system-wide
- Run `dec2ascii –help’ to see usage options.
How to use dec2ascii?
To convert decimal numbers to ASCII characters using dec2ascii, just run the following command:
./dec2ascii
For example, to convert the decimal numbers 84 114 97 103 68 97 116 101
to ASCII characters, you can use the following command:
./dec2ascii 84 114 97 103 68 97 116 101
In this case, the result will be the following:
TragDate
Conclusion
In conclusion, I created dec2ascii to help people who are facing the same problem as me. I hope this tool will be useful for you and that it will make your work easier when you need to convert the decimal representation of characters to ASCII text. Feel free to contribute to its development and tell me your thoughts!