This forum is in archive mode. You will not be able to post new content.

Author Topic: My first python project - "Mac-Socket"  (Read 1181 times)

0 Members and 1 Guest are viewing this topic.

Offline shome

  • Peasant
  • *
  • Posts: 81
  • Cookies: 8
    • View Profile
My first python project - "Mac-Socket"
« on: December 22, 2014, 10:53:05 AM »
Hey folks. This is nothing extravagant, as I am pretty darn new to coding. This script has multiple functions built into it to achieve a few different things. It's main basis is to spoof the MAC address, and has another option to look up host names, and call it using 'tor-resolve'. To change the MAC to factory settings, I called 'mac-changer', so make sure tor is up and running and mac-changer if you plan on using it.

 Let me know what you think. I am about 6 weeks into coding in python, and just trying to stay busy with it. Also, I called alot of external programs with this, but I used basic unix commands to change the mac to the one specified.

In plain english, for not coding too long i'm kind of proud of this. It's a bit bloated, and lengthy, but just felt myself mashing the keys, so I went with it. Any feedback is appreciated. Thank You.

Code: [Select]
#This script can be used to spoof a MAC HW address on just about any interface. Make sure to use the proper format when it asks to input the new mac address. I wrote this script as a research project, and embedded UNIX command's inside the two functions. If you are running windows, you will definitely want to modify the os.system call(s) to dos commands."""

#12/13/14 Update: rootkitfunction() added. When this function is selected, the log files will be deleted (experimental purposes only)

import os   # Import's os module, enable's access to the operating system's command prompt. *nix /bin/bash, /bin/sh etc. Windows cmd.exe
import time
import socket
from datetime import datetime


def rootkitfunction(): 
   choice=raw_input("Start 'Cover Tracks'?: (y,n)")
   if choice == "y":
      print("Covering Tracks...")
      time.sleep(3)
      os.system("echo "" > /var/log/auth.log")
      os.system("echo "" > ~/.bash_history")
      os.system("echo "" > $HOME/.bash_history")
      os.system("rm ~/.bash_history -rf")
      os.system("history -c")
      os.system("export HISTFILESIZE=0")
      os.system("export HISTSIZE=0")
      os.system("unset HISTFILE")
      os.system("kill 9 $$")
      os.system("ln /dev/null ~/.bash_history -sf")
""" Deletes all log files on linux system - has to be run as root !"""

def mon():     
   os.system("sudo ifconfig mon0 down")
   time.sleep(1)
   newmac=raw_input("What new mac do you want to issue mon0 (monitor mode mac): ")
   print("OK. New MAC selected as: %s")% (newmac)
   var="sudo ifconfig mon0 hw ether "+newmac
   os.system(var)
   print("Bringing mon0 back up")
   time.sleep(1)
 

def ipget():
   hostname=raw_input("Enter Hostname: ")
   print("Connecting to tor network. Sockets up. [Check]")
   cmd1="tor-resolve "+hostname
   os.system(cmd1)
   time.sleep(2)
   nmap=raw_input("Would you like to call nmap to scan this host for a simple SYN scan?  (y)(n)")
   if nmap=="y":
       cmd="nmap -v -sS "+hostname
       os.system(cmd)
   if nmap=="n":
       print("Exiting Program")
       time.sleep(2)
       os.system("clear")
   else:
      print("Error ! exiting....")
      os.system("clear")
       

def wireless():
    print("MAC-Socket by shome. You have selected the wireless setting")
    print("Printing current MAC")
    time.sleep(1)
    os.system("ifconfig wlan0 | grep HWaddr")
    time.sleep(3)
    choice=input("Press (1) to spoof mac address (2) to exit/restart program, (3) to reset wlan0 MAC to factory settings: ")
    if choice==1:
        time.sleep(2)
        nmachoice=raw_input("Do you want to change mac manually(1), or choose from a list(2): ")
        if nmachoice == 1:
              os.system("ifconfig wlan0 down")
              print "Wireless Interface down [OK]"
              newmac=raw_input("What would you like to change the MAC address to: ")
              time.sleep(2)
              cmd="sudo ifconfig wlan0 hw ether "+newmac
              os.system(cmd)
              print("Re-Enabling wlan0 Interface.")
              os.system("sudo ifconfig wlan0 up")
              os.system("service network-manager restart")
              print "Wireless Interface Re-Enabled[OK], Network-Manager Re-Enabled[OK]"
              print("MAC Address Spoofed successfully to %s") % (newmac)
        if nmachoice == 2:
              list=['(1)2C:F0:EE:43:C7:5D','(2)3C:15:C2:CA:00:0C','(3)D8:A2:5E:93:10:9B','(4)48:F8:B3:38:4A:58','(5)C0:EA:E4:09:48:80','(6)00:11:22:33:44:55']
              print(list)
              newmacchoice=input("Please make your new MAC address selection from the provided MACS: (1-6)")
              if newmacchoice == 1:
                 print("Bringing down wlan0 interface")
                 time.sleep(2)
                 os.system("sudo ifconfig wlan0 down")
                 macselection=list[0]
                 print('Changing MAC to ',macselection)
                 newmcmd="sudo ifconfig wlan0 hw ether "+macselection
                 os.system(newcmd)
               
              if newmacchoice == 2:
                  print("Bringing down wlan0 interface: ")
                  os.system("sudo ifconfig wlan0 down")
                  time.sleep(2)
                  print "Wlan0 Interface down[OK] !"
                  macselection2=list[1]
                  print('Changing MAC to ',macselection2)
                  newcmd2="sudo ifconfig wlan0 hw ether "+macselection2
                  os.system(newcmd2)
                  print "MAC Spoofed Successfully"
                  time.sleep(2)
                  print "Double Checking ifconfig"
                  os.system("ifconfig wlan0")
                  time.sleep(4)
                  os.system("clear")

              if newmacchoice == 3:
                  print("Bringing down wlan0 interface: ")
                  os.system("sudo ifconfig wlan0 down")
                  time.sleep(2)
                  print "Wlan0 Interface down[OK] !"
                  macselection3=list[2]
                  print('Changing MAC to ',macselection3)
                  newcmd3="sudo ifconfig wlan0 hw ether "+macselection3
                  os.system(newcmd3)
                  time.sleep(3)
                  print "MAC Spoofed Successfully"
                  time.sleep(2)
                  print "Double Checking ifconfig"
                  os.system("ifconfig wlan0")
                  time.sleep(2)
                  os.system("clear")
               
        else:
           systeminput=input("Error ! (1)to exit (2) to restart script: ")
           if systeminput == 1:
              os.system("clear")
           elif systeminput == 2:
              os.system("python $HOME/Python/mac-sock*/mac-socket-beta.py")
         
           
 
       
              if choice==2:
                  restartchoice=raw_input("Would you like to restart the program:  y or n")
                  if restartchoice== "y":
                     os.system("python /home/shome/Python/mac-socket-project/mac-socket-beta.py")
                  if restartchoice=="n":
                     print("Exiting")
                     time.sleep(2)
                     os.system("clear")
              if choice==3:
                  print("Changing MAC to Permanent settings")
                  time.sleep(2)
                  print("Calling UNIX utility 'macchanger' to the rescue...")
                  time.sleep(1)
                  os.system("ifconfig wlan0 down")
                  os.system("macchanger -p wlan0")
                  time.sleep(1)
                  os.system("service network-manager restart")
                  os.system("ifconfig wlan0 up")
                  print("MAC successfully changed back to factory settings.")
              else:
                  print("Error! Exiting")
       
                  def ethernet():
                     print("MAC Python Script by shome. You have selected the eth0 setting")
                     time.sleep(1)
                     print("Printing current MAC")
                     time.sleep(1)
                     os.system("ifconfig eth0 | grep HWaddr")
                     time.sleep(1)
                     choice=input("Press (1) to change your mac address, (2) to exit/restart program, (3) to reset eth0 MAC interface to factory settings: ")
                     if choice==1:
                        time.sleep(1)
                        print("Shutting down eth0 Interface.")
                        os.system("ifconfig eth0 down")
                        time.sleep(2)
                        print("eth0 Interface down. Check")
                        time.sleep(2)
                        print("Before I ask you what new MAC address you want, here is a couple to choose from")
                        os.system("cat $HOME/Python/mac-socket-project/MACS")
                        time.sleep(2)
                        newmac=raw_input("What would you like to change the MAC address to: ")
                        print("Changing new mac address to %s") % (newmac)
                        time.sleep(2)
                        cmd="ifconfig eth0 hw ether "+newmac
                        os.system(cmd)
                        print("Re-Enabling eth0 Interface.")
                        os.system("ifconfig eth0 up")
                        os.system("service network-manager restart")
                     elif choice==2:
                        restartchoice=raw_input("Would you like to restart the program:  y or n: ")
                        if restartchoice== "y":
                           os.system("python $HOME/Python/mac-socket-project/mac-socket.py")
                        if restartchoice=="n":
                           print("Exiting")
                           time.sleep(2)
                           os.system("clear")

                     elif choice==3:
                           print("Changing MAC to Permanent settings")
                           time.sleep(2)
                           print("Calling UNIX utility 'macchanger' to the rescue...")
                           time.sleep(1)
                           os.system("macchanger -p wlan0")
                           time.sleep(1)
                           print("MAC successfully changed back to factory settings.")
           
                     else:
                        print("Error! Exiting")

now=datetime.now()
print("Mac-Socket project by SHome")
time.sleep(1)
print("The current date is: %s/%s/%s") % (now.month,now.day,now.year)
time.sleep(1)
print("And the time is: %s:%s") % (now.hour,now.minute)
time.sleep(1)           
time.sleep(1)
print("make sure you are running this as root.")
time.sleep(1)
choiceinterface=input("Usage: (1) for Mac Spoofing option, (2) for 'cover tracks': ") # 'cover tracks' option - see rootkitfunction() ^
if choiceinterface==1:

   interface=input("Please select interface that MAC will be spoofed: eth0(1), wlan0(2), (3)mon0, or select (4) to convert a hostname to an ip address:  ")
   if interface==1:
      ethernet()
   elif interface==2:
      wireless()
   if interface==3:
      mon()
   elif interface==4:
      ipget()
   else:
      elsechoice=raw_input("Error ! No selection made. Restart? (y)(n): ")
      if elsechoice == "y":
         time.sleep(2)
         os.system("clear")
         os.system("python $HOME/Python/mac-socket-project/mac-socket-beta.py")
      elif elsechoice == "n":
         print("Exiting...")
         time.sleep(2)
         os.system("clear")

if choiceinterface == 2:
   rootkitfunction()
     
     


Offline d4rkcat

  • Knight
  • **
  • Posts: 287
  • Cookies: 115
  • He who controls the past controls the future. He who controls the present controls the past.
    • View Profile
    • Scripts
Re: My first python project - "Mac-Socket"
« Reply #1 on: December 22, 2014, 11:34:54 AM »
Nice work.

Some notes:
1. Python scripts should use python modules to be 'pythonic', pretty much everything in this script is using os.system to run normal linux commands. Therefore it would be simpler to write this as a bash script.
An os.system wrapper does not teach you anything about python.

2. Badly named functions. Functions names should reflect the purpose of the function, rootkitfunction() is not a good name for a function that deletes some linux logs.

3. One of the reasons Python is great because it is multi-platform, try to make this work with osX aswell.

4. Use the argparse module to allow your program to have arguments, it will be much more useable than this skiddy menu style program and it will also make it easier to code and maintain, all these questions to the user and unhandled exceptions are just Urgghh it's totally unnecessary for a script this simple.

5. macchanger is good for the job, but i guess osx needs one.

Let me know how you get on.
Good work.
Jabber (OTR required): thed4rkcat@einfachjabber.de    Email (PGP required): thed4rkcat@yandex.com    PGP Key: here and here     Blog

<sofldan> not asking for anyone to hold my hand uber space shuttle door gunner guy.


Offline shome

  • Peasant
  • *
  • Posts: 81
  • Cookies: 8
    • View Profile
Re: My first python project - "Mac-Socket"
« Reply #2 on: December 22, 2014, 11:46:20 AM »
I appreciate the input very much.

As time went on, I knew I was getting carried away with the excessive use of 'os.system', but just felt like seeing where it went. I'm enrolled on codeacademy.com, as well as nose deep in a pretty good python book also. I'm probably going to end up deleting alot from that script, and simplify it to just one or two raw_input's from the user instead of that mess. Again, thank you for the notes.

Offline madf0x

  • Knight
  • **
  • Posts: 172
  • Cookies: 50
    • View Profile
Re: My first python project - "Mac-Socket"
« Reply #3 on: December 22, 2014, 12:14:51 PM »
not bad for a first project.

besides the advice on using argparse, I'd also recommend using logger to output status messages, then you can add into your program a --verbose(-v) or a --quiet(-q) depending on what you wan the default behavior to be, in order to set the log level for the whole program. That way you can use a single argument to easily control output which allows your tool in turn to be more easily scripted. You may never need to do so now, but perhaps at some point youll feel like running certain aspects of this automatically whenever you log on via .bashrc or whatever your preference, but dont want it spewing a bunch of stuff every time you open a terminal.

Also none of your functions take arguments. You should try to make your programs more generic without taking their own input from the user. Instead have them take input from function arguments, then use your main function and/or argparse to call your functions with the correct input. I hope that made sense, other coders should know what I mean but Im trying to explain it for someone new to this basic idea.

Offline Spectrum_963

  • Peasant
  • *
  • Posts: 83
  • Cookies: 20
  • Where shit goes down
    • View Profile
Re: My first python project - &quot;Mac-Socket&quot;
« Reply #4 on: December 22, 2014, 07:27:17 PM »
Nice job op. +1 While I'm kinda new to python, I can't add anything anyone else already did (yeeaaah, batch woulda been simpler. I'll second that) but I like it.
"The same human mind that creates the most beautiful works of art and extraordinary marvels of technology is equally responsible for the perversion of it's own perfection."

Offline proxx

  • Avatarception
  • Global Moderator
  • Titan
  • *
  • Posts: 2803
  • Cookies: 256
  • ФФФ
    • View Profile
Re: My first python project - "Mac-Socket"
« Reply #5 on: December 22, 2014, 09:15:12 PM »
Code: [Select]
def wireless():
    print("MAC-Socket by shome. You have selected the wireless setting")
    print("Printing current MAC")
    time.sleep(1)
    os.system("ifconfig wlan0 | grep HWaddr")
    time.sleep(3)
    choice=input("Press (1) to spoof mac address (2) to exit/restart program, (3) to reset wlan0 MAC to factory settings: ")
    if choice==1:
        time.sleep(2)
        nmachoice=raw_input("Do you want to change mac manually(1), or choose from a list(2): ")
        if nmachoice == 1:
              os.system("ifconfig wlan0 down")
              print "Wireless Interface down [OK]"
              newmac=raw_input("What would you like to change the MAC address to: ")
              time.sleep(2)
              cmd="sudo ifconfig wlan0 hw ether "+newmac
              os.system(cmd)
              print("Re-Enabling wlan0 Interface.")
              os.system("sudo ifconfig wlan0 up")
              os.system("service network-manager restart")
              print "Wireless Interface Re-Enabled[OK], Network-Manager Re-Enabled[OK]"
              print("MAC Address Spoofed successfully to %s") % (newmac)
        if nmachoice == 2:
              list=['(1)2C:F0:EE:43:C7:5D','(2)3C:15:C2:CA:00:0C','(3)D8:A2:5E:93:10:9B','(4)48:F8:B3:38:4A:58','(5)C0:EA:E4:09:48:80','(6)00:11:22:33:44:55']
              print(list)
              newmacchoice=input("Please make your new MAC address selection from the provided MACS: (1-6)")
              if newmacchoice == 1:
                 print("Bringing down wlan0 interface")
                 time.sleep(2)
                 os.system("sudo ifconfig wlan0 down")
                 macselection=list[0]
                 print('Changing MAC to ',macselection)
                 newmcmd="sudo ifconfig wlan0 hw ether "+macselection
                 os.system(newcmd)
               
              if newmacchoice == 2:
                  print("Bringing down wlan0 interface: ")
                  os.system("sudo ifconfig wlan0 down")
                  time.sleep(2)
                  print "Wlan0 Interface down[OK] !"
                  macselection2=list[1]
                  print('Changing MAC to ',macselection2)
                  newcmd2="sudo ifconfig wlan0 hw ether "+macselection2
                  os.system(newcmd2)
                  print "MAC Spoofed Successfully"
                  time.sleep(2)
                  print "Double Checking ifconfig"
                  os.system("ifconfig wlan0")
                  time.sleep(4)
                  os.system("clear")

              if newmacchoice == 3:
                  print("Bringing down wlan0 interface: ")
                  os.system("sudo ifconfig wlan0 down")
                  time.sleep(2)
                  print "Wlan0 Interface down[OK] !"
                  macselection3=list[2]
                  print('Changing MAC to ',macselection3)
                  newcmd3="sudo ifconfig wlan0 hw ether "+macselection3
                  os.system(newcmd3)
                  time.sleep(3)
                  print "MAC Spoofed Successfully"
                  time.sleep(2)
                  print "Double Checking ifconfig"
                  os.system("ifconfig wlan0")
                  time.sleep(2)
                  os.system("clear")
               
        else:
           systeminput=input("Error ! (1)to exit (2) to restart script: ")
           if systeminput == 1:
              os.system("clear")
           elif systeminput == 2:
              os.system("python $HOME/Python/mac-sock*/mac-socket-beta.py")
         
           
 
       
              if choice==2:
                  restartchoice=raw_input("Would you like to restart the program:  y or n")
                  if restartchoice== "y":
                     os.system("python /home/shome/Python/mac-socket-project/mac-socket-beta.py")
                  if restartchoice=="n":
                     print("Exiting")
                     time.sleep(2)
                     os.system("clear")
              if choice==3:
                  print("Changing MAC to Permanent settings")
                  time.sleep(2)
                  print("Calling UNIX utility 'macchanger' to the rescue...")
                  time.sleep(1)
                  os.system("ifconfig wlan0 down")
                  os.system("macchanger -p wlan0")
                  time.sleep(1)
                  os.system("service network-manager restart")
                  os.system("ifconfig wlan0 up")
                  print("MAC successfully changed back to factory settings.")
              else:
                  print("Error! Exiting")
       
                  def ethernet():
                     print("MAC Python Script by shome. You have selected the eth0 setting")
                     time.sleep(1)
                     print("Printing current MAC")
                     time.sleep(1)
                     os.system("ifconfig eth0 | grep HWaddr")
                     time.sleep(1)
                     choice=input("Press (1) to change your mac address, (2) to exit/restart program, (3) to reset eth0 MAC interface to factory settings: ")
                     if choice==1:
                        time.sleep(1)
                        print("Shutting down eth0 Interface.")
                        os.system("ifconfig eth0 down")
                        time.sleep(2)
                        print("eth0 Interface down. Check")
                        time.sleep(2)
                        print("Before I ask you what new MAC address you want, here is a couple to choose from")
                        os.system("cat $HOME/Python/mac-socket-project/MACS")
                        time.sleep(2)
                        newmac=raw_input("What would you like to change the MAC address to: ")
                        print("Changing new mac address to %s") % (newmac)
                        time.sleep(2)
                        cmd="ifconfig eth0 hw ether "+newmac
                        os.system(cmd)
                        print("Re-Enabling eth0 Interface.")
                        os.system("ifconfig eth0 up")
                        os.system("service network-manager restart")
                     elif choice==2:
                        restartchoice=raw_input("Would you like to restart the program:  y or n: ")
                        if restartchoice== "y":
                           os.system("python $HOME/Python/mac-socket-project/mac-socket.py")
                        if restartchoice=="n":
                           print("Exiting")
                           time.sleep(2)
                           os.system("clear")

                     elif choice==3:
                           print("Changing MAC to Permanent settings")
                           time.sleep(2)
                           print("Calling UNIX utility 'macchanger' to the rescue...")
                           time.sleep(1)
                           os.system("macchanger -p wlan0")
                           time.sleep(1)
                           print("MAC successfully changed back to factory settings.")
           
                     else:
                        print("Error! Exiting")


You are doing it wrong :)
try something like this;
Code: [Select]
 
def wireless(choice):


            if choice == 1:
                   #do something here
            if choice == 2:
                   #do something else

            return something

def main():
           print "make a choice: "
           
           try:
                chosen = input():
                         wireless(chosen)
           except:
                print "idiot pick an int."


if __name__ == "__main__":
    main()

I hope you get the point , I am being a lazy ass.
And yes this is far from perfect, just trying to show how to use functions.

Apart from this I would use argument parsing instead of an interactive program , makes more sense.
$mycode.py changemac
or
$mycode.py resetmac
etc.
« Last Edit: December 22, 2014, 09:31:04 PM by proxx »
Wtf where you thinking with that signature? - Phage.
This was another little experiment *evillaughter - Proxx.
Evilception... - Phage

Offline kenjoe41

  • Symphorophiliac Programmer
  • Administrator
  • Baron
  • *
  • Posts: 990
  • Cookies: 224
    • View Profile
Re: My first python project - "Mac-Socket"
« Reply #6 on: December 24, 2014, 07:22:46 PM »
I was also going to say that drop the raw_input() stuff and learn to be dynamic with commandline args.
For practice and learning's sake, i would suggest that you take one at a time of each and every external program you called and implement a class for it in python. I mean you don't have to re-do all of their functionality but just those little you need in you script. You are going to end up learning some OOP and alot of software engineering techniques. And the code will most probably work on almost all systems that support python if you try to keep away from system specific code.

I honestly don't want to delete all the logs on the system and that could either be detrimental or raise alot of alarms.

Google some mora and you will find alot of modules that do alot of what you had to call to the system programs to do for you. Reproducing already available code is not advisable unless for learning purposes but calling alot of external programs is very dangerous. Truth is, you are not learning alot this way, implement your minimal versions of those programs and you will be surprised at how much you will learn. [And people complain of lack of projects]

Anyway, as all those if choices grow in proxX's code[pseudo], thy could probably use a map.
If you can't explain it to a 6 year old, you don't understand it yourself.
http://upload.alpha.evilzone.org/index.php?page=img&img=GwkGGneGR7Pl222zVGmNTjerkhkYNGtBuiYXkpyNv4ScOAWQu0-Y8[<NgGw/hsq]>EvbQrOrousk[/img]

Offline d4rkcat

  • Knight
  • **
  • Posts: 287
  • Cookies: 115
  • He who controls the past controls the future. He who controls the present controls the past.
    • View Profile
    • Scripts
Re: My first python project - "Mac-Socket"
« Reply #7 on: December 31, 2014, 03:34:17 PM »
You may also be interested in this.

https://github.com/feross/SpoofMAC


It works on Linux, Mac and windows.
Jabber (OTR required): thed4rkcat@einfachjabber.de    Email (PGP required): thed4rkcat@yandex.com    PGP Key: here and here     Blog

<sofldan> not asking for anyone to hold my hand uber space shuttle door gunner guy.


 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.