Project

General

Profile

Todo #532 ยป snort_rules_edit.php

Ivan Radaev, 04/22/2010 04:39 AM

 
1
#!/usr/local/bin/php
2
<?php
3
/*
4
	system_edit.php
5
	Copyright (C) 2004, 2005 Scott Ullrich
6
	All rights reserved.
7

    
8
	Adapted for FreeNAS by Volker Theile (votdev@gmx.de)
9
	Copyright (C) 2006-2009 Volker Theile
10
	
11
	Adapted for Pfsense Snort package by Robert Zelaya
12
	Copyright (C) 2008-2009 Robert Zelaya
13

    
14
	Using dp.SyntaxHighlighter for syntax highlighting
15
	http://www.dreamprojections.com/SyntaxHighlighter
16
	Copyright (C) 2004-2006 Alex Gorbatchev. All rights reserved.
17

    
18
	Redistribution and use in source and binary forms, with or without
19
	modification, are permitted provided that the following conditions are met:
20

    
21
	1. Redistributions of source code must retain the above copyright notice,
22
	   this list of conditions and the following disclaimer.
23

    
24
	2. Redistributions in binary form must reproduce the above copyright
25
	   notice, this list of conditions and the following disclaimer in the
26
	   documentation and/or other materials provided with the distribution.
27

    
28
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
29
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
30
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
32
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37
	POSSIBILITY OF SUCH DAMAGE.
38
*/
39

    
40
require_once("guiconfig.inc");
41
require_once("config.inc");
42
require_once("/usr/local/pkg/snort/snort_gui.inc");
43
require_once("/usr/local/pkg/snort/snort.inc");
44

    
45
if (!is_array($config['installedpackages']['snortglobal']['rule'])) {
46
	$config['installedpackages']['snortglobal']['rule'] = array();
47
}
48

    
49
//nat_rules_sort();
50
$a_nat = &$config['installedpackages']['snortglobal']['rule'];
51

    
52
$id = $_GET['id'];
53
if (isset($_POST['id']))
54
	$id = $_POST['id'];
55
	
56
$ids = $_GET['ids'];
57
if (isset($_POST['ids']))
58
	$ids = $_POST['ids'];
59
	
60

    
61
if (isset($id) && $a_nat[$id]) {
62

    
63
	$pconfig['enable'] = $a_nat[$id]['enable'];
64
	$pconfig['interface'] = $a_nat[$id]['interface'];
65
	$pconfig['rulesets'] = $a_nat[$id]['rulesets'];
66
}
67

    
68
/* convert fake interfaces to real */
69
$if_real = convert_friendly_interface_to_real_interface_name2($pconfig['interface']);
70

    
71

    
72
$file = $_GET['openruleset'];
73

    
74
//read snort file
75
$filehandle = fopen($file, "r");
76

    
77
//get rule id
78
$lineid = $_GET['ids'];
79

    
80
//read file into string, and get filesize
81
$contents2 = fread($filehandle, filesize($file));
82

    
83
//close handler
84
fclose ($filehandle);
85

    
86
//delimiter for each new rule is a new line
87
$delimiter = "\n";
88

    
89
//split the contents of the string file into an array using the delimiter
90
$splitcontents = explode($delimiter, $contents2);
91

    
92
//copy rule contents from array into string
93
$tempstring = $splitcontents[$lineid];
94

    
95
function write_rule_file($content_changed, $received_file)
96
{
97
    //read snort file with writing enabled
98
    $filehandle = fopen($received_file, "w");
99

    
100
    //delimiter for each new rule is a new line
101
    $delimiter = "\n";
102

    
103
    //implode the array back into a string for writing purposes
104
    $fullfile = implode($delimiter, $content_changed);
105

    
106
    //write data to file
107
    fwrite($filehandle, $fullfile);
108

    
109
    //close file handle
110
    fclose($filehandle);
111

    
112
}
113

    
114

    
115

    
116
if($_POST['highlight'] <> "") {
117
	if($_POST['highlight'] == "yes" or
118
	  $_POST['highlight'] == "enabled") {
119
		$highlight = "yes";
120
	} else {
121
		$highlight = "no";
122
	}
123
} else {
124
	$highlight = "no";
125
}
126

    
127
if($_POST['rows'] <> "")
128
	$rows = $_POST['rows'];
129
else
130
	$rows = 1;
131

    
132
if($_POST['cols'] <> "")
133
	$cols = $_POST['cols'];
134
else
135
	$cols = 66;
136

    
137
if ($_POST)
138
{
139
	if ($_POST['save']) {
140
		
141
		/* get the changes */
142
	    $rule_content2 = $_POST['code'];
143
	
144
		//copy string into file array for writing
145
	    $splitcontents[$lineid] = $rule_content2;
146
	
147
	    //write the new .rules file
148
	    write_rule_file($splitcontents, $file);
149
		
150
		header("Location: /snort/snort_rules_edit.php?id=$id&openruleset=$file&ids=$ids");	
151
		
152
	}
153
}
154

    
155
$pgtitle = array(gettext("Advanced"), gettext("File Editor"));
156

    
157
//
158
?>
159

    
160
<?php include("head.inc");?>
161

    
162
<body link="#000000" vlink="#000000" alink="#000000">
163
<table width="100%" border="0" cellpadding="0" cellspacing="0">
164
	<tr>
165
		<td class="tabcont">
166
			<form action="snort_rules_edit.php?id=<?=$id; ?>&openruleset=<?=$file; ?>&ids=<?=$ids; ?>" method="post">
167
				<?php if ($savemsg) print_info_box($savemsg);?>
168
				<table width="100%" cellpadding='9' cellspacing='9' bgcolor='#eeeeee'>
169
					<tr>
170
						<td>
171
							<input name="save" type="submit" class="formbtn" id="save" value="save" /> <input type="button" class="formbtn" value="Cancel" onclick="history.back()">
172
							<hr noshade="noshade" />
173
							<?=gettext("Disable original rule"); ?>:
174
							<input id="highlighting_enabled" name="highlight2" type="radio" value="yes" <?php if($highlight == "yes") echo " checked=\"checked\""; ?> />
175
							<label for="highlighting_enabled"><?=gettext("Enabled"); ?></label>
176
							<input id="highlighting_disabled" name="highlight2" type="radio" value="no"<?php if($highlight == "no") echo " checked=\"checked\""; ?> />
177
							<label for="highlighting_disabled"><?=gettext("Disabled"); ?></label>
178
						</td>
179
					</tr>
180
				</table>
181
				<table width='100%'>
182
					<tr>
183
						<td valign="top" class="label">
184
							<div style="background: #eeeeee;" id="textareaitem">
185
							<!-- NOTE: The opening *and* the closing textarea tag must be on the same line. -->
186
							<textarea  wrap="off" style="width: 98%; margin: 7px;" class="<?php echo $language; ?>:showcolumns" rows="<?php echo $rows; ?>" cols="<?php echo $cols; ?>" name="code"><?php echo $tempstring;?></textarea>
187
							</div>
188
						</td>
189
					</tr>
190
				</table>
191
				<table width='100%'>
192
					<tr>
193
						<td valign="top" class="label">
194
							<div style="background: #eeeeee;" id="textareaitem">
195
							<!-- NOTE: The opening *and* the closing textarea tag must be on the same line. -->
196
							<textarea   disabled wrap="off" style="width: 98%; margin: 7px;" class="<?php echo $language; ?>:showcolumns" rows="33" cols="<?php echo $cols; ?>" name="code2"><?php echo $contents2;?></textarea>
197
							</div>
198
						</td>
199
					</tr>
200
				</table>
201
				<?php // include("formend.inc");?>
202
			</form>
203
		</td>
204
	</tr>
205
</table>
206
<script class="javascript" src="/snort/syntaxhighlighter/shCore.js"></script>
207
<script class="javascript" src="/snort/syntaxhighlighter/shBrushCSharp.js"></script>
208
<script class="javascript" src="/snort/syntaxhighlighter/shBrushPhp.js"></script>
209
<script class="javascript" src="/snort/syntaxhighlighter/shBrushJScript.js"></script>
210
<script class="javascript" src="/snort/syntaxhighlighter/shBrushJava.js"></script>
211
<script class="javascript" src="/snort/syntaxhighlighter/shBrushVb.js"></script>
212
<script class="javascript" src="/snort/syntaxhighlighter/shBrushSql.js"></script>
213
<script class="javascript" src="/snort/syntaxhighlighter/shBrushXml.js"></script>
214
<script class="javascript" src="/snort/syntaxhighlighter/shBrushDelphi.js"></script>
215
<script class="javascript" src="/snort/syntaxhighlighter/shBrushPython.js"></script>
216
<script class="javascript" src="/snort/syntaxhighlighter/shBrushRuby.js"></script>
217
<script class="javascript" src="/snort/syntaxhighlighter/shBrushCss.js"></script>
218
<script class="javascript">
219
<!--
220
  // Set focus.
221
  document.forms[0].savetopath.focus();
222

    
223
  // Append css for syntax highlighter.
224
  var head = document.getElementsByTagName("head")[0];
225
  var linkObj = document.createElement("link");
226
  linkObj.setAttribute("type","text/css");
227
  linkObj.setAttribute("rel","stylesheet");
228
  linkObj.setAttribute("href","/snort/syntaxhighlighter/SyntaxHighlighter.css");
229
  head.appendChild(linkObj);
230

    
231
  // Activate dp.SyntaxHighlighter?
232
  <?php
233
  if($_POST['highlight'] == "yes") {
234
    echo "dp.SyntaxHighlighter.HighlightAll('code', true, true);\n";
235
    // Disable 'Save' button.
236
    echo "document.forms[0].Save.disabled = 1;\n";
237
  }
238
?>
239
//-->
240
</script>
241
<?php //include("fend.inc");?>
242

    
243
</body>
244
</html>
    (1-1/1)