Python & Notepad++, (Sat, Sep 7th)

[Python -& Notepad++](/forums/diary/Python+Notepad/31240/)==========================================================* * [](http://www.facebook.com/sharer.php?u=https%3A%2F%2Fisc.sans.edu%2Fforums%2Fdiary%2F31240 ‘Share on Facebook’)* [](http://twitter.com/share?text=Python%20%26%20Notepad%2B%2B&url=https%3A%2F%2Fisc.sans.edu%2Fforums%2Fdiary%2F31240&via=SANS_ISC ‘Share on Twitter’) **Published** : 2024-09-07. **Last Updated** : 2024-09-07 13:11:33 UTC **by** [Didier Stevens](/handler_list.html#didier-stevens) (Version: 1) [0 comment(s)](/diary/Python+Notepad/31240/#comments) [PythonScript](https://npppythonscript.sourceforge.net/docs/latest/index.html) is a [Notepad++](https://notepad-plus-plus.org/) plugin that provides a Python interpreter to edit Notepad++ documents.You install PythonScript in Notepad++ like this:![](https://isc.sans.edu/diaryimages/images/20240907-084324.png)![](https://isc.sans.edu/diaryimages/images/20240907-084414.png)![](https://isc.sans.edu/diaryimages/images/20240907-084500.png)![](https://isc.sans.edu/diaryimages/images/20240907-084529.png)Use ‘New Script’ to create a new Python script:![](https://isc.sans.edu/diaryimages/images/20240907-084641.png)As an example, I will create a template substitution script, something that I use often. You provide a substitution template as input, and then each line of the open document is substituted according to the given template.First we create the script substitute.py:![](https://isc.sans.edu/diaryimages/images/20240907-084923.png)This is the template substitution script I developed:“`python hljsdef Substitute(contents, lineNumber, totalLines): contents = contents.rstrip(‘-n-r’) if contents != ”: editor.replaceLine(lineNumber, template.replace(token, contents))token = notepad.prompt(‘Provide a token’, ‘Substitute token’, ‘%%’)template = notepad.prompt(‘Provide a template’, ‘Substitute template’, ”)if token != None and template != None: editor.forEachLine(Substitute)“`You can paste it into Notepad++:![](https://isc.sans.edu/diaryimages/images/20240907-091633.png)I will now demonstrate the script on a new document I created in Notepad++: the list of today’s top 10 scanning IP addresses:![](https://isc.sans.edu/diaryimages/images/20240907-085812.png)For each IP address, I want to generate a command that I will then execute.The script can now be invoked to be executed on this open document like this:![](https://isc.sans.edu/diaryimages/images/20240907-093325.png)The first line of Python script substitute.py to be executed, is line 6 (*token = notepad.prompt…*). It prompts the user for a token string (default %%), this is a string that, when used in the template string, will be replaced by each line in the open document![](https://isc.sans.edu/diaryimages/images/20240907-085841.png)Line 7 prompts the user for a template string:![](https://isc.sans.edu/diaryimages/images/20240907-085917.png)When the user has not cancelled answering the prompts (tested in line 8), line 9 (*editor.forEachLine(Substitute)*) is executed: it runs function Substitute on each line of the document:![](https://isc.sans.edu/diaryimages/images/20240907-085939.png)Then I can copy/paste all these generated commands into a cmd.exe console:![](https://isc.sans.edu/diaryimages/images/20240907-090043.png)![](https://isc.sans.edu/diaryimages/images/20240907-090226.png)This example is a bit contrived, as you could also use a for loop in the scripting shell to achieve the same result.I also use this Python script often when I’m programming. Say that I want to hardcode this list of scanning IP addresses in a Python script. I can quickly create a Python list as follows:![](https://isc.sans.edu/diaryimages/images/20240907-150202.png)![](https://isc.sans.edu/diaryimages/images/20240907-150219.png)![](https://isc.sans.edu/diaryimages/images/20240907-150246.png)![](https://isc.sans.edu/diaryimages/images/20240907-150304.png)And then I add the variable assignment statemnt and create a list:![](https://isc.sans.edu/diaryimages/images/20240907-150350(1).png)Didier Stevens Senior handler [blog.DidierStevens.com](http://blog.DidierStevens.com) Keywords:[0 comment(s)](/diary/Python+Notepad/31240/#comments)

Related Tags:
NAICS: 518 – Computing Infrastructure Providers

Data Processing

Web Hosting

Related Services

NAICS: 92 – Public Administration

NAICS: 51 – Information

cmd.exe

cmd

Blog: SANS Internet Storm Center

Command and Scripting Interpreter

Associated Indicators:
null