|
|
Hi,
This post was really very helpful..
Can you please share your code to download/upload files as well..
Thanks in advance,
Gaurav
|
|
|
|
|
I am trying to delete some files and I get "bad message". I changed my command to whoami and I get the same response. I can sucessfully put files out there using the SshTransferProtocolBase, but I can't delete them using the shell. I'm a unix newbee, so it's probably something stupid, but I can't seem to figure out what.
Here's my code:
SshShell shell = new SshShell(ftpHostName, ftpUser, ftpPassword);
string shellPrompt = string.Empty;
try
{
shell.Connect();
if (shell.ShellConnected && shell.ShellOpened)
{
shell.WriteLine("whoami");
Console.WriteLine(shell.Expect(shellPrompt));
}
}
catch (Exception ex)
{
logger.Error("Delete .dat.lock file error -- details are: " + ex.Message);
}
finally
{
shell.Close();
}
|
|
|
|
|
I Have the same problem!
Any suggestions?
|
|
|
|
|
Hi, I use this code to do a simple file transfer and have a problem:
Scp scp = new Scp("IP", @"user", "pwd");
scp.Connect(22);
-- everything is ok until here
scp.From(@"/public/1.txt", @"c:\sftp_test\");
but this causes Scp.cs->SCP_CheckAck(Stream ins) function to fail with "Specified file path is invalid" error. We are using Tumbleweed as a server and do see "/public/1.txt" structure from web ui.
I do understand that it cannot locate file, but is there any way to find out the path to be provided (like load directory structure or something like this) ? Thanks.
|
|
|
|
|
Hi, I was wondering if SharpSSH can connect to unix with sudo access? I'm using SshExec(host, username, password) and sending a grep command to retrieve the data I need. Any help would be much appreciated.
|
|
|
|
|
Hello again Tamir,
First of all, Thank you very much for sharing this project with us and respond all our questions, you are very kind person.
In this Occasion, I would want to ask you about a rare behavior that I noted in the class SshStream
When I send the first command to AIX-Unix server and I try to read the response, nothing appear
then, when I fire the second command to the server and I read the response
I get the response of the first command that I sent before, but not the second.
When I send the third, I get the answer of the second, and so on...
What is happening here ?
I'm using SharpSSH-1.1.1.13.src from tamirgal.com And Visual C#2005.
I think, it is a cleaning buffer problem, but I'm not totally sure.
I see the buffer is created every time I invoke the method
ReadResponse(), so I don't know where the problem is ...
public string ReadResponse()
{
int readCount;
StringBuilder resp = new StringBuilder();
byte[] buff = new byte[1024];
Match match;
do
{
readCount = this.Read(buff);
resp.Append(
System.Text.Encoding.Default.GetString(
buff), 0, readCount);
string s = resp.ToString();
match = m_prompt.Match( s );
}while(!match.Success);
return HandleTerminalChars( resp.ToString() );
}
------------------------------------------------------------------
My Code Is very simple: just like this
SshStream sshst;
private void button1_Click(object sender, EventArgs e)
{
sshst = new SshStream(_IP, _USER, _PASS);
sshst.Prompt = "$";
sshst.RemoveTerminalEmulationCharacters = true;
MessageBox.Show("connected");
}
private void button2_Click(object sender, EventArgs e)
{
sshst.Write("pwd");
sshst.Flush();
this.txtresult.Text = sshst.ReadResponse();
}
private void button3_Click(object sender, EventArgs e)
{
sshst.Write("cd ..");
sshst.Flush();
this.txtresult.Text = sshst.ReadResponse();
}
private void button4_Click(object sender, EventArgs e)
{
sshst.Close();
MessageBox.Show("Closed");
}
Thank you!
Best Regards !!!
Javier Nicolas(Argentina)
|
|
|
|
|
Hello, all!
I have a strange issue with SharpSSH 1.1.1.13
I have no issues using SFTP session connect() method from my WinXP machine with a linux host (little-endian X86 PC), but when connecting to another linux server (unknown processor type and endianness), I get an exception in verify() method, line 84, SignatureDSA.cs. The error message is "Bad Data \r\n" and the stack trace is:
at System.Security.Cryptography.DSACryptoServiceProvider._ImportKey(IntPtr hCSP, Int32 algid, DSACspObject data)\r\n at System.Security.Cryptography.DSACryptoServiceProvider.ImportParameters(DSAParameters parameters)\r\n at Tamir.SharpSsh.jsch.jce.SignatureDSA.verify(Byte[] sig) in D:\\installs\\SharpSSH-1.1.1.13.src\\SharpSSH-1.1.1.13.src\\SharpSSH\\jsch\\jce\\SignatureDSA.cs:line 84\r\n at Tamir.SharpSsh.jsch.DHG1.next(Buffer _buf) in D:\\installs\\SharpSSH-1.1.1.13.src\\SharpSSH-1.1.1.13.src\\SharpSSH\\jsch\\DHG1.cs:line 293\r\n at Tamir.SharpSsh.jsch.Session.connect(Int32 connectTimeout) in D:\\installs\\SharpSSH-1.1.1.13.src\\SharpSSH-1.1.1.13.src\\SharpSSH\\jsch\\Session.cs:line 279
I don't know anything about the server that's causing the issue, since I don't have control over that machine. I was thinking that the issue may be that the other side is big-endian, and the DSA signature being verified is coming in in big-endian byte order. Of course, this is a guess, and I might be totally off here.
Does anyone have any suggestions on how to resolve the issue or what additional info is needed in order to help figure out what's going on?
Thank you in advance.
Regards,
Mark.
|
|
|
|
|
Found out some more info about the server:
Session protocol = SSH-2
SSH implementation = 3.2.9.1 SSH Secure Shell (non-commercial)
Encryption algorithm = aes
Compression = No
File transfer protocol = SFTP-3
Server host key fingerprint
ssh-dss 1024
I now think the issue may be that ssh-dss is used, and if I understand correctly, there is only partial support for it in SharpSSH. I was wondering if someone can comment on this in more detail. Any help would be appreciated.
Regards,
Mark.
|
|
|
|
|
I have the same problem that DSA can not be verified. Temporarily I manually set the result as true and then everything works fine now but the program lost the ability to verify DSA.
|
|
|
|
|
|
When using SharpSSH 1.1.1.13 and VMWare ESX (which uses an older version of OpenSSH v3.6) together, upon sFtp upload or download, if you try to untar a compressed file, you get this ...
gzip: stdin: invalid compressed data--format violated
tar: Unexpected EOF in archive
tar: Unexpected EOF in archive
tar: Error is not recoverable: exiting now
The bytes in the original and the one with the problem match exactly, however the md5sum does not. I did some testing transferring text files. In each case, I uploaded the text file with WinSCP, and the same text file with SharpSSH. Also I only transfered in binary mode. First I started with a null byte. Both the WinSCP version and SharpSSH version uploaded matched as far as the bytes and the md5. Then I moved on to a text file with all 255 characters in it, which also matched. Then moved on to a text file with 500 mb worth of each 255 characters in a line over and over again, which matched also. I uploaded a 1 mb zip file from windows, and it matched with the one uploaded by winscp. Then tried a 50 mb file, and it was corrupt. The zip test told me it had crc errors. I did similar tests with gzip, bzip, and rar, all with errors and corrupt files. Uncompressed tars as well did not transfer properly. In each case, the file size matched but the md5 checksum did not.
modified on Friday, September 12, 2008 12:03 AM
|
|
|
|
|
In Session.cs, in disconnect() line 1529, there is a possibility, in certain error conditions that connectThread will be null, like in premature host port closures, premature to what the library expected.
So all I did was add
<br />
if(connectThread !=null) <br />
before
<br />
lock (connectThread)<br />
{<br />
...<br />
This will prevent the inner exception of the true error from being swallowed up.
Rob Achmann
maker of .Net thingys
|
|
|
|
|
hello Tamir,
is there any kind of licence restriction to use your components in my projects ?
Regards!
Javier Nicolas(Argentina)
|
|
|
|
|
|
thank you Tamir
Javier Nicolas(Argentina)
|
|
|
|
|
I connect via WiMax. I've had some connectivity issues with SharpSSH becuase of this.
I noticed that even though you establish a conection, the underlying objects are not ready yet.
The call to socket.getInputStream() would throw an internal NullReference Exception, even though the socket object is valid.
The following code accounts for this.
if (proxy == null)
{
Stream In = null;
Stream Out = null;
if (socket_factory == null)
{
socket = Util.createSocket(host, port, connectTimeout);
// r_achmann 17AUG2008 : On a bad, or unstable connection (like WiMax)
// (or perhaps it's the quality of the router?)
// the GetInputStream call can initially fail
System.DateTime endDT = System.DateTime.Now.AddSeconds(2);
while ((In == null || Out == null) && System.DateTime.Now <= endDT)
{
if (In == null)
{
try { In = socket.getInputStream(); }
catch { }
}
if (Out == null)
{
try { Out = socket.getInputStream(); }
catch { }
}
}
}
else
{
socket = socket_factory.createSocket(host, port);
In = socket_factory.getInputStream(socket);
Out = socket_factory.getOutputStream(socket);
}
Rob Achmann
maker of .Net thingys
|
|
|
|
|
Depending on something to happen over a number of iterations makes for a shakey foundation of solid architecture.
Lets do something better.
This code can be improved by making the 2 seconds configurable.
C:\Projects\SharpSSH-1.1.1.13.src\SharpSSH\jsch\Channel.cs
public virtual void connect()
{
if(!session.isConnected())
{
throw new JSchException("session is down");
}
try
{
Buffer buf=new Buffer(100);
Packet packet=new Packet(buf);
// send
// byte SSH_MSG_CHANNEL_OPEN(90)
// string channel type //
// uint32 sender channel // 0 // uint32 initial window size // 0x100000(65536)
// uint32 maxmum packet size // 0x4000(16384)
packet.reset();
buf.putByte((byte)90);
buf.putString(this.type);
buf.putInt(this.id);
buf.putInt(this.lwsize);
buf.putInt(this.lmpsize);
session.write(packet);
// r_achmann 17AUG2008 : iterative dependancies are shakey.
// int retry=1000;
System.DateTime endDT = System.DateTime.Now.AddSeconds(2);
while(this.getRecipient()==-1 &&
session.isConnected() &&
System.DateTime.Now <= endDT ) // retry > 0)
{
// r_achmann 17AUG2008 why try/catch on this?? So expensive
Thread.sleep(50);
//try{}
//catch(Exception ee){}
//retry--;
}
Rob Achmann
maker of .Net thingys
|
|
|
|
|
Call me persnikity, I like to keep track of what the captured exception _really_ was, when throwing a new one...
For example in C:\Projects\SharpSSH-1.1.1.13.src\SharpSSH\jsch\PortWatcher.cs, in function getPort.
The catch for getByName can now store the real reason of the exception with
catch(Exception uhe)
{
throw new JSchException("PortForwardingL: invalid address "+address+" specified.", uhe);
}
Now, your exception handlers can report the true issues to support personnel.
To achieve this, just add a couple of constructors to
C:\Projects\SharpSSH-1.1.1.13.src\SharpSSH\java\Exception.cs
(Note that to access, System.Exception, you must use the namespace 'redirection' up in the 'using' reference
public Exception(Ex ex) : base( string.Empty, ex)
{
}
and
public Exception(string msg, Ex ex) : base(msg, ex)
{
}
Then, C:\Projects\SharpSSH-1.1.1.13.src\SharpSSH\jsch\JSchException.cs
add
public JSchException(string msg, Exception ex) : base(msg, ex)
{
}
It's just more refined.
R.
Rob Achmann
maker of .Net thingys
modified on Saturday, August 23, 2008 10:47 PM
|
|
|
|
|
|
Anyone have a working AES256CBC.cs file yet?
Here is mine...can't seem to get it to work.
What happens is that the s2ccipher blows up when it gets 'invalid data'... for user auth phase after key exchange. the j var ends up being some huge number above 60 million as it's place in the buffer is probably wrong. Thus the 'read' code sends an exception 'Invalid Data'
Any help appreciated!
////////////////////////////////////////////////////////////////
using System;
using System.Security.Cryptography;
namespace Tamir.SharpSsh.jsch.jce
{
/*r_achmann 17AUG2008 this version if added to support AES 256 */
/* -*-mode:java; c-basic-offset:2; -*- */
/*
Copyright (c) 2002,2003,2004 ymnk, JCraft,Inc. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
3. The names of the authors may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
public class AES256CBC : Cipher
{
private int mode;
private const int ivsize = 32; // RijndaelManaged insists on this
private const int bsize = 16;
private System.Security.Cryptography.RijndaelManaged rijndael;
private ICryptoTransform cipher;
public override int getIVSize() { return ivsize; }
public override int getBlockSize() { return bsize; }
public override void init(int mode, byte[] key, byte[] iv)
{
this.mode = mode;
rijndael = new RijndaelManaged();
rijndael.Mode = CipherMode.CBC;
rijndael.Padding = PaddingMode.Zeros;
rijndael.BlockSize = 256;
rijndael.KeySize = 256;
byte[] tmp;
if (iv.Length != ivsize)
{
tmp=new byte[ivsize];
if (iv.Length > ivsize)
Array.Copy(iv, 0, tmp, 0, tmp.Length);
else
Array.Copy(iv, 0, tmp, 0, iv.Length);
//tmp = rijndael.IV;
iv = tmp;
}
if (key.Length != bsize)
{
tmp=new byte[bsize];
if (key.Length > bsize)
Array.Copy(key, 0, tmp, 0, tmp.Length);
else
Array.Copy(key, 0, tmp, 0, key.Length);
// tmp = rijndael.Key;
key = tmp;
}
try
{
// SecretKeySpec keyspec=new SecretKeySpec(key, "AES");
// cipher=javax.crypto.Cipher.getInstance("AES/CBC/"+pad);
// cipher.init((mode==ENCRYPT_MODE?
// javax.crypto.Cipher.ENCRYPT_MODE:
// javax.crypto.Cipher.DECRYPT_MODE),
// keyspec, new IvParameterSpec(iv));
cipher = (mode == ENCRYPT_MODE ?
rijndael.CreateEncryptor(key, iv) :
rijndael.CreateDecryptor(key, iv));
}
catch (Exception e)
{
Console.WriteLine(e);
cipher = null;
}
}
public override void update(byte[] foo, int s1, int len, byte[] bar, int s2)
{
//cipher.update(foo, s1, len, bar, s2);
cipher.TransformBlock(foo, s1, len, bar, s2);
}
public override string ToString()
{
return "aes256-cbc";
}
}
}
////////////////////////////////////////////////////
RAchmann
maker of .Net thingys
|
|
|
|
|
Hi Rob,
I've got a solution to your AES256-CBC issue.. Looks like you set the IV to 32 and the Block size to 16 instead of the other way around.
Here's my working code tested against WinSSHD-5.02 and F-Secure 5.20 servers:
public class AES256CBC : Cipher
{
private int mode;
private const int ivsize = 16;
private const int bsize = 32;
private RijndaelManaged rijndael;
private ICryptoTransform cipher;
public override int getIVSize(){return ivsize;}
public override int getBlockSize(){return bsize;}
public override void init(int mode, byte[] key, byte[] iv)
{
this.mode=mode;
rijndael = new RijndaelManaged();
rijndael.Mode = CipherMode.CBC;
rijndael.Padding = PaddingMode.None;
rijndael.KeySize = 256;
byte[] tmp;
if(iv.Length>ivsize)
{
tmp=new byte[ivsize];
Array.Copy(iv, 0, tmp, 0, tmp.Length);
iv=tmp;
}
if(key.Length>bsize)
{
tmp=new byte[bsize];
Array.Copy(key, 0, tmp, 0, tmp.Length);
key=tmp;
}
try
{
cipher = (mode==ENCRYPT_MODE ? rijndael.CreateEncryptor(key, iv): rijndael.CreateDecryptor(key, iv));
}
catch(Exception e)
{
System.Diagnostics.Debug.WriteLine(e.ToString());
cipher=null;
}
}
public override void update(byte[] foo, int s1, int len, byte[] bar, int s2)
{
cipher.TransformBlock(foo, s1, len, bar, s2);
}
public override string ToString() {return "aes256-cbc";}
}
WinSSHD logs show success (F-Secure logs are a PITA):
000000000176 2009-01-02 09:14:38.437500 UTC WinSSHD 5.02 [255] Trace
Session thread 1022 handling connection from 172.17.1.106:10268:
Key exchange: diffie-hellman-group1-sha1. Session encryption: aes256-cbc, MAC: hmac-sha1, compression: none.
000000000177 2009-01-02 09:14:38.448647 UTC WinSSHD 5.02 [040] Info
Session thread 1022 for virtual account 'test' in context 'WinSSHD_VirtualUsers' from 172.17.1.106:10268:
Logon attempt 1 for user name 'test' with logon method 'password' succeeded.
Hope this helps.
|
|
|
|
|
Thanks - I'll give that a go.
Did you take my other changes I've posted? They really help on slow/suspect connections.
R Achmann
maker of .Net thingys
|
|
|
|
|
Hi Rob,
I did apply some of the changes you gave. I also did a painstaking merge of the Jsch-1.40 code into ChannelSftp and some of the support functions in Util.
To be honest.. My application only requires SFTP at the moment, so I did away with a lot of the other classes and completely did away with the java tree, converting everything to native C# functions. I had to do a lot of tracing to figure out why my sessions were hanging on disconnect. I ended up doing a lot of cross-checking and Monitor.Enter/Exit to prevent the main session and the run() reader from contending for the disconnect.
I've also discovered that HMACMD596 and HMACSHA196 are broken.
HMACMD596 can be fixed by using this for the doFinal() method:
public byte[] doFinal()
{
cs.Close();
byte[] result = mentalis_mac.Hash;
byte[] key = mentalis_mac.Key;
mentalis_mac.Clear();
init(key);
byte[] hMAC = new byte[bsize];
Array.Copy(result, 0, hMAC, 0, bsize);
result = hMAC;
return result;
}
I'm working on HMACSHA196 now.
pb
EDIT:..
Turns out the fix for HMACSHA196 is the same. What was throwing me was that the init() method was using MD5CryptoServiceProvider instead of SHA1CryptoServiceProvider and I didn't notice. Brilliant.
pb
modified on Saturday, January 3, 2009 5:14 AM
|
|
|
|
|
Do you have a link to that Jsch-1.40 version? Is that your c# version? Do you have a link to your c# version?
I'd be interested in looking at that. I may end up doing that myself.
R
GaltSalt
maker of .Net thingys
|
|
|
|
|