Solved

How is a program sended through TCP ?

Posted on 2013-05-30
6
445 Views
Last Modified: 2013-11-13
I know the question is a typical one but it is not ! In fact this is more exactly :

I found that when I transfer a picture thru TCP/IP, I first read it byte with byte and I send then those bytes. I outputed in a MsgBox all the bytes of that small picture and the result looked like this :
0x7EGG45UIGU84HG8UEGEUG5GH479GU8EG  ......... 

Open in new window

Starting with the '0' folowed by an 'x' then a bunch of numers and letters mixed together.
I'm pretty new coder so thats why Im asking and thats why I don't understand some things :
=> After this, I manually extracted this text and copy pasted it in the "FileWrite" function -> to write that string in a new created file to build the picture and clone it . And it worked ! The picture I wanted to clone( to transfer ) was exactly the same as the one built ,just copy pasting the 'byte string' of that picture !

Pretty easy and simple and logic ! I read the picture, outputed the bytes that are composing that picture, took that bunch of letters and numbers with that 0x in front and I wrote exactly that 'text', in a new created file, via ofcourse the programming functions.

BUT when I do exactly the same with a program ( executable ), the file is same size, same icon, BUT is not working when I start it ! It says that is not a valid Win32 program.

? So my question is : how can I do the same with an executable file ? Why when I fileread it and then fileWrite it works, BUT if I show what it had been read, copy that in the 'what to be wrote" parameter of the FileWrite function, don't work just like with the pictures ?!
0
Comment
Question by:AlexMert
  • 3
  • 3
6 Comments
 
LVL 25

Expert Comment

by:clockwatcher
Comment Utility
You haven't given us enough to go on to really answer the question.  We don't know what language and tools you're using to read your input and write your output.   But based on the fact that you mentioned FileWrite.  I'm guessing that might mean that you're using java's FileWriter.  FileWriter is intended to write character streams not binary streams.  It does character encoding.  If you really are using java, you'll want to look at using a FileOutputStream.  The same can be said for the input side.  You'll want to use an InputStream that doesn't do any character decoding and is compatible with a binary data stream.

If I guessed wrong on your language choice, the problem is still likely the same.  Whatever you're using to do either your reading or writing is probably built to work with character streams and is doing encoding in the background.  You should be using functions/objects/methods that are designed to work with binary data.
0
 

Author Comment

by:AlexMert
Comment Utility
Oh ok sorry my fault . Right now Im not coding in any major language like java or C , I code in AutoIt 3 ( a script-based language but very very powerful ). But nevermind, I will post this on their forum since they know exactly about this language problems. Thanks anyway !
0
 
LVL 25

Expert Comment

by:clockwatcher
Comment Utility
Glanced at the AutoIt docs and you need to make sure you're opening your file for writing in binary mode.

http://www.autoitscript.com/autoit3/docs/functions/FileOpen.htm

FileOpen ( "myoutputfile.exe", 16 )
0
Want to promote your upcoming event?

Are you going to an event? Are you going to be exhibiting at a tradeshow? Talking at a conference? Using a promotional banner in your email signature ensures that your organization’s most important contacts stay in the know and can potentially spread the word about the event.

 

Author Comment

by:AlexMert
Comment Utility
Yes I know, thats what Im using, as I said it works perfect with pictures, but let me clarify you perfectly, and Im pretty sure is an interesting subject too :

The FileWrite function in au3, is doing exactly what is saying : writing BYTES in an empty file ! Those BYTES I provide them in it's second parameter so looks like this :
FileWrite( "C:\calc.exe" , $bytes )

Open in new window

where the $bytes is the variable from the second parameter that contains the bytes that I need to copy in the new created file, to clone the previous one. Everything clear till now ? Ok then how do I obtain those bytes ? I read the file I want to clone byte by byte and save all of them in a variable that I will send it to that second parameter of the FileWrite function. So I copy the file from one location to another, byte by byte :). Clear till now ? ok, but what I REALLY WANT IS :

- to transform those bytes into a string or text that I could copy and send it via Messenger to a friend that will take this text and transform it back into exactly the same bytes tyhat I had and apply them too to the FileWrite function, exactly like me ! As I said , this works with picture type files, but not with executable files !
0
 
LVL 25

Accepted Solution

by:
clockwatcher earned 275 total points
Comment Utility
Character/text data carries with it an encoding.  Translating a binary data stream into a text string involves encoding it.  It's actually why you're having problems with this in the first place.  You can't just send over a binary data stream to something that is expecting text.  You'll get garbage because behind the scenes the thing that's receiving it is decoding it into something that it understands.  In the case of your image, you're not getting enough garbage for it to make a difference.  It's intact enough to still be an image.  If you ran an MD5 on it though, you'd probably find that it didn't match-- that a few bytes had been messed with.  A graphic file is loose enough that in many cases it won't matter.  An executable isn't.

Typically, you would base64 encode a binary data stream, if you had to count on the thing that's receiving it to handle it as text.  Base64 will encode your binary data into an ascii encoding, so an input stream that expects text will be able to safely handle it and not do any unsafe character translation on it when it comes in.   You'd then base64 decode your ascii string back into a raw binary data stream.
0
 

Author Closing Comment

by:AlexMert
Comment Utility
Excellent man ! Now you clarified me out ! Awesome resonse :) Thanks alot!
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This story has been written with permission from the scammed victim, a valued client of mine – identity protected by request.
I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
Notifications on Experts Exchange help you keep track of your activity and updates in one place. Watch this video to learn how to use them on the site to quickly access the content that matters to you.
Articles on a wide range of technology and professional topics are available on Experts Exchange. These resources are written by members, for members, and can be written about any topic you feel passionate about. Learn how to best write an article t…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now