当前位置: 技术问答>linux和unix
ftp client若若一问!(在线给分)
来源: 互联网 发布时间:2015-01-05
本文导语: 以下是一个简单的试验。通过socket 连接ftp server的21端口。然后USER anonymous , PASS adfo@eea.net试图登陆服务器(支持匿名登陆,用telent server 21,然后送出USER anonymous , PASS adfo@eea.net。可以得到希望的结果) //////////...
以下是一个简单的试验。通过socket 连接ftp server的21端口。然后USER anonymous , PASS adfo@eea.net试图登陆服务器(支持匿名登陆,用telent server 21,然后送出USER anonymous , PASS adfo@eea.net。可以得到希望的结果)
////////////////////////////////////
////
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 21
//struct ioctl_data_t d;
int main(int argc, char *argv[])
{
int socketfd , numreceive;
struct hostent* server;
struct sockaddr_in server_addr;
char buffer[5000];
char hostname[] = "real.nwu.edu.cn";
if ((server = gethostbyname(hostname)) == NULL){ //DNS
printf("Get Host errorn");
exit(-1);
}
if ((socketfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){ //initlization socket
printf("socket initliaztion errorn");
exit(-1);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr = *((struct in_addr*)server->h_addr);
bzero(&(server_addr.sin_zero) , 8);
if (connect(socketfd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1){ //connect;
printf("connect errorn");
exit(-1);
}
int i = 1;
while(i!=2){
if ((numreceive = recv(socketfd, buffer, sizeof(buffer), 0)) == -1){ //receive message from a socket
printf("receive message errorn");
exit(-1);
}
i++;
}
int buffer_end = sizeof(buffer) + 1;
buffer[buffer_end] = '';
printf("%s" , buffer);
char send_msg[] = "USER anonymousn"; //send anonymous quest and receive server message;
if ((numreceive = send(socketfd, send_msg, sizeof(send_msg), 0)) == -1){
printf ("send message errorn");
exit(-1);
}
if ((numreceive = recv(socketfd, buffer, sizeof(buffer), 0)) == -1){ //receive message from a socket
printf("receive message errorn");
exit(-1);
}
buffer[numreceive + 1] = '';
printf("%sn", buffer);
char send_msg2[] = "PASS adfoa@245.netn"; //send password and receive message from server
if ((numreceive = send(socketfd, send_msg2, sizeof(send_msg2), 0)) == -1){
printf ("send message errorn");
exit(-1);
}
if ((numreceive = recv(socketfd, buffer, sizeof(buffer), 0)) == -1){ //receive message from a socket
printf("receive message errorn");
exit(-1);
}
buffer[numreceive + 1] = '';
printf("%sn" , buffer);
close(socketfd);
return 0;
}
//////////////////////////
运行后的提示如下:(faint.........)
///
220 real FTP server (Version wu-2.6.1(1) Wed Aug 9 05:54:50 EDT 2000) ready.
331 Guest login ok, send your complete e-mail address as password.
)
530 Please login with USER and PASS.
问题好像出在PASS *******那块。在SEND passwd时。是否需要加密(md5?)。可是我看了一些ftp client 的源码好像都不用。而且就算要加密。server也应当send一个
encrypt key 过来。
难道要用ioctl来转换么?没用过这个函数。有谁能说说。
////////////////////////////////////
////
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 21
//struct ioctl_data_t d;
int main(int argc, char *argv[])
{
int socketfd , numreceive;
struct hostent* server;
struct sockaddr_in server_addr;
char buffer[5000];
char hostname[] = "real.nwu.edu.cn";
if ((server = gethostbyname(hostname)) == NULL){ //DNS
printf("Get Host errorn");
exit(-1);
}
if ((socketfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){ //initlization socket
printf("socket initliaztion errorn");
exit(-1);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr = *((struct in_addr*)server->h_addr);
bzero(&(server_addr.sin_zero) , 8);
if (connect(socketfd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1){ //connect;
printf("connect errorn");
exit(-1);
}
int i = 1;
while(i!=2){
if ((numreceive = recv(socketfd, buffer, sizeof(buffer), 0)) == -1){ //receive message from a socket
printf("receive message errorn");
exit(-1);
}
i++;
}
int buffer_end = sizeof(buffer) + 1;
buffer[buffer_end] = '';
printf("%s" , buffer);
char send_msg[] = "USER anonymousn"; //send anonymous quest and receive server message;
if ((numreceive = send(socketfd, send_msg, sizeof(send_msg), 0)) == -1){
printf ("send message errorn");
exit(-1);
}
if ((numreceive = recv(socketfd, buffer, sizeof(buffer), 0)) == -1){ //receive message from a socket
printf("receive message errorn");
exit(-1);
}
buffer[numreceive + 1] = '';
printf("%sn", buffer);
char send_msg2[] = "PASS adfoa@245.netn"; //send password and receive message from server
if ((numreceive = send(socketfd, send_msg2, sizeof(send_msg2), 0)) == -1){
printf ("send message errorn");
exit(-1);
}
if ((numreceive = recv(socketfd, buffer, sizeof(buffer), 0)) == -1){ //receive message from a socket
printf("receive message errorn");
exit(-1);
}
buffer[numreceive + 1] = '';
printf("%sn" , buffer);
close(socketfd);
return 0;
}
//////////////////////////
运行后的提示如下:(faint.........)
///
220 real FTP server (Version wu-2.6.1(1) Wed Aug 9 05:54:50 EDT 2000) ready.
331 Guest login ok, send your complete e-mail address as password.
)
530 Please login with USER and PASS.
问题好像出在PASS *******那块。在SEND passwd时。是否需要加密(md5?)。可是我看了一些ftp client 的源码好像都不用。而且就算要加密。server也应当send一个
encrypt key 过来。
难道要用ioctl来转换么?没用过这个函数。有谁能说说。
|
没写过,提些建议。
你可以用crypt后的密文:
crypt(passwd, salt);
passwd是加密前的明文,salt是两个字符的字符串,你可以给任意字符。
比如:
char salt[] = "ab";
你也可以直接试一下shadow文件中相应用户加密后的密码
你可以用crypt后的密文:
crypt(passwd, salt);
passwd是加密前的明文,salt是两个字符的字符串,你可以给任意字符。
比如:
char salt[] = "ab";
你也可以直接试一下shadow文件中相应用户加密后的密码