faker.py
A Python based tool which for connections on a specified port. Upon connection, it logs the connection attempt, sleeps for a given time, then closes the connection.
Size 4.3 kB - File type text/x-pythonFile contents
#! /usr/bin/python
##### faker.py #####
# Copyright (c) 2005, George Bobeck
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * 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.
#
# * Neither the name of the LOYOLA UNIVERSITY CHICAGO nor the names of its contributors
# may be used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
# EXPRESS 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 THE COPYRIGHT OWNER OR CONTRIBUTORS 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.
#####
#
# To Use: python faker.py <port>
#
# OR chmod 0700 faker.py so that you can ./faker.py <port>
#
######
import os
import signal
import socket
import sys
import syslog
from time import *
##### Configurables #####
# Sleep Delay, in Seconds.
delay = 90
# Message to be sent to offender upon connection.
conn_msg = "Go Away, Lamer!"
# Selected Logging Types
# You can log to 'syslog', 'console', 'file', 'bell', 'none', or any combination of those five
log = ('console', 'file', 'bell')
# You can specify which directory a log file will be created in
filedir = '/root'
#####
arguments = sys.argv[1:]
PORT = int(arguments[0])
HOST = '' # Symbolic name meaning the local host
times = "Faker.py Started at " + strftime("%d %b %Y %H:%M:%S")
ports = {7:'echo', 9:'discard', 11:'systat', 13:'daytime', 19:'chargen', 20:'ftp-data', 21:'ftp', 22:'ssh', 23:'telnet', 25:'smtp', 37:'time', 42:'nameserver', 53:'DNS', 63:'whois', 67:'bootps', 68:'bootpc', 69:'tftp', 70:'gopher', 79:'finger', 80:'http', 88:'kerberos', 92:'npp', 109:'pop2', 110:'pop3', 113:'auth', 115:'sftp', 119:'nntp', 123:'ntp'}
# takes a list of strings and returns one large string
def stringer(lst):
st = ''
lp = 0
while lp < len(lst):
st = st + lst[lp]
lp = lp + 1
return st
def log_message(message):
# Sound system bell upon log activity
if ('bell' in log):
os.system('echo -n -e "\a"')
# LOG TO SYSLOG
if ('syslog' in log):
syslog.syslog(syslog.LOG_AUTH ,message)
# LOG TO FILE
if ('file' in log):
tst = PORT in ports
if tst:
fina = filedir + '/' + ports[PORT] + '-faked.log'
else:
fina = filedir + '/' + str(PORT) + '-faked.log'
try:
# Get data file.
input = open(fina , 'r')
backup_list = input.readlines()
out = stringer(backup_list) + message + '\n'
input = open(fina , 'w')
input.write(out)
input.close()
except:
# The data file doesn't exist, create it.
input = open(fina , 'w')
message2 = message + '\n'
input.write(message2)
input.close()
# LOG TO CONSOLE
if ('console' in log):
print message
if ('none' in log):
pass
def signal_handler(signal, frame):
timee = "Faker.py Stopped at " + strftime("%d %b %Y %H:%M:%S")
log_message(timee)
sys.exit(0)
log_message(times)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
signal.signal(signal.SIGINT, signal_handler)
while 1:
s.listen(5)
conn, addr = s.accept()
time1 = strftime("%d %b %Y %H:%M:%S")
msg = '%s - Connection from %s:%s to localhost:%s'% (time1, addr[0], addr[1],PORT)
log_message(msg)
# Try to send a message, then sleep a bit for fun.
try:
conn.send(conn_msg)
# if that fails, don't send the message and go on to sleep.
except:
pass
# sleep and then close the connection
sleep(delay)
conn.close()
Click here to get the file