Sponsored Content
Top Forums Shell Programming and Scripting sed used for updating firewall allow ftp from DHCP access Post 302171558 by manouche on Thursday 28th of February 2008 07:26:16 PM
Old 02-28-2008
sed used for updating firewall to accept

Here is my situation. You can make all kinds of comments about how I am doing it and why from the networking standpoint, but I am really only looking for comments on how to make it work. Of course, any other ideas for how to do this would be welcome.

I have an iptables firewall that allows only specific ips in to the ftp port. I have a user who needs ftp access but works from a variety of locations and thus, the ip is constantly changing.

So, I read about "port knocking" and my idea is an offshoot of that.

I created a web page that captures the users ip address at the time and writes it to a file. I have two other text files that include the other parts of the iptables string needed for the firewall.

Here is file1.txt:

/sbin/iptables -A INPUT -p tcp -s

Here is ip.txt: (actual ip address masked from forum)

my.ip.my.ip

Here is file 3.txt:

--destination-port 21 -j ACCEPT

So, the idea is that I capture the ip into ip.txt, then cat the three files like this and use sed to put them back on one line, separated by spaces.

cat 1.txt ip.txt 3.txt | sed -n -e ":a" -e "$ s/\n//gp;N;b a" > final.txt

So, now I have the file final.txt that looks like this:

/sbin/iptables -A INPUT -p tcp -s my.ip.my.ip --destination-port 21 -j ACCEPT

The next step is to modify the firewall. I could rename this file to updatefirewall.sh and use this file as a script to modify the firewall like this:

./updatefirewall.sh or something like that.

Or, I could use sed or some other utility to do a search and replace/substitute on the actual firewall script that I have in place. The comment for that particular entry has a distinguishing character at the end of it, so I could search for the string and do a replace with the new string. I came up with the following, which looks for the end of the comment (#) and a new line (\n), and then an ip address and replaces it with "showboat".

sed 'N;s/#\n/[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/showboat/'

That was just for a test. I actually need to replace it with the line:

/sbin/iptables -A INPUT -p tcp -s my.ip.my.ip --destination-port 21 -j ACCEPT

which would be read from the text file final.txt.

So, after all that, my question is, is there a way for sed to read in a string from a file, or would I really need to create a sed script with variables and such?
 

10 More Discussions You Might Find Interesting

1. Linux

Ftp to firewall

Hi, My Linux Mandrake 9.2 is dedicate to be a web server, ftpserver, dns, firewall and internet router. With firewall enabled, I can't transfer files from a local computer to this machine. I can still; however, connect to the ftp server but whenever I do ls, get, mget. my process gets frezze... (0 Replies)
Discussion started by: vtran4270
0 Replies

2. Shell Programming and Scripting

delete dhcp.conf entry using sed

I am trying to use sed to remove entries from my dhcpd.conf file. The form of the file is: host foo { option 1 option 2 } host bar { option 1 option 2 } I was trying to use a label like: sed -e :a -e "s/^host bar {*//g;/{/N;//ba" /etc/dhcpd.conf... (2 Replies)
Discussion started by: tizatron
2 Replies

3. Shell Programming and Scripting

Sed Help in Updating something only in one particular file.

Hi, The scenerio is that I want to replace a text in one particular line of a file. But when I am using the sed it's replacing all the occurences of that text. Like the file is: >cat test DNGGF10 :None :Test DNGGF11 :ABC :Test1 DNGGF12 :None :Test2 DNGGF13 :None :... (4 Replies)
Discussion started by: bisla.yogender
4 Replies

4. BSD

Firewall Build: Problem Updating, Patching

In building a firewall, I need to update and patch the operating system after the initial install. However, the compiler and sources should never be on a firewall PC. So what is the best way to do this ? This firewall PC has 2 identical drives; I was thinking of performing 2 installs, updating... (2 Replies)
Discussion started by: larsuuber
2 Replies

5. AIX

extract data from DB2 and FTP it outside ompany's firewall

The main function is to extract the data from DB2 tables and FTPed to outside the company's firewall directly. pls find the attachment and help me out..i have only one day to do this (1 Reply)
Discussion started by: priyanka3006
1 Replies

6. AIX

Extract data from DB2 tables and FTP it to outside company's firewall

Please help me in creating the script in AIX. requirement is; The new component's main function is to extract the data from DB2 tables and company's firewall directly. The component function needs to check the timestamp in the DB2 tables ((CREDAT and CRETIM) with the requested timestamp and... (1 Reply)
Discussion started by: priyanka3006
1 Replies

7. AIX

ftp access without shell access

Hi all, I'm using AIX v 5.3 I want to create system users to access through ftp or sftp and restrict those users into specific directory and don't traverse the whole file system just to be restricted within a directory and don't get shell access . i don't want to use any other third party... (7 Replies)
Discussion started by: h@foorsa.biz
7 Replies

8. Shell Programming and Scripting

updating a file with sed -help

I have a file of records all made up of single charactors that need to be updated using their row/column (the dashes represent spaces as the html formatting of this text box when it posts removes the "nonessential" white space between the charactors) file example A 1 c B 2 b C 3 a... (2 Replies)
Discussion started by: dboward
2 Replies

9. UNIX for Dummies Questions & Answers

iptables rule to block access from VM Browser to Firewall Login Page

(1 Reply)
Discussion started by: senrabdet
1 Replies

10. Shell Programming and Scripting

Updating variables using sed or awk

Hi, I have a file(testfile.txt) that contains list of variables as shown below. T $$FirstName=James $$LastName=Fox $$Dateofbirth=1980-02-04 ……and so on there are 50 different variables. I am writing a script(script1.sh) that will update the above three variable one by one with the values... (6 Replies)
Discussion started by: Saanvi1
6 Replies
LOCKOUT(1)							      lockout								LOCKOUT(1)

NAME
lockout - avoid slacking and impose productivity and discipline on yourself WARNING
This program is VERY DANGEROUS. If it fails, you may end up not knowing the root password to your own computer (in which case you need to boot into single-user mode). There are no known reports of this actually happening, but we don't know how stupid you are. Also, you should probably not run this on a multi-user system. SYNOPSIS
lockout lock HhMm | Hh | Mm lockout lock HH:MM lockout lock HH:MMam | HH:MMpm lockout lock HHam | HHpm lockout lock lockout unlock [force] lockout status DESCRIPTION
Lockout is a tool that imposes discipline on you so that you get some work done. For example, lockout can be used to install a firewall that does not let you browse the Web. Lockout changes the root password for a specified duration; this prevents you from secretly ripping down the firewall and then browsing the Web anyway. In case of an emergency, you can reboot your computer to undo the effects of lockout and to restore the original root password. Obviously, lockout lock and lockout unlock can only be run by root. lockout status can be run by any user. lockout without any parameters shows a brief help message. lockout lock takes one optional parameter. If no parameter is given, you are dropped in interactive mode and asked for the duration of the lock or the time at which the lock should be lifted. You can also supply this as a parameter on the command line. Lockout understands various time formats. You can specify a delay, e.g., 3h (3 hours), 1h30m (1 hour and 30 minutes), or 90m (1 hour and 30 minutes), or you can specify absolute time, e.g., 2pm, 2:30am, 15:30, etc. You will be asked to confirm the time at which lockout will unlock your system. If you type "yes", lockout executes /etc/lockout/lock.sh and changes the root password to something completely random. /etc/lock- out/lock.sh is a shell script that you write. It takes measures to make sure you stop slacking. For example, it could install a firewall that prevents outgoing connections to port 80. See the "EXAMPLES" section below. lockout unlock takes an optional force parameter. Without any parameters, lockout lock will check whether it is time to unlock the system and, if so, executes /etc/lockout/unlock.sh, which is a shell script that you write. It should undo the effects of /etc/lockout/lock.sh, executed when the system was locked. If you pass the force parameter to lockout unlock, lockout will forcibly unlock your system, whether it was really time for that or not. lockout unlock should be called every minute by cron. See "CONFIGURATION". lockout status will print out the time at which the system is going to be unlocked. CONFIGURATION
/etc/cron.d/lockout must contain the following two entries: */1 * * * * root /usr/bin/lockout unlock >/dev/null 2>&1 @reboot root /usr/bin/lockout unlock force >/dev/null 2>&1 The examples that follow assume you are using sudo(8) and you have a file, /etc/lockout/sudoers.normal which is the normal /etc/sudoers file, and /etc/lockout/sudoers.lock, which is the /etc/sudoers file when lockout locks your computer. This example also assumes you are using iptables(8). /var/lib/iptables/active should contain your default firewall rules, and /var/lib/iptables/work should contain the firewall rules that enforce discipline. See below for an example. /etc/lock/lock.sh imposes discipline. For example: #!/bin/sh /etc/init.d/iptables load work cp /etc/lockout/sudoers.lock /etc/sudoers /etc/init.d/sudo stop /etc/init.d/sudo start /etc/lock/unlock.sh undoes these effects. For example: #!/bin/sh /etc/init.d/iptables restart cp /etc/lockout/sudoers.normal /etc/sudoers /etc/init.d/sudo stop /etc/init.d/sudo start Your /var/lib/iptables/work may look something like this: *filter :INPUT ACCEPT [1047:99548] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [1104:120792] # allow incoming packets from localhost, ntp, # and existing connections -A INPUT -i lo -j ACCEPT -A INPUT -p udp -m udp --source-port ntp -m state --state ESTABLISHED -j ACCEPT -A INPUT -m state --state ESTABLISHED -j ACCEPT -A INPUT -p tcp -j DROP -A INPUT -p udp -j DROP # allow outgoing connections for email and DNS -A OUTPUT -d 127.0.0.1/8 -j ACCEPT -A OUTPUT -p tcp -m tcp --dport smtp -j ACCEPT -A OUTPUT -p tcp -m tcp --dport domain -j ACCEPT -A OUTPUT -p udp -m udp --dport domain -j ACCEPT -A OUTPUT -j DROP COMMIT EXAMPLES
lockout lock 2h30m [locks out for 2h and 30m] lockout lock 90m [locks out for 1h and 30m] lockout lock 3pm [locks out until 3pm] lockout lock 3:20am [locks out until 3:20am] lockout lock 15:20 [locks out until 3:20pm] lockout status [shows when the system is going to be unlocked] FILES
/etc/lockout/lock.sh: executed when running lockout lock /etc/lockout/unlock.sh: executed when running lockout unlock SEE ALSO
usermod(8), iptables(8), passwd(1), cron(8), crontab(1) BUGS
Arguably, a program that changes the root password to something random with the possibility of never recovering the original password might be considered a bug by itself. Other than that, no known bugs. AUTHOR
Thomer M. Gil, http://thomer.com/lockout/ lockout 2004-09-08 LOCKOUT(1)
All times are GMT -4. The time now is 08:17 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy