Overview of ftp_func.inc

Public Function Summary

Public functions are intended to be called by the code that imports this library.

Name Summary
ftp_authenticate
ftp_close
ftp_pasv
ftp_recv_data
ftp_recv_line
ftp_recv_listing
ftp_send_cmd
get_ftp_banner
get_ftp_port

Public Function Details

ftp_authenticate

Named Parameters

pass
socket
user

Code

function ftp_authenticate( socket, user, pass ) {

  local_var r;

  if( ! socket ) set_kb_item( name: "nvt_debug_empty/" + get_script_oid(), value:get_script_oid() + "#-#socket#-#ftp_authenticate" );
  if( ! user ) set_kb_item( name: "nvt_debug_empty/" + get_script_oid(), value:get_script_oid() + "#-#user#-#ftp_authenticate" );
  if( ! pass ) set_kb_item( name: "nvt_debug_empty/" + get_script_oid(), value:get_script_oid() + "#-#pass#-#ftp_authenticate" );
 
  r = ftp_recv_line( socket:socket ); # Receive the banner
  send( socket:socket, data:'USER ' + user + '\r\n' );
  r = ftp_recv_line( socket:soc );
  if( r[0] != '3' && 
      r[0] != '2' ) return 0;

  send( socket:socket, data:'PASS ' + pass + '\r\n' );
  r = ftp_recv_line( socket:socket );
  if( r[0] != '2' ) return 0;
  else return 1;
}

#

		
top

ftp_close

Named Parameters

socket

Code

function ftp_close( socket ) {

  if( ! socket ) set_kb_item( name: "nvt_debug_empty/" + get_script_oid(), value:get_script_oid() + "#-#socket#-#ftp_close" );

  send( socket:socket, data:'QUIT\r\n' );
  close( socket );
}

function get_ftp_banner( port ) {

		
top

ftp_pasv

Named Parameters

socket

Code

function ftp_pasv( socket ) {

  local_var r, port, array;

  if( ! socket ) set_kb_item( name: "nvt_debug_empty/" + get_script_oid(), value:get_script_oid() + "#-#socket#-#ftp_pasv" );
 
  send( socket:socket, data:'PASV\r\n' );
  r = ftp_recv_line( socket:soc );
  if( r[0] != '2' ) return 0;
  port = egrep( pattern:"^227 .* \([0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+\)",
	        string:r );
  if( ! port ) return 0;
  array = eregmatch( pattern:"^227 .* \([0-9]+,[0-9]+,[0-9]+,[0-9]+,([0-9]+),([0-9]+)\)", string:port );
  if( isnull( array ) ) return 0;
  port = int( array[1] ) * 256 + int( array[2] );
  return port;
}

function get_ftp_port( default ) {

		
top

ftp_recv_data

Named Parameters

line
socket

Code

function ftp_recv_data( socket, line ) {

  local_var buf, bytes, min;

  if( ! socket ) set_kb_item( name: "nvt_debug_empty/" + get_script_oid(), value:get_script_oid() + "#-#socket#-#ftp_recv_data" );

  if( line != NULL ) {
    bytes = ereg_replace( pattern:"^150.*\(([0-9]*) .*\)", string:line, replace:"\1" );
  }
 
  if( ! bytes ) { bytes = 8192; min = 1; }
  else { min = int( bytes ); bytes = int( bytes) ; }
 
  return recv( socket:socket, min:bytes, length:bytes );
}

#

		
top

ftp_recv_line

Named Parameters

retry
socket

Code

function ftp_recv_line( socket, retry ) {

  local_var n, r, res, t1, t2;

  if( ! socket ) set_kb_item( name: "nvt_debug_empty/" + get_script_oid(), value:get_script_oid() + "#-#socket#-#ftp_recv_line" );

  r = recv_line( socket:socket, length:65535 );
  n = 0;
  if( log_verbosity > 1 ) t1 = unixtime();
  while( strlen( r ) == 0 && n ++ < retry )
    r = recv_line( socket:socket, length:65535 );
    if (log_verbosity > 0) {
      t2 = unixtime();
      if( n > 0 && t2 - t1 > 1 ) log_print( 'ftp_recv_line: retried ', n, ' time( s ) = ', t2-t1, 's. Increase read timeout!\n' );
    }
    if( strlen( r ) < 4 ) 
      return( r );

  n = 0;
  res = r;
  # Some FTP servers have a broken banner on several lines with a leading space
  while( ( strlen( r ) > 3 && r[3] == "-" ) || ( strlen( r ) >= 3 && r[0] == ' ' ) ) {
    n ++;
    r = recv_line( socket:socket, length:65535 );
    if( n > 255 )
      return( NULL );
    res = res + r;
  }
  return( res );
}

function ftp_recv_listing( socket ) {

		
top

ftp_recv_listing

Named Parameters

socket

Code

function ftp_recv_listing( socket ) {

  local_var n, r, buf;

  if( ! socket ) set_kb_item( name: "nvt_debug_empty/" + get_script_oid(), value:get_script_oid() + "#-#socket#-#ftp_recv_listing" );
 
  n = 0;
  r = recv_line( socket:socket, length:65535 );
  buf = r;
  while( strlen( r ) ) {
    n ++;
    if( n > 4096 )
      return( NULL );
    r = recv_line( socket:socket, length:65535 );
    buf += r; 
  }
  return buf;
}

function ftp_recv_data( socket, line ) {

		
top

ftp_send_cmd

Named Parameters

cmd
socket

Code

function ftp_send_cmd( socket, cmd ) {

 if( ! socket ) set_kb_item( name: "nvt_debug_empty/" + get_script_oid(), value:get_script_oid() + "#-#socket#-#ftp_send_cmd" );
 if( ! cmd ) set_kb_item( name: "nvt_debug_empty/" + get_script_oid(), value:get_script_oid() + "#-#cmd#-#ftp_send_cmd" );

 send( socket:socket, data:cmd + '\r\n' );
 return ftp_recv_line( socket:socket );
}

function ftp_recv_line( socket, retry ) {

		
top

get_ftp_banner

Named Parameters

port

Code

function get_ftp_banner( port ) {

  local_var lines, sb, banner, soc, str;

  if( ! port ) set_kb_item( name: "nvt_debug_empty/" + get_script_oid(), value:get_script_oid() + "#-#port#-#get_ftp_banner" );

  lines = 0;
  sb = "ftp/banner/" + port;
  banner = get_kb_item( sb );
  if( banner ) return( banner );
  if( get_kb_item( "ftp/" + port + "/broken" ) ) exit( 0 );

  if( ! get_port_state( port ) ) return (0);
  soc = open_sock_tcp( port );
  if( ! soc ) {
    set_kb_item( name:"ftp/" + port + "/broken", value:TRUE );
    return NULL;
  }
  banner = ftp_recv_line( socket:soc );
  ftp_close( socket:soc );
  if( ! banner ) {
    set_kb_item( name:"ftp/" + port + "/broken", value:TRUE );
    return NULL;
  }
  replace_kb_item( name:sb, value:banner );
  return( banner );
}

function ftp_send_cmd( socket, cmd ) {

		
top

get_ftp_port

Named Parameters

default

Code

function get_ftp_port( default ) {

  local_var default, port, p;

  if( ! default ) set_kb_item( name: "nvt_debug_empty/" + get_script_oid(), value:get_script_oid() + "#-#default#-#get_ftp_port" );

  port = get_kb_item( "Services/ftp" );
  if( port ) default = port;

  p = get_kb_item( "ftp/" + default + "/broken" );
  if( p ) exit( 0 );

  if( ! get_port_state( default ) ) exit( 0 );

  return default;
}

		
top