1300 11 77 16 support@ramtech.net.au

Cisco IOS Macros

Ever needed to make an alteration that would mean you could no longer access the router when you did?  Let’s say you have a router with a DSL interface connected to your ISP.  Say you are accessing it remotely and need to change the username and password then bounce the interface.  Remotely, this is a PITA, as, as soon as you issue the shut command  to bounce the interface, that’s it.  Game over you can no longer access the router because the dialer interface is shut down and you have no way to bring it back up without booting the router.  If you boot the router without writing the changes to the startup-config, you have achieved nothing but booting the router and annoying everyone connected.

But sneaky Cisco have hidden a macro functionality in the IOS that allows us to do this without having to resort to EEM or fancy tclsh scripts.  Here’s an example how it works…

First we create our macro

R1(config)# macro name macroNewUserPass
Enter macro commands one per line. End with the character '@'.
interface dialer 0
ppp chap hostname TMPHOST
ppp chap password 0 TMPPASS
shut
no shut
exit@

Next, we need to Run our macro with the substituted actual username and password, instead of the Keyword place holders that I have there now (TMPHOST and TMPPASS).

R1(config)# macro global apply macroNewUserPass TMPHOST "username@isp.com" TMPPASS "It's a s3cr3t"

And that’s it. So to pull apart this last command… The macro command puts us in to macro mode in the config, the global apply tells it that this is going to be applied in global config, the macro name is obvious.  The last part is simply Keyword Substitution.  When the Macro named macroNewUserPass runs the keyword TMPHOST becomes username@isp.com and the TMPPASS keyword becomes it’s a s3cr3t

So while we will loose connection as soon as the shut is issued on the dialer interface, the macro keeps running and issues the no shut and brings the interface back up again for us.
Now, in this particular example, depending on whether this interface bounce has resulted in an WAN IP address change or not, you may need to ssh in to a different IP than before. But that’s a little outside the scope of this document.

Cisco VTI Example

Virtual Tunnel Interface or VTI (Also known as Route Based VPN) are an excellent choice for site to site IPSec encrypted VPNs terminating on Cisco IOS devices. You can always use the older Crypto maps but I find VTIs so much easier to deal with and to debug. If you are terminating on an ASA firewall or another non Cisco device Crypto Maps (or Policy Based VPNs as they’re known) may be your only choice, but where you have a choice, choose VTIs.

In Jeremy Stretch’s Excellent Blog he covers the differences in these two posts.
http://packetlife.net/blog/2011/aug/15/policy-based-vs-route-based-vpns-part-1/
http://packetlife.net/blog/2011/aug/17/policy-based-vs-route-based-vpns-part-2/

I won’t go into what each stage of the VTI does. This is just a quick example to get you started so you can research the finer points yourself and learn what it all does.

You generally need at least IPSECURITY IOS image or AdvanceIP for VPNs to work in any variation of the IOS.

The following is a very simple example of setting up a basic VTI site to site with IPSec Encryption.

LAN(a) -----  Router1  ~~~WAN~~~  Router2  ----- LAN(b)
IP address assumptions.  But of course, use your own addressing schema...
LAN(a) Subnet - 192.168.10.0/24
LAN(b) Subnet - 192.168.50.0/24
R1LAN Address - 192.168.10.1
R2LAN Address - 192.168.50.1
R1WAN Address - 1.2.3.4
R2WAN Address - 6.8.10.12

On Router1

crypto isakmp policy 10
 encr aes
 hash md5
 authentication pre-share
 group 2
crypto isakmp key 0 [Enter PSK Here] address 6.8.10.12 no-xauth
crypto ipsec transform-set tsVTI esp-aes esp-md5-hmac 
crypto ipsec profile profVTI
 set transform-set tsVTI 
interface Loopback0
 description Loopback Interface for UnNumbered VTIs
 ip address 10.1.1.1 255.255.255.252
interface Tunnel0
 description LAN(a) -- LAN(b)
 ip unnumbered Loopback0
 tunnel source Dialer0
 tunnel destination 6.8.10.12
 tunnel mode ipsec ipv4
 tunnel protection ipsec profile profVTI

On Router2

crypto isakmp policy 10
 encr aes
 hash md5
 authentication pre-share
 group 2
crypto isakmp key 0 [Enter the Same PSK here] address 1.2.3.4 no-xauth
crypto ipsec transform-set tsVTI esp-aes esp-md5-hmac 
crypto ipsec profile profVTI
 set transform-set tsVTI 
interface Loopback0
 description Loopback Interface for UnNumbered VTIs
 ip address 10.1.1.2 255.255.255.252
interface Tunnel0
 description LAN(a) -- LAN(b)
 ip unnumbered Loopback0
 tunnel source Dialer0
 tunnel destination 1.2.3.4
 tunnel mode ipsec ipv4
 tunnel protection ipsec profile profVTI

Now all that is needed is a route for each router to be able to find the other LAN down the VPN Tunnel. The most basic example, shown here, is a static route on each router but you may want to use a proper routing protocol.

Router1(Config)# ip route 192.168.50.0 255.255.255.0 10.1.1.1
Router2(Config)# ip route 192.168.10.0 255.255.255.0 10.1.1.2

You can also use the form ip route 192.168.50.0 255.255.255.0 tunnel 0 but this may have issues with proxy arp traffic if there is a lot of traffic on the tunnel (SIP traffic for example).

You may also want to keep in mind that if you have an ACL or some firewall variant protecting your WAN facing interface you will need to allow UDP 500 and ESP in, at a minimum, for this to work.

Cisco IP ACL Configuration

A while ago poor Cisco Freak from the Whirlpool Forum and others had the arduous task of trying to get me to properly understand what happens when a Cisco IP Access Control List (ACL) is applied to an interface as in or out.
This sounds simple enough and for 20 years or so I had thought it was. That’s until I gave some (rather poor) advice to a poor soul regarding blocking some VLAN traffic on his Cisco switch. Due to some misconceptions on my part my advice was errant. The above mentioned gurus set about patiently (at least it seemed patiently to me) correcting my minor, yet important misconceptions regarding the nuances of applying Cisco ACLs to interfaces.

I have since found what I reckon is the best explanation of the concept I have read. And surprisingly it is from the cisco site.

The relevant section is…

When you refer to a router, these terms have these meanings.

Out—Traffic that has already been through the router and leaves the interface. The source is where it has been, on the other side of the router, and the destination is where it goes.
In—Traffic that arrives on the interface and then goes through the router. The source is where it has been and the destination is where it goes, on the other side of the router.
Inbound —If the access list is inbound, when the router receives a packet, the Cisco IOS software checks the criteria statements of the access list for a match. If the packet is permitted, the software continues to process the packet. If the packet is denied, the software discards the packet.
Outbound—If the access list is outbound, after the software receives and routes a packet to the outbound interface, the software checks the criteria statements of the access list for a match. If the packet is permitted, the software transmits the packet. If the packet is denied, the software discards the packet.

The in ACL has a source on a segment of the interface to which it is applied and a destination off of any other interface. The out ACL has a source on a segment of any interface other than the interface to which it is applied and a destination off of the interface to which it is applied.

For this to cement with me I had to imagine I am a Traffic Cop on a router interface.  So to keep it simple, let’s imagine a simple router that only has one LAN and one WAN interface.  Then imagine you are the Policeman standing guard on the LAN interface on that router.  That means you stand facing (connected to) the LAN (probably a switch). In this instance, Inbound traffic is coming at you head on, headed for the router.

So if an ACL is applied in on you that means you are deciding if it should be allowed from the LAN into the Router.  if an ACL is applied out on you that means it you are deciding if it should be allowed from the Router to the LAN.  In both cases you will look at your rule book and consider if the traffic is permissible or not. Hence why it is important to block traffic at it’s source to conserve precious CPU resources.

Back to the analogy, being the good Cop you are you know that if you get a positive match on your list you stop looking any further.  So you see a deny statement for a packet you instantly deflect it and don’t have to deal with it anymore.  You don’t keep looking down the list to see if it’s permitted elsewhere.  Equally if it’s permitted, you don’t keep looking to see if it is later denied.  That’d be silly.

Just remember the Cop (ACL) always faces the direction the interface is connected to.  LAN faces the LAN, WAN faces the WAN, VLAN faces that VLAN.  That’s a tricky one.  The VLAN ACLs inbound traffic is coming at it from the VLAN headed elsewhere!  Hence, on a VLAN ACL, if the ACL is applied out, it is traffic destined for the VLAN!  If you remember/understand that, the rest is a cinch.

RR

Editing Cisco ACLs

There are a number of different type of Cisco ACLs available on their various IOS powered devices.  Of these the numbered, and named types are the most prevalent.  For my money I always use named where possible as I find them a lot neater and easier to edit live.  With a numbered ACL, if you need to change the order of the ACL (remember they are processed sequentially so the order is very important)  you need to do something like

  • copy the whole ACL into vi (or your favourite editor)
  • edit it to suit your new requirements
  • delete the old one from the config using something like no ip access-list 158
  • paste the edit one in to the config.

The problem with this is that this quite often isn’t possible if your ACL controls your ability to access the router/switch.  The same situation can exist if you don’t know how to edit named ACLs while they are live.  This method should really only be done on routers you are connected to by the console and aren’t live.

An easier solution is simply to edit named ACLs live. Now this isn’t necessarily for the faint of heart, and can easily end in a router/switch boot if you aren’t careful, but it is the “best” method in my opinion.

So how to do it…

First, you need to understand that named ACLs actually have line numbers.  I will also stress again that the ACL is assessed sequentially, so the order is imperative. Also remember, the ACL stops processing as soon as it gets a positive match.

Now if you show the ACL you will get something like this…

Router1(config)# do sh access-l aclInternetInbound
Extended IP access list aclInternetInbound
 10 permit tcp any any established (3579 matches)
 20 permit udp any any eq domain (13 matches)
 30 deny ip 12.0.0.0 1.255.255.255 any
 40 deny ip 24.200.0.0 0.3.255.255 any
 50 deny ip 24.248.0.0 0.7.255.255 any
 60 deny ip 38.0.0.0 0.255.255.255 any
 70 deny ip 41.177.0.0 0.0.255.255 any
 80 deny ip 41.208.192.0 0.0.63.255 any
 90 deny ip 58.192.0.0 0.31.255.255 any (22 matches)
 100 deny ip 59.32.0.0 0.31.255.255 any
 110 deny ip 59.151.0.0 0.0.127.255 any
 120 deny ip 60.0.0.0 0.31.255.255 any
 130 deny ip 61.12.0.0 0.0.127.255 any
 140 deny ip 61.32.0.0 0.7.255.255 any
 ...
 2050 permit tcp any any eq smtp  (4006 matches)

As you can see this is part of a mail server router ACL (that blocks a lot of our chinese and russian friends from bothering us) and we can see the line numbers shown. So to edit it while live we simple enter the ACL configuration and edit the line numbers directly. The other lines remain unaffected and in their original order. We can just as easily remove offending Line Numbers if necessary…
Edit: Please note the “Saftey Switch” of   reload in 10   (minutes). This reloads the router config to the current startup config if we stuff up our editing and lose connection. Thanks to Daniel996 for reminding me. (I Hope the 996 means He owns a Duke…)

Router1(config)# do reload in 10
Router1(config)# ip access ext aclInternetInbound
Router1(config-ext-nacl)# no 50                                  <--- Deletes line number 50
Router1(config-ext-nacl)# 65 deny ip 41.0.0.0 0.0.255.255 any    <--- Adds a line number 65 between 60 and 70
Router1(config-ext-nacl)# exit
Router1(config)# do sh access-l aclInternetInbound
Extended IP access list aclInternetInbound
 10 permit tcp any any established (3579 matches)
 20 permit udp any any eq domain (13 matches)
 30 deny ip 12.0.0.0 1.255.255.255 any
 40 deny ip 24.200.0.0 0.3.255.255 any
 60 deny ip 38.0.0.0 0.255.255.255 any
 65 deny ip 41.0.0.0 0.0.255.255 any
 70 deny ip 41.177.0.0 0.0.255.255 any
 80 deny ip 41.208.192.0 0.0.63.255 any
 90 deny ip 58.192.0.0 0.31.255.255 any (22 matches)
 100 deny ip 59.32.0.0 0.31.255.255 any
 110 deny ip 59.151.0.0 0.0.127.255 any
 120 deny ip 60.0.0.0 0.31.255.255 any
 130 deny ip 61.12.0.0 0.0.127.255 any
 140 deny ip 61.32.0.0 0.7.255.255 any
 ...
 2050 permit tcp any any eq smtp  (4020 matches)
Router1(config)# do reload cancel

Using this method it is easy to end up with the line numbers all over the place so Cisco have kindly provided a re-sequencing method to tidy the world up again.

Router1(config)# ip access-l res aclInternetInbound 10 10

this simple re-sequences the line numbers starting at number 10 and incrementing each line by 10. We then end up with …

Router1(config)# do sh access-l aclInternetInbound
Extended IP access list aclInternetInbound
 10 permit tcp any any established (3579 matches)
 20 permit udp any any eq domain (13 matches)
 30 deny ip 12.0.0.0 1.255.255.255 any
 40 deny ip 24.200.0.0 0.3.255.255 any
 50 deny ip 38.0.0.0 0.255.255.255 any
 60 deny ip 41.0.0.0 0.0.255.255 any
 70 deny ip 41.177.0.0 0.0.255.255 any
 80 deny ip 41.208.192.0 0.0.63.255 any
 90 deny ip 58.192.0.0 0.31.255.255 any (22 matches)
 100 deny ip 59.32.0.0 0.31.255.255 any
 110 deny ip 59.151.0.0 0.0.127.255 any
 120 deny ip 60.0.0.0 0.31.255.255 any
 130 deny ip 61.12.0.0 0.0.127.255 any
 140 deny ip 61.32.0.0 0.7.255.255 any
 ...
 2050 permit tcp any any eq smtp  (4072 matches)

A couple of things to remember…

  • All ACLs, number or named, have an implicit   deny ip any any   at the end of them.  So permit or deny the stuff you definitely want and let the implicit deny handle the rest.  Don’t clog your ACL’s with useless crud that will get picked up by the implicit deny anyway.  It just chew processor resources unnecessarily.
  • The ACL is assessed sequentially so be very careful with the order of statements.  No point putting a permit everything followed by a deny something explicit.
  • As soon as the ACL gets a positive match, it stops processing.  So a   deny ip any any   at the start of an ACL will ensure nothing ever gets through 🙂
  • Use   reload in 10   and   reload cancel   in case you stuff up.  They are life savers if the router or switch is remote. (Thanks again to Daniel996)

So there you go. Edit them live and save a lot of stress. But do be careful won’t you.

Decrypt Cisco type 7 Passwords right in the IOS

OK. Admit it. How often do you need to recover a type 7 password quickly? about once a month for me (he said embarrassed). 😐

there are a few site that let you do it from a page but what if you don’t have easy access to the Web? Well, help is at hand. Here’s how to do it from right there in the IOS…

Let’s assume it’s something basic like your ISP password, but any password stored insecurely on the router with type 7 encryption is a candidate

interface Dialer3
 ppp chap password 7 094D4D1B1815070B1B0D17393C2B3A37
  1. Create a temporary Key Chain
  2. Add a Key to the chain
  3. Add a type 7 key-string to the key
  4. Show the chain to reveal the un-encrypted string
  5. Remove the Key Chain so as not to clutter your config with rubbish.
Router1(config)#key chain temp

Router1(config-keychain)#key 1

Router1(config-keychain-key)#key-string 7 094D4D1B1815070B1B0D17393C2B3A37
Router1(config)#do sh key chain temp
Key-chain temp:
    key 1 -- text "acrappypassword"
        accept lifetime (always valid) - (always valid) [valid now]
        send lifetime (always valid) - (always valid) [valid now]
Router1(config)#no key chain temp
Router1(config)#

And there you have it! Proof that passwords stored with type 7 encryption in the config really are crappy. 🙂

Lion Serial Console Cable update.

As previously mention in this article, I had been happily using a serial console cable on my Macs (here after known as an SCC) (mine is an Aten UC-232A) without incident, until I upgraded my Mac Book Air to Lion.  Then it all broke again.

Here’s what I had to do to fix it…

First, I had to download the new 1.4.0 driver from here.  But that is only the start of it.  You need to follow the instructions contained in the Zip file and once the driver is installed, boot the Mac.

Next you need to edit the Plist file to reflect your actual hardware.  It is owned by root so you will need to sudo to be able to edit it. You need to plug your SCC and use System Profiler to get it’s Product ID and Vendor ID.  Remember these are in Hex, but when you edit the Info.plist the entries are in Decimal.  I found it easier to create a whole new <dict> entry rather than trashing the original installed by the driver.

Mine looks like this in System Profiler

Product ID: 0x2008
Vendor ID: 0x0557  (ATEN International Co. Ltd.)
Version: 3.00
Speed: Up to 12 Mb/sec
Manufacturer: Prolific Technology Inc.
Location ID: 0x04100000 / 4
Current Available (mA): 500
Current Required (mA): 100

So…

sudo vi /System/Library/Extensions/ProlificUsbSerial.kext/Contents/Info.plist

I added the following section after the original <key>067B_2303</key> Section

<key>0557_2008</key>
<dict>
   <key>CFBundleIdentifier</key>
   <string>com.prolific.driver.PL2303</string>
   <key>IOClass</key>
   <string>com_prolific_driver_PL2303</string>
   <key>IOProviderClass</key>
   <string>IOUSBInterface</string>
   <key>bConfigurationValue</key>
   <integer>1</integer>
   <key>bInterfaceNumber</key>
   <integer>0</integer>
   <key>idProduct</key>
   <integer>8200</integer>
   <key>idVendor</key>
   <integer>1367</integer>
</dict>

Once this is complete and saved you need to unplug the the SCC (very important) and load the edited kext file.

sudo kextload /System/Library/Extensions/ProlificUsbSerial.kext

Plug the SCC back in and do an ls on /dev and you should see your SCC in there now.  Mine looks like this…

crw-rw-rw-  1 root    wheel      18,   6  9 Feb 13:42 tty.usbserial

So if all has gone well you can now connect to your serial device again. For me that is any number of Cisco Routers or switches and the command is…

screen /dev/tty.usbserial 9600

Have Fun!