Skip to content

Instantly share code, notes, and snippets.

@claus
Last active March 23, 2021 22:13
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save claus/25264a7a8555b87ae8783c6d7d480d52 to your computer and use it in GitHub Desktop.
Save claus/25264a7a8555b87ae8783c6d7d480d52 to your computer and use it in GitHub Desktop.

A while ago i needed a way to look up addresses from zip codes in Brazil for a weekend tinker project (Zip codes are called "CEP" here, "Código de Endereçamento Postal", or Postal Addressing Code). Turns out such a database is pretty hard to get hold of. Zip code data is created and maintained by the Brazilian Post (Correios), they have a copyright on it and are selling it for a hefty price tag. There are two versions for sale, "DNE Básico" for R$1.100 (complete dataset in text format, probably CSV), and "DNE Máster" for R$2.500. As far as i aware, the "Máster" version is just the same as the "Básico" version, but additionally contains the database in MS Access format. Okay, both are a bit above my budget.

Luckily there is another option, called "Guia Postal Brasileiro Eletrônico", or GPBe. This is an executable that you can run on your PC to manually query zip codes and addresses. And to be able to do that it must somehow contain the entire database in some form. It costs R$50, which is affordable (about US$15), so i bought a copy.

So what you get when you buy this GPBe is a Windows executable, something like this: GPBe_2018_1801.exe. Sad trombone. I'm on a Mac and too lazy to install a Windows VM.

Reverse Engineering time!

The executable is a self-extracting RAR, which can be unpacked with Unarchiver.app. Yay!

You'll get:

  • Instala.exe
  • SetupEnderecador.exe
  • setup.exe
  • Leia_me.txt

The database is located in setup.exe, which is an Inno Setup executable. You can unpack such an exe with the tool innoextract, available via Homebrew: brew install innoextract, then do innoextract setup.exe.

You'll find the database in app/Access/dbgpbe.mdb. This is a Microsoft Access database. You can export tables to CSV (or to other databases) with mdbtools, also available via Homebrew: brew install mdbtools, then get the list of tables with mdb-tables dbgpbe.mdb, and export each table to CSV with mdb-export dbgpbe.mdb <table-name>.

Done!

No, wait.

Look at those CEP tables:

CEP Table in dbgpbe.mdb

Yeah that's right, that looks like base64 encoded garbage. They encrypted the actual CEP in the database. I read somewhere that they use TwoFish encryption with a SHA-1 hashed key "gpbe", but somehow didn't succeed in my decryption attempts. Maybe they changed the encryption method, or i am just doing something wrong. In fact, i looked at some CEP values and found that incrementing CEPS in most cases lead to incrementing encoded CEP values, so i don't know if this is actually TwoFish. It might be something much simpler.

Anyhow, turns out we don't need that CEP column at all. All tables that have CEP columns also have LOG_KEY_DNE columns, which contains the same CEP, just encoded a bit differently. I assume to be able to search the database for parts of the CEP?

LOG_KEY_DNE Table in dbgpbe.mdb

http://blog.otavio.info/2008/06/banco-de-dados-dos-correios.html?showComment=1257370846711#c3054233794277156792

@sortapod
Copy link

There are freely downloadable CEP databases at http://cep.la Zipped tsv form. Collected by enthusiasts recently and years ago.

@renatosbc
Copy link

Olá Claus.

O campo Log_Key_DNE funciona assim:
Exemplo - UGBK09EPC4PE90LA
UG BK 09 EP C4 PE 90 LA
0 9 7 2 5 2 7 0

TABELA:
X8 1
Z0 2
1B 3
3D 4
4C 5
2A 6
09 7
Y7 8
5V 9
UG 0

CN 1
EP 2
GR 3
ID 4
JS 5
HQ 6
FO 7
DM 8
BK 9
AL 0

8X 1
0Z 2
B1 3
D3 4
C4 5
A2 6
90 7
7Y 8
V5 9
GU 0

NC 1
PE 2
RG 3
DI 4
SJ 5
QH 6
OF 7
MD 8
KB 9
LA 0

@renatosbc
Copy link

Você poderia subir o GPBe_2018_1801.exe aqui neste repositório?
Aparentemente (28/12/19) a ECT não vende mais o GPBe, e estou procurando justamente essa versão que você tem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment