Corrupt .zip file after being downloaded by Netcat

0

I create a .zip file with WinRAR on my Windows, it can be opened and extracted on Linux as well as Windows if I transfer it via mail or any other common way.

But when I transfer it via Netcat, the zip file gets damaged.

On Linux, when I try to extract the netcatted file with 7z, I get errors like:

Open ERROR: Can not open the file as [zip] archive

WARNINGS:
Headers Error:
There are data after the end of archive

I move that same netcatted zip file back to windows and try to open it, I get error:

The archive is either in unknown format or damaged.

The zip file in question contains a folder, which contains files.

My linux machine is the listener, I used this netcat solution: HERE

I thought that may have been the problem, so I just used the basic solution:

  • Listener (Linux):

    nc -lp PORT > file.zip
    
  • Sender (Windows, PowerShell):

    Get-Content ZIP_FILE | ./Netcat32 IP PORT
    

Still wouldn't work.

Note that I had no problem transferring text files, just zip files.

Below is the comparison of the same 2 zip files, the left one is transferred via netcat (can't be opened by either Linux or Windows), the right one is via mail (can be opened by both). They were opened in gedit.

What am I doing wrong and how to make it work?

comparison

EDIT: According to Kamil Maciorowski I've tried to send the zip file through netcat as an stream of bytes, using this code in Powershell, Windows:

#Sender:
   $contents=Get-Content -Encoding Byte -ReadCount 0 ./file.zip
   echo $contents | ./NetCat32 IP PORT

Now on the linux machine I have a zip file that is 6 times the original size (which is no problem), and its contents are 1-3 digit numbers in each newline. (But shouldn't they all be in 1 line because I used the -ReadCount 0 tag? (see -ReadCount HERE)

I cannot open that file. Trying to extract it I get an error: "Is not an archive".

Typing the linux command file to see the file type of "file.zip" I get: "ASCII TEXT, with CRLF line terminators"

EDIT 2:

I booted another linux (as client), tried transfering the SAME problematic zip file that has been originally created on windows, and it worked without problems.

I booted back into windows and tried the following commands in cmd.exe:

type file.zip | Netcat32.exe IP PORT
Netcat32.exe IP PORT < file.zip

Both worked...

I tried the same on powershell. I even tried using get-content with all the available encoding types (unknown, utf8, unicode...). No success.

...

It seems that the problem is in powershell and its own cat command synonyms (gc, get-content, type, cat)..

OK, I have to run the command via cmd.exe, but I wish to run the cmd.exe command inside powershell if possible.

My current solution that works is to have a .bat file with the whole command, which is called inside of powershell... But I'd be glad to have a cleaner option.

EDIT 3: No need for a .bat file... I had problems with invoke-command, invoke-expression, start-job... but this command worked successfuly:

Start-Process cmd -ArgumentList " /c type file.zip | Netcat32.exe IP PORT"

Problem was in the way the Powershell Get-Content command works, so I had to use solutions in cmd.exe

I will wait 1-2 days for a powershell solution before I answer the question.

MyWays

Posted 2018-10-22T08:50:26.620

Reputation: 157

@KamilMaciorowski Didn't work.. Thanks though. Is there a simpler file transfer solution other than netcat? But one that's equally portable? – MyWays – 2018-10-22T10:31:50.273

netcat is as simple as it can be; it takes byte stream and passes it through a network (or the other way around). In this case it looks to me PowerShell is trying to be smart or something, instead of just passing binary data. But probably it's just different than tools I know; or maybe we don't know the right way. Can you use full-scale file sharing/transfer protocol like Samba or SFTP? – Kamil Maciorowski – 2018-10-22T10:38:40.683

Ncat is the modern version, included in the Nmap package. – harrymc – 2018-10-22T11:01:10.503

i've added more info – MyWays – 2018-10-22T17:19:22.347

get-content will parse to strings. it will mangle binary – Yorik – 2018-10-24T19:58:32.080

@Yorik Is there another way in Powershell then? – MyWays – 2018-10-24T20:46:10.730

A quick search also suggests that "byte" option for encoding is no longer valid for get- and set-content in Powershell v.6 and that "AsByteStream" should be used. You probably want to check out stackoverflow (random link: https://stackoverflow.com/questions/51155829/add-content-bytes-fails-with-powershell-core )

– Yorik – 2018-10-26T14:09:17.190

Answers

0

The problem was not in Netcat, but in the way PowerShell's command Get-Content works.

What was as easy as cat file.zip > newfile.zip in Linux creates a corrupt zip file on Windows with the PowerShell alternative Get-Content file.zip | Out-File newfile.zip

I have yet to figure out how to use it. It probably an encoding problem. (But I've tried all types of encoding with the -Encoding tag). And since no one answers, I'll close the question.

There's a workaround, using cmd.exe:

cmd /c type file.zip | Netcat32.exe IP PORT

MyWays

Posted 2018-10-22T08:50:26.620

Reputation: 157