Tags: code emulation
Rating:
We had to emulate AT&T style asm. Just 2 available registers TRX and DRX. 3 operations.
- REVERSE \<register\>
- XOR \<register\> \<register or "string"\>
- MOV \<register\> \<register or "string"\>
# C++ Solution
-----
```
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <iostream>
#include <algorithm>
#include <numeric>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <queue>
#include <istream>
#include <sstream>
#include <iterator>
#include <fstream>
using namespace std;
void sxor(string& s1, string s2) {
for(int i = 0; i < max(s1.size(),s2.size()); i++) {
if(i >= s2.size()) {
s1[i] = (s1[i]);
} else if(i >= s1.size()) {
s1 += (s2[i]);
} else {
s1[i] = (s1[i] ^ s2[i]);
if((s1[i] ^ s2[i]) == '}') cout << "hoooray\n";
}
}
}
int main() {
char init[] = {'G','E','D',0x3,'h','G',0x15,'&','K','a', ' ', '=', ';', 0x0c, 0x1a, '3', '1', 'o', '*', '5', 'M'};
string TRX(init);
string DRX{};
ifstream a("Crypto.asm");
string text;
while(std::getline(a,text)) {
std::istringstream iss(text);
std::vector<std::string> results(std::istream_iterator<std::string>{iss},
std::istream_iterator<std::string>());
if(results[0] == "REVERSE") {
if(results[1] == "TRX") {
reverse(TRX.begin(),TRX.end());
}
else if (results[1] == "DRX") {
reverse(DRX.begin(), DRX.end());
} else {
cout << "ERROR\n";
}
}
else if(results[0] == "XOR") {
if(results[2][0] == '"' && results[2][results[2].size()-1] == '"') {
if(results[1] == "TRX") {
sxor(TRX,results[2].substr(1,results[2].size()-2));
} else if(results[1] == "DRX") {
sxor(DRX,results[2].substr(1,results[2].size()-2));
} else {
cout << "ERROR\n";
}
} else {
if(results[1] == "TRX" && results[2] == "TRX") {
sxor(TRX,TRX);
} else if(results[1] == "DRX" && results[2] == "DRX") {
sxor(DRX,DRX);
} else if(results[1] == "TRX" && results[2] == "DRX") {
for_each(TRX.begin(),TRX.end(), [](char a) { cout << std::hex << int(a) << ' '; });
sxor(TRX,DRX);
} else if(results[1] == "DRX" && results[2] == "TRX") {
sxor(DRX,TRX);
} else {
cout << "ERROR\n";
}
}
}
else if(results[0] == "MOV") {
if(results[2][0] == '"' && results[2][results[2].size()-1] == '"') {
if(results[1] == "TRX") {
TRX = results[2].substr(1,results[2].size()-2);
} else {
DRX = results[2].substr(1,results[2].size()-2);
}
} else {
if(results[1] == "TRX" && results[2] == "DRX") {
TRX = DRX;
} else if(results[1] == "DRX" && results[2] == "TRX") {
DRX = TRX;
} else {
cout << "ERROR\n";
}
}
}
else {
cout << "ERROR\n";
}
cout << "TRX: " << TRX << " DRX: " << DRX << '\n';
}
cout << '\n';
for_each(TRX.begin(),TRX.end(), [](char a) { cout << std::hex << int(a) << ' '; });
cout << TRX << '\n';
return 0;
}
```
( works only with g++, clang bugs lol )
> flag{N1ce_Emul8tor!1}