Thursday, January 23, 2020

(Python) Setting nano up for Python scripting

There are much better IDEs for Python than using nano if you are doing app development. For quick hacks though nano works great. With a few tweaks it can be a much better experience.

For Python code the recommended spacing is to use:

Per the PEP: https://www.python.org/dev/peps/pep-0008/#indentation

  • Spaces are the preferred indentation method (Avoid using tabs).
  • Use 4 spaces per indentation level.
  • Limit all lines to a maximum of 79 characters (we dont fix this here)


I use nano all the time (convenience) but the default is an 8 space tab.

You can set it each time from using the nano arguments

nano -$ -clET 4 <filename.txt>

  • -$ = Enable soft line wrapping
  • -c  = Constantly show cursor position
  • -l = Show line numbers in front of text
  • -E =  Convert typed tabs to spaces
  • -T  <#cols> = Set width of a tab to #cols columns



or this can be permanently reset in the system nanorc file and just work when you launch nano:

nano /etc/nanorc

  • set tabsize 4
  • set tabstospaces
  • set constentshow
  • set linenumbers
  • set softwrap


Monday, October 8, 2018

(Python) GeoLocation of an IP address from the command line.



import sys
import json
import urllib.request

url = "http://ipinfo.io/" + sys.argv[1] + "/json"

#print(url)
with urllib.request.urlopen(url) as response:
    myDict = json.loads(response.read().decode())

try:
    outputHostname  = 'Hostname:\t{}\n'.format(myDict.get("hostname"))
    outputCity      = 'City:\t\t{}\n'.format(myDict.get("city"))
    outputRegion    = 'Region:\t\t{}\n'.format(myDict.get("region"))
    outputCountry   = 'Country:\t{}\n'.format(myDict.get("country"))
    outputLoc       = 'Loc:\t\t{}\n'.format(myDict.get("loc"))
    outputPostal    = 'Postal:\t\t{}\n'.format(myDict.get("postal"))
    outputPhone     = 'Phone:\t\t{}\n'.format(myDict.get("phone"))
    outputOrg       = 'Org:\t\t{}'.format(myDict.get("org"))

    output =  outputHostname + outputCity + outputRegion + outputCountry + outputLoc + outputPostal + outputPhone + outputOrg                             
    print(output)
except:
    output = 'IP Address: {}\nBogon: {}'.format(myDict.get('ip'),myDict.get('bogon'))
    print(output)

Sunday, April 16, 2017

(powershell) Remove line breaks from a file

I needed to remove the newlines from an exported certificate to get the public key.
Nice and simple.


 $result = 'string_with_newlines'' -replace "`t|`n|`r",""

Saturday, May 28, 2016

My GIT Notes

GIT (to practice git “try.git.io"
======================

Setup:

One time system setup:
git config —global user.name <username> 
git config —global user.email test.user@gmail.com
git config —global push.default matching (for forward compatibility, optional)
git config —global alias.co checkout (This aliases checkout to co, optional)

Note: these settings are saved globally in the "/home/<username>/.gitconfig” file


Step 1) Creating a github repository and connecting it to your project. 

NOTE: you need to create a project repository on the github website then add remote target to git.

  on the github web site: "+New Repository” enter <projectname> 

  on the local machine in the <projectname> directory
git remote add origin https://github.com/<username>/<projetname>.git
  The previous line adds the following to the "<projectname>/.git/config” file. 
  [remote "origen"]
        url = https://github.com/<username>/sample_app.git
        fetch = +refs/heads/*:refs/remotes/origen/*
  [branch "master"]
       remote = origen
        merge = refs/heads/master
 
Step 2) Initializing and synchronizing the project 

initialize git: (use this the first time for each project)

cd <project root directory>

git init  (Create an empty git repository or reinitialize a current one)

git add -A  (Adds all of the files in the current directory to a staging area {except what is in .gitignore})

git status (view files in staging area)

git commit -a -m “My log message goes here” (commit the changes to the repository {NOTE: local only})

-m “” lets you add a message to each commit. You can view the commit messages using "git log
-a (be careful with the ‘-a’ this adds all changed files and commits them. This may or may not be what you want_ 

git add . (USE THIS VERY CAREFULLY AND NOT MUCH!!!!, This will add all data including images to your commit)

git push -u origin master (this will fail if you did not set up the github repository)


Branching
—————
git checkout -b branch-a  (This creates a new branch named “branch-a” and puts code into it)

git branch (This lists all of the current branches)
* branch-a
  master

git checkout master (Return to the master branch, Master is used for known working code. Do not post in progress code to this branch)

git diff branch-a  (This will compare the files in the master branch with the files in the “branch-a” branch and show you the difference)

To Delete a Branch
————————-
git branch -d branch-a (This will delete a branch ONLY if it has been merged, Use -D if you want to deleted regardless of merge state)



To save a work in progress if you need to go work on something else:
————————————————————————————————
git checkout -b branch-b 

git commit -am “WIP: This is a work in progress, needed to work on patch on master”

git checkout master

Tweak app

git commit -am “Fix to master done”

git checkout branch-b


To merge the branches, navigate to the branch you want code MOVED INTO and run merge:
————————————————————————————————————————

git checkout master

git merge branch-b -m “Merged branch-b features with master"


GitFlow
——————
Produces a diagram of the flow of the get matenence. 


Git Tree
———————
git tree

edit you .gitconfig and add this to the bottom
nano ~/.gitconf

tree = log —graph —decorate -oneline —all —color

Monday, May 25, 2015

(Powershell) Zip / Unzip


Add-Type -AssemblyName System.IO.Compression.FileSystem
function Unzip
{
    param([string]$zipfile, [string]$outpath)

    [System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
}

Unzip "C:\a.zip" "C:\a"

Friday, May 15, 2015

(PowerShell) Quick and dirty NetCat style listener

I was trying to come up with something in PowerShell that would listen on an arbitrary port and return a string. This seems to do the job perfectly.


while ($true)
{
    $listener = new-object System.Net.Sockets.TcpListener([System.Net.IPAddress]::Parse('0.0.0.0'), 42000)

    $listener.start()

    $client = $listener.AcceptTcpClient()

    $stream = $client.GetStream()
   
    $stWriter = new-Object System.IO.StreamWriter($stream)
   
    $stWriter.WriteLine("Put your text right here")
    $stWriter.Flush
    $stWriter.Close()

    $client.Close()

    $listener.Stop()

    write-host "Connection closed."
}

Thursday, April 30, 2015

(Metasploit) Clear console history

This is going here just so I don't have to go looking for it again.
I was trying to figure out how to clear the Metasploit command history to have a fresh msfconsole experience. It turns out that it is stored in the /root/.msf4/history file.

So we check the file sizes of the history and log files using tree -ha
















and clear them out.

echo "" > /root/.msf4/history

Done