PE Datei in Hex ausgeben
-
Hallo, ich habe ein Programm geschrieben welches eine PE Datei ausliest und deren Inhalt in Hex auf dem Bildschirm ausgibt.
[Die Datei die ich auslese heisst hier console.exe die Datei ruft nur eine MessageBox und ExitProcess auf. console.exe habe in Assembler (masm32) geschrieben.]
Am Anfang stimmt die Ausgabe noch später nicht mehr.
Folgender Teil wird bei mir z.b. nicht mit ausgeben:
45 78 69 74 50 72 6f 63 65 73 73 <-- Steht für ExitProcessDer Teil steht laut PEiD im Import Table von console.exe aber wieso bekomme ich den mit meinem Programm nicht ausgegeben ?
Ist der Import Table in der PE Datei verschlüsselt oder so?
#include <iostream> #include <fstream> using namespace std; int main(int argc, char* argv[]) { int n=15; string save; unsigned char var; long file_size; ifstream datei ( "console.exe", ios :: binary); if ( !datei.good() ) { cout<<"Die Datei existiert nicht !"<<endl; system("PAUSE"); return 0; } datei.seekg ( 0 , ios::end ); file_size = datei.tellg(); datei.seekg ( 0 , ios::beg ); datei>>save; datei.close(); for(int i=0 ; i< file_size ; i++) { var = save[i]; cout << " " << hex << static_cast<int>(var); if ( i == n ) //zeilenumbruch { cout<< endl; n=n+15; } } system("PAUSE"); }Ausgabe von meinem Programm:
4d 5a 90 0 3 0 0 0 4 0 0 0 ff ff 0 0 b8 0 0 0 0 0 0 0 40 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 80 0 0 0 e 1f ba e 0 b4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 38 85 91 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0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0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 e8 89 91 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 60 8a 91 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0Drücken Sie eine beliebige Taste . . .
-
datei >> saveliest etwas bis zu dem ersten Zeichen das irgendwie als Whitespace gilt. Danach pfuscht du fröhlich in Speicher rum, in dem du nichts zu suchen hast, da du nicht die Größe von save benutzt, sondern deine eigene Größenmessung.
-
So habs hinbekommen.
Das datei>>save; war wohl nicht so schlau. ^^
#include <iostream> #include <fstream> #include <vector> using namespace std; int main(int argc, char* argv[]) { int n=15; int i=0; unsigned char *save="0"; unsigned char var; long file_size; ifstream datei ( "console.exe"); if ( !datei.good() ) { cout<<"Die Datei existiert nicht !"<<endl; system("PAUSE"); return 0; } datei.seekg ( 0 , ios::end ); file_size = datei.tellg(); datei.seekg ( 0 , ios::beg ); vector<unsigned char>line( file_size ); while ( !datei.eof() ) { datei >> line[i]; cout<<hex<< static_cast<int>(line[i]); i++; if ( i == n ) //zeilenumbruch { cout<< endl; n=n+15; } } datei.close(); cout<<endl<<endl; system("PAUSE"); }
-
Oh, das ist aber ganz schön umständlich. Und falsch. Guck dir mal die letzte Zeile an, die ausgegeben wird. Und die Anzahl der Whitespace-Zeichen. Und überhaupt die Gesamtzahl der Zeichen.
Das datei>>save; war wohl nicht so schlau. ^^
Warum machst du es dann immer noch? Das ändert doch nichts, ob du unsigned char oder string nimmst. Das Problem ist, dass Operator >> nicht für dein Vorhaben geeignet ist.
Ich schreibe dir gleich mal ein Beispiel wie es richtig geht.
-
Da:
#include <iostream> #include <vector> #include <fstream> #include <cctype> #include <iomanip> using namespace std; int main(int, char *argv[]) { cout << "SeppJs selbstausgebender Hexviewer!\n\nDie Executable hat folgenden Inhalt:\n\n"; vector<unsigned char> content; { ifstream in(argv[0], ios::in | ios::binary); in.seekg (0, ios::end); size_t file_size = in.tellg(); in.seekg (0, ios::beg); content.resize(file_size); in.read(reinterpret_cast<char*>(&content[0]), file_size); } const size_t line_length = 16; size_t base = 0; for(; base < content.size() - line_length; base += line_length) { for (size_t offset = 0; offset < line_length; ++offset) { size_t index = base + offset; cout << setfill('0') << setw(2) << hex << static_cast<unsigned int>(content[index]) << ' '; } cout << ' '; for (size_t offset = 0; offset < line_length; ++offset) { size_t index = base + offset; if (isgraph(content[index])) cout << content[index]; else cout << '.'; } cout << '\n'; } // Letzte Zeile braucht Sonderbehandlung: for (size_t offset = 0; offset < line_length; ++offset) { size_t index = base + offset; if (index < content.size()) cout << setfill('0') << setw(2) << hex << static_cast<unsigned int>(content[index]) << ' '; else cout << " "; } cout << ' '; for (size_t offset = 0; offset < line_length; ++offset) { size_t index = base + offset; if (index < content.size()) if (isgraph(content[index])) cout << content[index]; else cout << '.'; else cout << ' '; } cout << '\n'; }
-
Ty für das Beispiel deine Programm Ausgabe sieht bei weitem besser aus als meine.
Eine Frage habe ich aber noch, wenn man Daten aus einer Datei ausliest sollte man möglichst immer die read Funktion benutzen ?
Wann darf man >> benutzen um Daten aus Dateien zu lesen ?
-
Das kommt halt drauf an, was man möchte. Operator >> verwirft allen Whitespace den er findet. getline verwirft Trennzeichen. Und das ist auch gut so, denn das will man normalerweise. Wenn man aber Rohdaten lesen will, dann will man das eben nicht. Und dann muss man die Lesefunktionen benutzen, die das nicht machen (im Prinzip alle anderen). Ich habe jetzt read genommen, weil es erfahrungsgemäß das mit Abstand schnellste ist, um einen ganzen Block Daten am Stück zu lesen.
Guck mal in eine Referenz wie hier und schau dir die Unterschiede an.
http://www.cplusplus.com/reference/iostream/istream/